Spring annotation @SessionAttributes

 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

 

 

Java code   Favorite code
  1. package  com.baobaotao.web;  
  2.   
  3. …  
  4. import org.springframework.ui.ModelMap;  
  5. import org.springframework.web.bind.annotation.SessionAttributes;  
  6.   
  7. @Controller  
  8. @RequestMapping("/bbtForum.do")  
  9. <span style= "color: #008000;" > @SessionAttributes ( "currUser" //①Name the attribute in ModelMap as the attribute of currUser  
  10. //Put it into the Session property list so that this property can be accessed across requests</span>  
  11. public class  BbtForumController {   
  12. …  
  13.     @RequestMapping(params = "method=listBoardTopic")  
  14.     public String listBoardTopic(@RequestParam("id")int topicId, User user,  
  15. ModelMap model) {  
  16.         bbtForumService.getBoardTopics (topicId);  
  17.         System.out.println("topicId:" + topicId);  
  18.         System.out.println("user:" + user);  
  19.         model.addAttribute( "currUser" ,user); <span style= "color: #008000;" > //②Add an attribute to ModelMap</span>  
  20.         return"listTopic";   
  21.     }  
  22.   
  23. }  

    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.

Java code   Favorite code
  1. @Controller  
  2. <span style="color: #008000;">@SessionAttributes("currentUser")</span>  
  3. publicclass GreetingController{   
  4. @RequestMapping  
  5. publicvoid hello<span style="color: #008000;">(@ModelAttribute("currentUser")</span> User user){   
  6.   //user.sayHello()  
  7. }  
  8. }  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326484232&siteId=291194637