When the controller finishes processing the request, it typically returns the Modelandview object that includes 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, just change the login class
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; @Controllerpublic class Login {@RequestMapping (value= "Login") Public Modelandview Login () {Modelandview Mav = new Modelandview (); Mav.setviewname ("Welcome");//Return file name Mav.addobject ( "Message", "Hello Kitty");//listlist<string> list = new arraylist<string> (); List.add ("Java"); List.add ("C + + "), List.add (" Oracle "), Mav.addobject (" Booklist ", list);//mapmap<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, for example, the following methods to build your Modelandview object
@RequestMapping (value= "logout") public Modelandview logout () {String message = "Welcome to the next visit!" "; return new Modelandview (" Logout "," message "," message ");
Then change 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, where can these two jars be found, of course, can be downloaded on the Internet.
In addition, there are below Tomcat, under \webapps\examples\web-inf\lib .
The premise is that you have not deleted some of the useless items below WebApps.
welcome.jsp
<body> <!--output ordinary characters-- ${message} <br/> <!--output List-- <p> book listings </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 Study (ii)--modelandview object