Using SessionAttributes in a Spring MVC Project

    Introduction


    This article describes the implementation of a greatly simplified movie ticket booking process. It is assumed that the user enters the data necessary 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 is saved in the form of a session, at the end of the reservation the 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 are used in the form of view.

    The user navigates in the following order:

    • booking.jsp: movie selection page
    • customer.jsp: personal data entry page
    • payment.jsp: payment information entry page
    • confirmation.jsp: confirmation page


    Listing: booking.jsp

    <% @ page contentType = "text / html; charset = UTF-8" language = "java"%>
    <% @ taglib uri = "http://www.springframework.org/tags/form" prefix = "form"%>
    
    
        Booking Start Page
            Movie ID: 


    Listing: customer.jsp

    <% @ page contentType = "text / html; charset = UTF-8" language = "java"%>
    <% @ taglib uri = "http://www.springframework.org/tags/form" prefix = "form"%>
    
    
        Customer Page
            Last name: 


    Listing: payment.jsp

    <% @ page contentType = "text / html; charset = UTF-8" language = "java"%>
    <% @ taglib uri = "http://www.springframework.org/tags/form" prefix = "form"%>
    
    
        Payment Page
            CreditCard Number: 


    Listing: confirmation.jsp

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


    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 saved in the session. @SessionAttributes can also be used with an 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 start () the form will be copied to http session attributes thanks 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 with 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 with customer.jsp, but it was saved in session during selectMovie ()
            Assert.notNull (ticketForm.getMovieId ());
            // lastName was not passed with payment.jsp, but it was saved in 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 personal data security
            return "booking / confirmation";
        }
    }
    


    In addition


    You can control the 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";
        }
    


    By calling status.setComplete (); you end the Spring session, SessionAttributes will be deleted, while maintaining the HTTP session.

    Also popular now: