Search code examples
spring-bootspring-mvcthymeleaf

How to pass data from page to page in Thymeleaf based spring boot application


I am developing application in Spring boot and i am using Thymeleaf as a template engine. Its like a Ordering application where user selects option on Page 1, Page 2 and so on and at last page i have to save all the previously user selected options to DB.

Can anyone suggest what will be the best design approach to pass data from one page to another should i need to use session ? I have Model objects defined for each page and i am passing to and from data using these model object .


Solution

  • I did something like below

    @Component
    @Scope("session")
    public class Cart
    {
       // simple POJO fields
    }
    

    and then use this inside the Controller i want

    @Scope("request")
    public class SessionController
    {
       @Autowired
       private Cart cart;
    
       @RequestMapping("/addToCart")
       public String addToCart(@RequestParam("id") int id)
       {
           // 
       } 
    }
    

    since the scope of cart is session so i can use this model object to whichever controller i want put the value in cart object get the value from it wherever i want.