spring mvc @SessionAttributes not binding properly

2019-08-22 16:43发布

问题:

I have a Portlet controller like this. Here I bind a variable to Session. After that it redirects to another controller and renders the jsp .

@SessionAttributes({"attrName"})
public class Controller{
public void manage(ModelMap modelMap)  {
modelMap.addAttribute("attrName", true)
response.sendRedirect(URL_CONTROLLER_2);
}
}

So this redirects to another controller that renders the jsp. In jsp when I do:

alert("${attrName}")

I get null. Why am I not able to see the attribute. Even when I debug and I check ModelMap in controller # 2, the attribute "attrName" is not present in session.

回答1:

AFAIK it's an expected behaviour because @SessionAttributes are not shared among different controllers. Attributes saved this way will be removed from session as soon as next controller is called.



标签: spring-mvc