Spring store object in session

后端 未结 4 1480
醉梦人生
醉梦人生 2020-12-04 09:15

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

4条回答
  •  难免孤独
    2020-12-04 09:24

    @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);
    }
    

提交回复
热议问题