<% @ 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>
<% @ 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>
<% @ 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>
<% @ page contentType = "text / html; charset = UTF-8" language = "java"%> <html> <head> <title> Confirmation Page </ title> </ head> <body> Thank you for your purchase! </ body> </ html>
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; } }
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"; } }
@RequestMapping (value = "/ confirmation", method = RequestMethod.GET) public String confirmation (SessionStatus status) { status.setComplete (); return "booking / confirmation"; }
Source: https://habr.com/ru/post/199048/
All Articles