📜 ⬆️ ⬇️

Using SessionAttributes in the Spring MVC project

Introduction


This article describes the implementation of a greatly simplified process of booking a movie ticket. It is assumed that the user enters data required for booking on several pages, the functionality is divided logically, i.e. on the first page, he enters data related to the session, on the second his personal data, on the third - data for payment. The last page is to confirm the reservation. All that the user enters - will be saved as a form in the session, at the end of the reservation data from the session is deleted.


')
Where to begin


Immediately make a reservation, it is assumed that Spring MVC is already used in your project, jsp is used in the form of view.

The user navigates in the following order:



Listing: booking.jsp

 <% @ page contentType = "text / html; charset = UTF-8" language = "java"%>
 <% @ taglib uri = "http://www.springframework.org/tags/form" prefix = "form"%>
 <html>
 <head>
     <title> Booking Start Page </ title>
 </ head>
 <body>
     <form: form action = "/ booking / movie" modelAttribute = "ticketForm">
         Movie ID: <form: input path = "movieId" />
         <input type = "submit" />
     </ form: form>
 </ body>
 </ html>


Listing: customer.jsp

 <% @ page contentType = "text / html; charset = UTF-8" language = "java"%>
 <% @ taglib uri = "http://www.springframework.org/tags/form" prefix = "form"%>
 <html>
 <head>
     <title> Customer Page </ title>
 </ head>
 <body>
     <form: form action = "/ booking / customer" modelAttribute = "ticketForm">
         Last Name: <form: input path = "lastName" />
         <input type = "submit" />
     </ form: form>
 </ body>
 </ html>


Listing: payment.jsp

 <% @ page contentType = "text / html; charset = UTF-8" language = "java"%>
 <% @ taglib uri = "http://www.springframework.org/tags/form" prefix = "form"%>
 <html>
 <head>
     <title> Payment Page </ title>
 </ head>
 <body>
     <form: form action = "/ booking / payment" modelAttribute = "ticketForm">
         CreditCard Number: <form: input path = "creditCardNumber" />
         <input type = "submit" />
     </ form: form>
 </ body>
 </ html>


Listing: confirmation.jsp

 <% @ page contentType = "text / html; charset = UTF-8" language = "java"%>
 <html>
 <head>
     <title> Confirmation Page </ title>
 </ head>
 <body>
     Thank you for your purchase!
 </ body>
 </ html>


The form:

 public class TicketForm {

     private String movieId;
     private String lastName;
     private String creditCardNumber;

     public String getMovieId () {
         return movieId;
     }

     public void setMovieId (String movieId) {
         this.movieId = movieId;
     }

     public String getLastName () {
         return lastName;
     }

     public void setLastName (String lastName) {
         this.lastName = lastName;
     }

     public String getCreditCardNumber () {
         return creditCardNumber;
     }

     public void setCreditCardNumber (String creditCardNumber) {
         this.creditCardNumber = creditCardNumber;
     }

 }


An example of using the @SessionAttributes annotation in the BookTicketController controller, now the TicketForm types attached to the model will also be stored in the session. @SessionAttributes can also be used with the attribute name in the model.

 import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;
 import org.springframework.util.Assert;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.SessionAttributes;
 import org.springframework.web.bind.support.SessionStatus;

 @Controller
 @RequestMapping (value = "/ booking")
 @SessionAttributes (types = TicketForm.class)
 public class BookTicketController {

     @RequestMapping (method = RequestMethod.GET)
     public String start (Model model) {
         // after exiting the start () form will be copied to the http session attributes due to @SessionAttributes (types = TicketForm.class)
         model.addAttribute (new TicketForm ()); 
         return "booking / booking";
     }

     @RequestMapping (value = "/ movie", method = RequestMethod.POST)
     public String selectMovie (TicketForm ticketForm) {

         Assert.notNull (ticketForm);
         Assert.notNull (ticketForm.getMovieId ());

         return "booking / customer";
     }

     @RequestMapping (value = "/ customer", method = RequestMethod.POST)
     public String enterCustomerData (TicketForm ticketForm) {

         Assert.notNull (ticketForm);
         // movieId was not transmitted from customer.jsp, but it was saved in session during selectMovie ()
         Assert.notNull (ticketForm.getMovieId ());
         Assert.notNull (ticketForm.getLastName ());

         return "booking / payment";
     }

     @RequestMapping (value = "/ payment", method = RequestMethod.POST)
     public String enterPaymentDetails (TicketForm ticketForm) {

         // movieId was not transmitted from customer.jsp, but it was saved in session during selectMovie ()
         Assert.notNull (ticketForm.getMovieId ());
         // lastName was not transmitted from payment.jsp, but it was saved in the session during enterCustomerData ()
         Assert.notNull (ticketForm.getLastName ());
         Assert.notNull (ticketForm.getCreditCardNumber ());

         return "redirect: / booking / confirmation";
     }

     @RequestMapping (value = "/ confirmation", method = RequestMethod.GET)
     public String confirmation (SessionStatus status) {
         status.setComplete ();  // clear Spring Session for security of personal data
         return "booking / confirmation";
     }

 }


In addition


You can control SessionAttributes by passing the SessionStatus parameter to the controller method.

 @RequestMapping (value = "/ confirmation", method = RequestMethod.GET)
     public String confirmation (SessionStatus status) {
         status.setComplete ();
         return "booking / confirmation";
     }


Calling status.setComplete (); you finish the Spring session, the SessionAttributes will be deleted, while maintaining the HTTP session.

Source: https://habr.com/ru/post/199048/


All Articles