session - Spring MVC SessionAttributes with ModelAttribute usage -


i trying learn spring mvc recently. seems did not understand functionality of @sessionattributes , @modelattribute annotations.

this part of controller:

@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 + 2;     }      return new modelandview(new redirectview("showallitems")); } 

basically there jsp listing items. wenn user click "addtocart" specific item, item added shoppingcart list. better explain understanding of controller first , can tell me not get.

first time when itemcontroller called, createshoppingcart , createcount methods executed , return parameters saved in session under names "shoppingcart" , "count". when user calls url ".../addtocart/1", addtocart method called. since need there in method signature 2 values session, controller in session whether values there. yes are.. @ time shoppingcart empty list, , count 0. in method body, selected item added list, count 2. jsp displayed again.

the problem is, jsp can see list shoppingcart not empty. count still 0. when add items basket, can see on jsp shoppingcart filled items, value of count 0.

actually there no difference between shoppingcart , count objects.. dont why behaves this. first doubted count type primitive int, changed integer typ, still problem not solved.

you don't change count (you can't in fact), assign it. model still points old value. have add new value manually.

mymodelandview.add("count", count); 

but why bothering count if can use warenkorb.size anyway?


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -