Lydon Ch
Lydon Ch

Reputation: 8815

SessionAttribute and SpringFramework

How can I set session attribute using spring framework and annotation?

Something equivalent to

 request.getSession().setAttribute("key", "value");

Thanks.

Upvotes: 0

Views: 1468

Answers (1)

user178982
user178982

Reputation:

You can pass session as an attribute of controller's method and then use it directly without request.getSession(), however it isn't a big improvement and there are no annotations :)

public String handleRequest(HttpSession session) {
    session.setAttribute("key", "value");
}

EDIT:

You can also add attribute to ModelMap instance and then use @SessionAtributes annotation in these controller definitions in which you want to have that attribute inside ModelMap: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-sessionattrib

Upvotes: 2

Related Questions