问题
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"
Now I am in a Spring MVC environment.
For example, inside a Controller method, how can I save a variable in session?
Someone told me that a session-scoped bean would get the job.
Can you help me with a trivial code snippet?
回答1:
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);
...
}
来源:https://stackoverflow.com/questions/8680529/spring-mvc-http-session-management-equivalent