是否可以访问Spring MVC注释会话var跨多个控制器? [英] Is it possible to access Spring MVC annotated session vars across multiple controllers?
问题描述
我有一个运行Spring 3.0并使用Spring-MVC的Web应用程序。我有几个控制器设置如下:
@Controller
@RequestMapping(/ admin)
@SessionAttributes({clientLogin,selectTab,user,redirectUrl})
public class AdminController {
...
}
@Controller
@SessionAttributes({clientLogin,selectTab,user,redirectUrl})
public class PublicController {
....
}
我可以将注释变量添加到ModelMap中,例如
map.addAttribute(user,Bob);
在电流控制器中保持变量的效果很好。我可以从该控制器中的任何其他方法从模型映射访问var。但是当用户在另一个控制器中点击一个页面时,即使在@SessionAttributes中列出了同一个变量,它在第二个控制器中也不可用。
是否可以使用注释在多个控制器上访问这些注释变量?
如果你想在不同的控制器之间共享这些属性,你可以明确地把这些属性他们进入会话使用:
session.setAttribute()
I have a web app running Spring 3.0 and using Spring-MVC. I have a few controllers set up like this:
@Controller
@RequestMapping("/admin")
@SessionAttributes({"clientLogin", "selectTab", "user", "redirectUrl"})
public class AdminController {
...
}
@Controller
@SessionAttributes({"clientLogin", "selectTab", "user", "redirectUrl"})
public class PublicController {
....
}
I can add the annotated variables into the ModelMap with something like
map.addAttribute("user", "Bob");
That works fine to persist the variable in the current controller; I can access the var from the modelMap from any other method in that controller. But when the user hits a page in another Controller, even though the same variable is listed in the @SessionAttributes, it's not available in the second controller.
Is it possible to access these annotated variables across multiple controllers using the annotations?
No it isn't possible - SessionAttributes are badly named in my opinion.
If you want to share these attributes across different controllers, you can explicitly put them into the session using:
session.setAttribute()
这篇关于是否可以访问Spring MVC注释会话var跨多个控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!