SpringMVC3 Learning--modelandview Objects

Source: Internet
Author: User

Original link: SpringMVC3 Learning (ii)--modelandview object

When the controller finishes processing the request, it typically returns the Modelandview object that contains the view name or view object and some model properties to Dispatcherservlet.

Therefore, it is often necessary to construct the Modelandview object in the controller. The Modelandview class provides several overloaded constructors and a number of convenient methods,

Allows you to construct Modelandview objects according to your preferences. These constructors and methods support view names and view objects in a similar way.

When you have only one model property to return, you can specify that property in the constructor to construct the Modelandview object

On the basis of the previous article, only the login class is modified


 
  1. Package com.itmyhome;
  2. Import java.util.ArrayList;
  3. Import Java.util.HashMap;
  4. Import java.util.List;
  5. Import Java.util.Map;
  6. Import Org.springframework.stereotype.Controller;
  7. Import org.springframework.web.bind.annotation.RequestMapping;
  8. Import Org.springframework.web.servlet.ModelAndView;
  9. @Controller
  10. Public class Login {
  11. @RequestMapping (value="login")
  12. Public Modelandview Login () {
  13. Modelandview Mav = new Modelandview ();
  14. Mav.setviewname ("Welcome"); //Returns the file name
  15. Mav.addobject ("message","Hello Kitty");
  16. //list
  17. list<string> list = new arraylist<string> ();
  18. List.add ("Java");
  19. List.add ("C + +");
  20. List.add ("Oracle");
  21. Mav.addobject ("Booklist", list);
  22. //map
  23. map<string,string> map = new hashmap<string,string> ();
  24. Map.put ("Zhangsan", "Beijing");
  25. Map.put ("Lisi", "Shanghai");
  26. Map.put ("Wangwu", "Shenzhen");
  27. Mav.addobject ("map", map);
  28. return MAV;
  29. }
  30. }


Or to build your Modelandview object as follows


 
    1. @RequestMapping (value="logout")
    2. Public Modelandview logout () {
    3. String message = "Welcome to the next visit!"  ";
    4. return new Modelandview ("logout","message", message);
    5. }

Then modify the welcome.jsp output data

Iterating through a collection can use the JSTL expression to introduce a header file into the JSP


 
    1. <%@ taglib uri="Http://java.sun.com/jsp/jstl/core" prefix="C"%>

Jstl.jar and Standard.jar are imported under Lib.

First of all, these two jars where can be found, of course, can be downloaded on the Internet.

In addition, under Tomcat, under \webapps\examples\web-inf\lib .

The premise is that you have not deleted some of the useless items under WebApps.

welcome.jsp


 
  1. <body>
  2. <!--output normal characters --
  3. ${message} <br/>
  4. <!--output list --
  5. <p> book list </P>
  6. <c:foreach items="${booklist}" var="Node">
  7. <c:out value="${node}"></c:out>
  8. </C:foreach>
  9. <br/>
  10. <br/>
  11. <!--output map --
  12. <c:foreach items="${map}" var="Node">
  13. Name:<c:out value="${node.key}"></c:out>
  14. Address:<c:out value="${node.value}"></c:out>
  15. <br/>
  16. </C:foreach>
  17. </body>

Results

SpringMVC3 Learning--modelandview Objects (GO)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.