0

I have a kind of online store in Spring. I want to create a shopping cart for each new user who visits the site and stored it in the session. How can this be done? It's just that I've never worked with sessions in Spring. Perhaps there is a good resource to study this stuff.

2 Answers2

0

Make your controller session scoped

@Controller
@Scope("session")

then add one attribute to session

@RequestMapping(method = RequestMethod.GET)
public String testMestod(HttpServletRequest request){
   ShoppingCart cart = (ShoppingCart)request.getSession().setAttribute("cart",valueOfCart);
   return "testJsp";
}

then Scope the user object that should be in session every time:

@Component
@Scope("session")
public class User
 {
    String user;
    /*getter setter*/
  }

then inject class in each controller that you want

@Autowired
   private User user

The AOP proxy injection : in spring -xml:

 <bean id="user"    class="com.User" scope="session">     
      <aop:scoped-proxy/>
  </bean>

refer:How to use Session attributes in Spring-mvc

0

@Controller class example{

@RequestMapping("/") public String test(HttpSession session){

session.addAttribute("cart",new Cart()); }

}

srikanth
  • 61
  • 3
  • How do I add an item to this cart now. Those. how to get this cart in another controller. And how can I then completely empty this basket? –  Jan 14 '21 at 12:46