Spring MVC会话属性访问 [英] Spring MVC Session Attribute Access

查看:71
本文介绍了Spring MVC会话属性访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在spring 3.0下访问HttpSession而不将其包含在方法签名中?我真正想做的是能够从一个可以为空的HttpSession中传入值。

Is there any way under spring 3.0 to access the HttpSession without including it in the method signature? What I really want to do is be able to pass in values from an HttpSession that CAN BE null.

这样的事情:

@RequestMapping("/myHomePage")
public ModelAndView show(UserSecurityContext ctx) {}

而不是:

@RequestMapping("/myHomePage")
public ModelAndView show(HttpSession session) {
      UserSecurityContext ctx = (UserSecurityContext) session.getAttribute("userSecurityCtx");
}


推荐答案

注释不适合这个任务 - 我认为它也是,但是读取位另有说明:

使用
指示的会话属性此注释对应于
特定处理程序的模型属性,
透明地存储在
会话会话中。一旦
处理程序指示其
会话会话完成,将删除这些
属性。因此,在特定的
处理程序的对话过程中,使用
此工具用于此类会话
属性,这些属性应该是临时存储在会话中的

Session attributes as indicated using this annotation correspond to a specific handler's model attributes, getting transparently stored in a conversational session. Those attributes will be removed once the handler indicates completion of its conversational session. Therefore, use this facility for such conversational attributes which are supposed to be stored in the session temporarily during the course of a specific handler's conversation.

对于永久会话属性,例如
用户身份验证对象,请使用
传统的session.setAttribute
方法。或者,
考虑使用通用
WebRequest接口的属性
管理功能。

For permanent session attributes, e.g. a user authentication object, use the traditional session.setAttribute method instead. Alternatively, consider using the attribute management capabilities of the generic WebRequest interface.

In换句话说, @SessionAttribute 用于在会话中存储会话MVC模型对象(而不是将它们存储为请求属性)。它不适用于任意会话属性。正如您所发现的那样,只有会话属性始终存在时它才有效。

In other words, @SessionAttribute is for storing conversation MVC-model objects in the session (as opposed to storing them as request attributes). It's not intended for using with arbitrary session attributes. As you discovered, it only works if the session attribute is always there.

我不知道任何其他替代方案,我认为你坚持使用 HttpSession.getAttribute()

I'm not aware of any other alternative, I think you're stuck with HttpSession.getAttribute()

这篇关于Spring MVC会话属性访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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