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
- Package com.itmyhome;
- Import java.util.ArrayList;
- Import Java.util.HashMap;
- Import java.util.List;
- Import Java.util.Map;
- Import Org.springframework.stereotype.Controller;
- Import org.springframework.web.bind.annotation.RequestMapping;
- Import Org.springframework.web.servlet.ModelAndView;
- @Controller
- Public class Login {
- @RequestMapping (value="login")
- Public Modelandview Login () {
- Modelandview Mav = new Modelandview ();
- Mav.setviewname ("Welcome"); //Returns the file name
- Mav.addobject ("message","Hello Kitty");
- //list
- list<string> list = new arraylist<string> ();
- List.add ("Java");
- List.add ("C + +");
- List.add ("Oracle");
- Mav.addobject ("Booklist", list);
- //map
- map<string,string> map = new hashmap<string,string> ();
- Map.put ("Zhangsan", "Beijing");
- Map.put ("Lisi", "Shanghai");
- Map.put ("Wangwu", "Shenzhen");
- Mav.addobject ("map", map);
- return MAV;
- }
- }
Or to build your Modelandview object as follows
- @RequestMapping (value="logout")
- Public Modelandview logout () {
- String message = "Welcome to the next visit!" ";
- return new Modelandview ("logout","message", message);
- }
Then modify the welcome.jsp output data
Iterating through a collection can use the JSTL expression to introduce a header file into the JSP
- <%@ 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
- <body>
- <!--output normal characters --
- ${message} <br/>
- <!--output list --
- <p> book list </P>
- <c:foreach items="${booklist}" var="Node">
- <c:out value="${node}"></c:out>
- </C:foreach>
- <br/>
- <br/>
- <!--output map --
- <c:foreach items="${map}" var="Node">
- Name:<c:out value="${node.key}"></c:out>
- Address:<c:out value="${node.value}"></c:out>
- <br/>
- </C:foreach>
- </body>
Results
SpringMVC3 Learning--modelandview Objects (GO)