One, @ModelAttribute
By default, the attribute scope in the ModelMap is at the request level, that is, when the request ends, the attributes in the ModelMap will be destroyed. If you want to share the attributes in the ModelMap among multiple requests , you must dump the attributes into the session , so that the attributes of the ModelMap can be accessed across requests.
Spring allows us to selectively specify which attributes in the ModelMap need to be dumped into the session , so that these attributes can be accessed in the attribute list of the corresponding ModelMap for the next request . This function is achieved by annotating the @SessionAttributes annotation at the class definition .
Make a specific property of a model object Session -scoped
- package com.baobaotao.web;
- …
- import org.springframework.ui.ModelMap;
- import org.springframework.web.bind.annotation.SessionAttributes;
- @Controller
- @RequestMapping("/bbtForum.do")
- <span style= "color: #008000;" > @SessionAttributes ( "currUser" ) //①Name the attribute in ModelMap as the attribute of currUser
- //Put it into the Session property list so that this property can be accessed across requests</span>
- public class BbtForumController {
- …
- @RequestMapping(params = "method=listBoardTopic")
- public String listBoardTopic(@RequestParam("id")int topicId, User user,
- ModelMap model) {
- bbtForumService.getBoardTopics (topicId);
- System.out.println("topicId:" + topicId);
- System.out.println("user:" + user);
- model.addAttribute( "currUser" ,user); <span style= "color: #008000;" > //②Add an attribute to ModelMap</span>
- return"listTopic";
- }
- }
We add a ModelMap attribute at ②, whose attribute name is currUser , and at ① place the attribute named currUser in the ModelMap into the Session through the @SessionAttributes annotation , so we can not only request the corresponding JSP view in listBoardTopic() The user object can be obtained through request.getAttribute( " currUser " ) and session.getAttribute( " currUser " ) in the page, and session.getAttribute( " currUser " ) or ModelMap#get( can also be used in the JSP view page corresponding to the next request. " currUser " ) to access this property.
Here we only put the attributes of one ModelMap into the Session . In fact, @SessionAttributes allows multiple attributes to be specified. You can specify multiple attributes as an array of strings, such as @SessionAttributes({ " attr1 " , " attr2 " }) . In addition, @SessionAttributes can also specify the ModelMap attributes to be sessioned through the attribute type , such as @SessionAttributes(types = User.class) , of course, multiple classes can also be specified, such as @SessionAttributes(types = {User.class, Dept.class }) , you can also specify the attribute name and attribute type in combination: @SessionAttributes(types = {User.class,Dept.class},value={ " attr1 " , " attr2 " }) .
Two, @ModelAttribute
We can add @SessionAttributes to the controller that needs to access the Session attribute, and then add @ModelAttribute to the User parameter required by the action, and ensure that the attribute names of the two are the same. SpringMVC will automatically inject the attributes defined by @SessionAttributes into the ModelMap object. In the parameter list of the setup action, go to the ModelMap to get such an object and add it to the parameter list. As long as we don't call the setComplete() method of SessionStatus, this object will always remain in the Session, thus realizing the sharing of Session information.
- @Controller
- <span style="color: #008000;">@SessionAttributes("currentUser")</span>
- publicclass GreetingController{
- @RequestMapping
- publicvoid hello<span style="color: #008000;">(@ModelAttribute("currentUser")</span> User user){
- //user.sayHello()
- }
- }