Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Session attributes in Spring-mvc

Could you help me write spring mvc style analog of this code?

 session.setAttribute("name","value"); 

And how to add an element that is annotated by @ModelAttribute annotation to session and then get access to it?

like image 775
gstackoverflow Avatar asked Sep 13 '13 17:09

gstackoverflow


People also ask

What is the use of session attribute?

The session attribute indicates whether or not the JSP page uses HTTP sessions. A value of true means that the JSP page has access to a builtin session object and a value of false means that the JSP page cannot access the builtin session object.

How session is maintained in Spring MVC?

Have a look at the @SessionAttributes annotation, which allows you to define the attributes that will be stored in the session by your controller; this mechanism is mainly intended to maintain the conversational state for your handler and that state is usually cleared once the conversation is complete.

What is @SessionAttributes in Spring MVC?

@SessionAttribute annotation retrieve the existing attribute from the session. This annotation allows you to tell Spring which of your model attributes will also be copied to HttpSession before rendering the view.

How do I use session attribute in Thymeleaf?

Using session Object${session.name} will return the value of the name attribute stored in the current session. If no attribute with the specified name is found in the session, it will return null .


1 Answers

If you want to delete object after each response you don't need session,

If you want keep object during user session , There are some ways:

  1. directly add one attribute to session:

    @RequestMapping(method = RequestMethod.GET) public String testMestod(HttpServletRequest request){    ShoppingCart cart = (ShoppingCart)request.getSession().setAttribute("cart",value);    return "testJsp"; } 

    and you can get it from controller like this :

    ShoppingCart cart = (ShoppingCart)session.getAttribute("cart"); 
  2. Make your controller session scoped

    @Controller @Scope("session") 
  3. Scope the Objects ,for example you have user object that should be in session every time:

    @Component @Scope("session") public class User  {     String user;     /*  setter getter*/   } 

    then inject class in each controller that you want

       @Autowired    private User user 

    that keeps class on session.

  4. The AOP proxy injection : in spring -xml:

    <beans xmlns="http://www.springframework.org/schema/beans"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xmlns:aop="http://www.springframework.org/schema/aop"   xsi:schemaLocation="http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans-3.1.xsd     http://www.springframework.org/schema/aop     http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">    <bean id="user"    class="com.User" scope="session">            <aop:scoped-proxy/>   </bean> </beans> 

    then inject class in each controller that you want

    @Autowired private User user 

5.Pass HttpSession to method:

 String index(HttpSession session) {             session.setAttribute("mySessionAttribute", "someValue");             return "index";         } 

6.Make ModelAttribute in session By @SessionAttributes("ShoppingCart"):

  public String index (@ModelAttribute("ShoppingCart") ShoppingCart shoppingCart, SessionStatus sessionStatus) { //Spring V4 //you can modify session status  by sessionStatus.setComplete(); } 

or you can add Model To entire Controller Class like,

@Controller     @SessionAttributes("ShoppingCart")     @RequestMapping("/req")     public class MYController {          @ModelAttribute("ShoppingCart")         public Visitor getShopCart (....) {             return new ShoppingCart(....); //get From DB Or Session         }         } 

each one has advantage and disadvantage:

@session may use more memory in cloud systems it copies session to all nodes, and direct method (1 and 5) has messy approach, it is not good to unit test.

To access session jsp

<%=session.getAttribute("ShoppingCart.prop")%> 

in Jstl :

<c:out value="${sessionScope.ShoppingCart.prop}"/> 

in Thymeleaf:

<p th:text="${session.ShoppingCart.prop}" th:unless="${session == null}"> . </p> 
like image 195
Ali.Mojtehedy Avatar answered Sep 18 '22 04:09

Ali.Mojtehedy