Spring MVC:HTTP会话管理“等效" [英] Spring MVC: HTTP session management "equivalent"
问题描述
我来自PHP或ColdFusion之类的语言:如果我想在用户的http会话中保存某些内容,我会这样操作:
I come from languages like PHP or ColdFusion: if I wanted to save something in user's http session I act like this:
SESSION["foo"] = "bar"
现在我处于Spring MVC环境中.
Now I am in a Spring MVC environment.
例如,在Controller方法内部,如何在会话中保存变量?
For example, inside a Controller method, how can I save a variable in session?
有人告诉我,一个会话范围的bean 会得到这份工作.
Someone told me that a session-scoped bean would get the job.
您能帮我提供一个简短的代码段吗?
Can you help me with a trivial code snippet?
推荐答案
您可以使用会话范围的bean,并且在所有请求处理方法上也可以使用HttpServletRequest或HttpSession类型的参数.参见
You may use a session-scoped bean, and you may also have an argument of type HttpServletRequest or HttpSession on all your request handling methods. See http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#mvc-ann-methods.
@RequestMapping...)
public String processSubmit(..., HttpSession session, ...) {
...
session.setAttribute("someAttribute", someObject);
...
}
这篇关于Spring MVC:HTTP会话管理“等效"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!