Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring store object in session

I would like to implement a shopping cart with Spring, so I need to save an object Cart ( which has attributes like products, paymentType and deliveryType ) in session. I've tried to create it with bean and attribute "scope" set to "session", but it just doesn't work, should I use some additional annotations in my controller or Cart class? Any example usage would be really helpful :-) Thanks in advance.

like image 215
tomaszf Avatar asked Apr 05 '12 19:04

tomaszf


People also ask

How can get a session object in Spring?

Getting HttpSession Object in Spring Controller is very easy . Just Put it as a method parameter in controller method and Spring will automatically inject it . There is another approach where we create Session scoped Controller . This Controller get created for each session and controller object is stored in session.

What is the use of @SessionAttributes?

@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 can get a session object?

The Session object is created and made available through the context variable, $session . You do not need to perform any explicit call to create it. You can get a Session object by using the following syntax, if you already have a valid Entity object: $session=$entity->GetSession();


2 Answers

@Component @Scope("session") public class Cart { .. } 

and then

@Inject private Cart cart; 

should work, if it is declared in the web context (dispatcher-servlet.xml). An alternative option is to use the raw session and put your cart object there:

@RequestMapping(..) public String someControllerMethod(HttpSession session) {     session.setAttribute(Constants.CART, new Cart());     ...     Cart cart = (Cart) session.getAttribute(Constants.CART); } 
like image 151
Bozho Avatar answered Sep 23 '22 22:09

Bozho


If you are injecting the shopping cart directly into your controller, the issue is likely happening because your controller is singleton scoped (by default), which is wider scope than the bean you're injecting. This excellent article gives an overview of four approaches to exactly what you're trying to do: http://richardchesterwood.blogspot.co.uk/2011/03/using-sessions-in-spring-mvc-including.html.

Here's a quick summary of solutions:

  1. Scope the controller to session scope (use @scope("session") on controller level) and just have a shopping cart instance in the controller.
  2. Scope the controller to request and have session-scoped shopping cart injected.
  3. Just use the session directly - kind of messy, IMO.
  4. Use Spring's annotation <aop:scoped-proxy/>.

All of the methods have their pros and cons. I usually go with option 2 or 4. Option 4 is actually pretty simple and is the only approach I have seen documented by Spring.

like image 23
stephen.hanson Avatar answered Sep 23 '22 22:09

stephen.hanson