deepakP
deepakP

Reputation: 13

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 .

Upvotes: 0

Views: 2924

Answers (3)

jeremycoulter
jeremycoulter

Reputation: 9

Sessions work great as well. Cheers!

Upvotes: -1

deepakP
deepakP

Reputation: 13

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.

Upvotes: 0

jeremycoulter
jeremycoulter

Reputation: 9

I'd recommend completing this short tutorial (https://spring.io/guides/gs/handling-form-submission/). It illustrates how model attributes can be sent from a client page to the server as well as from the server to the client page.

Once the model is populated to its final state, the model can correspond to a table in your database as is illustrated in this tutorial (https://spring.io/guides/gs/accessing-data-mysql/) via Hibernate and the @Entity class.

Cheers!

Upvotes: 1

Related Questions