是否可以跨多个控制器访问 Spring MVC 带注释的会话变量? [英] Is it possible to access Spring MVC annotated session vars across multiple controllers?

查看:39
本文介绍了是否可以跨多个控制器访问 Spring MVC 带注释的会话变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行 Spring 3.0 并使用 Spring-MVC 的 Web 应用程序.我有几个控制器是这样设置的:

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 {
....
}

我可以使用类似

map.addAttribute("user", "Bob");

这很好地将变量持久化到当前控制器中;我可以从该控制器中的任何其他方法访问 modelMap 中的 var.但是当用户点击另一个控制器中的页面时,即使@SessionAttributes 中列出了相同的变量,它在第二个控制器中也不可用.

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?

推荐答案

不,这是不可能的 - 在我看来,SessionAttributes 的命名很糟糕.

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()

session.setAttribute()

这篇关于是否可以跨多个控制器访问 Spring MVC 带注释的会话变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆