1>Storing the return value of the method is ModelAndView. When returning, store the data in the ModelAndView object, such as:
newModelAndView("/WEBINF/jsp/","message",message)
The first parameter is url, the second parameter is the key of the data to be passed, and the third parameter is the data object.
It should be noted here that the data is stored in the request by default.
Example:
@RequestMapping(value="/mad/showData_1.do") public ModelAndView showData_1(){ String message = "This is the data to be passed"; /*The first parameter is url, the second parameter is the key of the data to be passed, and the third parameter is the data object. It should be noted here that the data is stored in the request by default. */ return new ModelAndView("/WEB-INF/jsp/","message",message); }
How to get the front desk page:
request:${}
2> You can add annotations in front of the class
@SessionAttributes({“message”,”user”})
This annotation can set the corresponding model parameters and also store a copy in the session. The parameters in this annotation are a set, and multiple parameters can be written, as in the above example, where message and user are keys of stored data.
Sample program:
@SessionAttributes({"message","user"}) //The corresponding data in the modelAndView will also be stored in the session
Page Get:
session:${}<br/>
3 > Data modelAndView returns a collection
This processing method is always the same as the above processing method, because the data type accepted by modelAndView is Object, and the collection is also handled the same way.
Example:
//Use the modelAndView object to pass the data to the foreground.//Pass multiple parameters (different types) @RequestMapping(value="/mad/showData_2.do") public ModelAndView showData_2(){ ("showData_2"); String message = "This is the data to be passed"; User user = new User("Zhang San", 12, new Date()); List<User> us= new ArrayList<User>(); (new User("Zhang San", 12, new Date())); (new User("Zhang Si", 13, new Date())); (new User("Zhang Wu", 14, new Date())); ModelAndView mad = new ModelAndView("/WEB-INF/jsp/"); //Save data into modelMap ("message", message); (user);//Default is lowercase of the first letter of the class name ("users", us); return mad; }
How to get the front desk page:
request:${}<br/> <c:forEach items="${ }"var="u"> ${ }-${ }-${ }<br/> </c:forEach>
4. Use modelAndView to pass multiple parameters.
Parameters can be set through the ModelAndView ("message", message); method.
The first parameter in this method is the key of the data, and the second parameter is the data object.
You can also use (user); method
The parameters of the data in this method are the data object, and the key of the data is the class name of the class of the object (the first letter is lowercase).
5. Use ModelMap to pass multiple data into jsp.
Adding a formal parameter to the method's parameter list ModelMap map, spring will automatically create a ModelMap object.
Then call put(key,value) or addAttribute(key,value) of the map to put the data into the map, and spring will automatically store the data into the request.
Example:
//Use modelMap object to pass data to the foreground. //Pass multiple parameters (different types) @RequestMapping(value="/mad/showData_3.do") public String showData_3(ModelMap map){ ("showData_3"); String message = "This is the data to be passed"; User user = new User("Zhang San", 12, new Date()); List<User> us= new ArrayList<User>(); (new User("Zhang San", 12, new Date())); (new User("Zhang Si", 13, new Date())); (new User("Zhang Wu", 14, new Date())); //Save data into modelMap ("message", message); ("user", user); ("users", us); return"/WEB-INF/jsp/"; }
Page call:
request:${}<br/> session:${ }<br/> application:${}<br/> <hr/> <h1>ModelMapData in</h1> ${}<br/> ${}<br/> <p>List</p> <c:forEach items="${}" var="u"> ${ }-${ }-${ }<br/> </c:forEach>
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.