The main research in this article is the relevant content of spring learning @SessionAttributes, as follows.
By default, the scope of the attributes in ModelMap is the request level, that is, when the request is completed, the attributes in ModelMap will be destroyed. If you want to share properties in ModelMap in multiple requests, you must transfer their properties to the session so that the properties of ModelMap can be accessed across requests.
spring allows us to selectively specify which properties in the ModelMap need to be transferred to the session so that these properties can also be accessed in the property list of the ModelMap corresponding to the next requested attribute. This function is implemented by labeling the @SessionAttributes annotation at the class definition.
Make specific properties of model objects have a Session scope scope
package com.baobaotao.web;… import org.springframework.ui.ModelMap;import org.springframework.web.bind.annotation.SessionAttributes;@Controller @RequestMapping("/bbtForum.do") @SessionAttributes("currUser") //① Put the attribute named currUser in the ModelMap into the Session attribute list so that this attribute can access 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);//②Add a property to ModelMap return "listTopic";}} We added a ModelMap attribute at ②, whose attribute is currUser, and at ①, we place the attribute named currUser in ModelMap into the Session through the @SessionAttributes annotation. Therefore, we can not only obtain the user object through request.getAttribute(“currUser”) and session.getAttribute(“currUser”) in the JSP view page corresponding to the listBoardTopic() request, but also access this attribute through session.getAttribute(“currUser”) or ModelMap#get(“currUser”) in the JSP view page corresponding to the next request.
Here we only put the attribute of a ModelMap into the Session. In fact, @SessionAttributes allows multiple attributes to be specified. You can specify multiple attributes through string arrays, such as @SessionAttributes({“attr1”,”attr2”}) . In addition ,@SessionAttributes can also specify the ModelMap attribute to be sessionized through the attribute type, such as @SessionAttributes(types = User.class) , and of course, multiple classes can also be specified, such as @SessionAttributes(types = {User.class,Dept.class}) , and can also use the attribute name and attribute type specification in conjunction with: @SessionAttributes(types = {User.class,Dept.class},value={“attr1”,”attr2”}) .
We can add @SessionAttributes to the controller that needs to access the Session property, and then add @ModelAttribute to the User parameter required by the action, and ensure that the attribute names of the two are consistent. SpringMVC will automatically inject the attributes defined by @SessionAttributes into the ModelMap object. When setting action parameter list, go to ModelMap to get such an object and add it to the parameter list. As long as we do not call setComplete() method of SessionStatus, this object will be kept in the Session, thereby realizing the sharing of Session information.
@Controller @SessionAttributes("currentUser")</span> public class GreetingController{@RequestMapping public void hello(@ModelAttribute("currentUser")User user){//user.sayHello()}}The above is all the content of this article about the @SessionAttributes instance analysis of spring learning. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!