Spring MVC-HttpSession.setAttribute和model.addObject之间的区别 [英] Spring MVC - difference between HttpSession.setAttribute and model.addObject

查看:382
本文介绍了Spring MVC-HttpSession.setAttribute和model.addObject之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近尝试学习Spring MVC.看来我不太了解@ModelAttribute批注和HttpSession的功能.

I am trying to learn Spring MVC recently. It seems that i did not understand well the functionalities of @ModelAttribute annotation and HttpSession.

@SessionAttributes({"shoppingCart", "count"})
public class ItemController {

@ModelAttribute("shoppingCart")
public List<Item> createShoppingCart() {
    return new ArrayList<Item>();
}

@ModelAttribute("count")
public Integer createCount() {
    return 0;
}

@RequestMapping(value="/addToCart/{itemId}", method=RequestMethod.GET)
public ModelAndView addToCart(@PathVariable("itemId") Item item, 
        @ModelAttribute("shoppingCart") List<Item> shoppingCart, @ModelAttribute("count") Integer count) {

    if(item != null) {
        shoppingCart.add(item);
        count = count + 1;
    }

    return new ModelAndView(new RedirectView("showAllItems")).addObject("count", count);
}

@RequestMapping(value="/deleteFromCart/{itemId}", method=RequestMethod.GET)
public ModelAndView deleteFromCart(@PathVariable("itemId") Item item, 
        HttpSession session) {

    List<Item> list = (List<Item>) session.getAttribute("shoppingCart");
    list.remove(item);
    //session.setAttribute("shoppingCart", list);

    Integer count = (Integer) session.getAttribute("count");
    count = count - 1;
    session.setAttribute("count", count);

    return new ModelAndView(new RedirectView("showAllItems"));
}

购物车和计数是会话属性.

ShoppingCart and count are the session attributes.

问题出在deleteFromCart方法中.我从会话中获取计数,在会话中重新分配并对其进行覆盖.但是我不能看到jsp计数的更新值.但是,可以看到更新的shoppingCart对象已更新,尽管我没有覆盖会话对象(因为该对象是与会话中已经存在的对象相同的对象).

The problem is in the deleteFromCart method. I get the count from session, reassign it and overwrite it in session. But i cant see the updated value of count on jsp. However, the updated shoppingCart object can be seen updated, although i do not overwrite the session object (since the object is the same object which is already in session).

但是为什么计数没有更新,尽管我用session.setAttribute覆盖了它? 当我将新的计数对象添加到模型中(model.addObject("count",count))时,我可以看到count的更新值.但是,为什么session.setAttribute没有给出相同的结果?

But why is the count not updated, although i overwrite it with session.setAttribute? When i add the new count object to the model (model.addObject("count", count)) then i can see the updated value of count. But why does not session.setAttribute give the same result?

推荐答案

首先,@SessionAttribute不必使用http会话.它使用SessionAttributeStore可以将任何内容作为其后备存储.仅默认实现使用http会话.

First of all, @SessionAttribute does not have to use the http session. It uses a SessionAttributeStore which can have anything as its backing storage. Only the default implementation uses the http session.

您的代码无法按预期工作的原因在于@SessionAttribute的工作方式.

The reason why your code does not work as expected lies in how @SessionAttribute works.

在调用控制器方法之前,将从会话中读取@SessionAttributes中列出的所有内容(在您的情况下为{"warenkorb", "count"})并添加到模型中.

Before a controller method is invoked, everything listed in @SessionAttributes, in your case {"warenkorb", "count"}, is read from the session and added to the model.

方法返回后,该会话将使用方法中已添加到模型中的所有内容进行更新.

After the method has returned the session is updated with everything that has been added to the model within the method.

.addObject("count", count)

->计数添加到模型中,然后添加到会话中.

-> count is added to the model and afterwards to the session.

session.setAttribute("count", count)

->计数将添加到会话中,但不会添加到模型中.在下一次调用任何控制器方法之前,它将被添加到模型中.但是到目前为止,该模型仍然具有旧的count.而模型就是添加到请求中的东西.而且,如果可以在请求范围内找到属性,则jsp不会在乎会话中的内容.

-> count is added to the session but not to the model. It will be added to the model before the next invocation of any controller method. But as for now the model still has the old count. And the model is what gets added to the request. And if an attribute can be found in the request scope then the jsp does not care about what's in the session.

当您使用@SessionAttributes@ModelAttribute(或通常是Spring MVC)时,请避免使用HttpSessionHttpRequest.甚至HttpResponse的使用也受到限制.代替Spring MVC的美丽:)

When you use @SessionAttributesand @ModelAttribute (or Spring MVC in general) then avoid using HttpSession or HttpRequest. Even HttpResponseis of limited use. Embrace the beauty of Spring MVC instead :)

这篇关于Spring MVC-HttpSession.setAttribute和model.addObject之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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