Spring MVC: HTTP session management “equivalent”

孤者浪人 提交于 2019-12-04 05:28:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!