会话中的Spring存储对象 [英] Spring store object in session

查看:123
本文介绍了会话中的Spring存储对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Spring实现购物车,所以我需要在会话中保存一个对象购物车(其中包含product,paymentType和deliveryType等属性)。我试图用bean创建它,并将属性scope设置为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.

推荐答案

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

然后

@Inject
private Cart cart;

应该有效,如果它在Web上下文中声明(dispatcher-servlet.xml)。另一种选择是使用原始会话并将您的购物车对象放在那里:

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

这篇关于会话中的Spring存储对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆