How to do it...

To implement form handling using Spring 5, perform the following steps:

  1. Let us first implement the model object that will contain all request data during form transactions. The form_page handles all the HTML components that will receive all request parameters from the client. To organize these numerous parameters during the request dispatch, it will be ideal if we create a form model or form backing object to persist all this data. This strategy can avoid a convoluted declaration of request parameters at the @Controller level. So, before creating the physical view and the @Controller, write this POJO first, containing all the data and its properties:
public class EmployeeForm { 
  
  private String firstName; 
  private String lastName; 
  private String position; 
 
   // getters and setters 
} 
  1. Then, create a @Controller named FormController inside the org.packt.dissect.mvc.controller package. A form controller is similar to a simple one because of its use of class-level @RequestMapping configuration for the URL. The only difference is that a form controller must only contain two methods: the GET handler, to serve the form views, and the POST handler to perform the operations after the form submission. Both of these handlers must not have URL mapping. The following is our FormController:
@Controller 
@RequestMapping("/employee_form.html") 
public class FormController { 
  
  @RequestMapping(method=RequestMethod.GET) 
  public String initForm(Model model){ 
  EmployeeForm employeeForm = new EmployeeForm(); 
    model.addAttribute("employeeForm", employeeForm); 
    return "form_page"; 
  } 
  
  @RequestMapping(method=RequestMethod.POST) 
  public String submitForm(Model model, 
    @ModelAttribute("employeeForm") EmployeeForm  
      employeeForm){ 
    model.addAttribute("employeeForm", employeeForm); 
    return "success_page"; 
  } 
} 

The initForm() loads the form_page view and initializes the EmployeeForm form backing object for the form's modelAttribute mapping. The submitForm() is executed after the form submission receives the form backing object that is persisted with the form data. Moreover, this method is implemented to pass the unaltered form object as a request attribute to the success_page view using the Model interface.

  1. Next, create the form_page as src/main/webapp/page/employee_form.jsp, which will contain the binding of the EmployeeForm form model to the HTML form components. This binding mechanism requires the use of the Spring Form tag library, again declared at the directive level of our JSP page together with the core Spring tag library for static texts and labels, and JSTL for common JSP supplementary support:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
<%@ taglib prefix="spring" 
uri="http://www.springframework.org/tags" %> 
<%@ taglib prefix="form" 
uri="http://www.springframework.org/tags/form"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
  "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<title><spring:message code="employee_form" /></title> 
</head> 
<body> 
   <h1><spring:message code="employee_form" /></h1>   
   <form:form  modelAttribute="employeeForm" method="post"> 
      <spring:message code="fnameLbl" /> 
            <form:input path="firstName"/><br/> 
      <spring:message code="lnameLbl" /> 
          <form:input path="lastName"/><br/> 
      <spring:message code="posLbl" /> 
          <form:input path="position"/><br/> 
      <input type="submit" value="Add Employee"/> 
   </form:form> 
</body> 
</html> 

To use the Spring Form tag library, we use the prefix form to access all its data binding-aware tags, to be used for accessing the setters of each component of the model attribute EmployeeForm. The first tag, <form:form>, renders the HTML <form> and enables the binding of request parameters to its inner tags. The <form:input> tag renders the HTML <input> tag with access to the model attributes with a default empty value. The <spring:message> just accesses the labels and header titles from the message bundle.

  1. The last view to create is the success_pagesrc/main/webapp/page/employee_profile.jsp, which contains the rendition of all the properties of the modelAttribute using the JSP's expression language and JSTL:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
<%@ taglib prefix="spring" 
uri="http://www.springframework.org/tags" %> 
<%@ taglib prefix="form" 
uri="http://www.springframework.org/tags/form"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
  "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<title><spring:message code="employee_profile" /></title> 
</head> 
<body> 
  <h1><spring:message code="employee_profile" /></h1> 
  <table> 
      <tr> 
         <th>First Name</th> 
         <th>Last Name</th> 
         <th>Position</th> 
      </tr> 
       <tr> 
        <td><c:out value='${ employeeForm.firstName }'/></td> 
        <td><c:out value='${ employeeForm.lastName }' /></td> 
        <td><c:out value='${ employeeForm.position }' /></td> 
      </tr> 
  </table> 
</body> 
</html>
  1. Update the views.properties by adding the following mappings:
form_page.(class)=org.springframework.web.servlet.view.JstlView 
form_page.url=/page/employee_form.jsp 
 
success_page.(class)=org.springframework.web.servlet.view.JstlView 
success_page.url=/page/employee_profile.jsp 
  1. Also, update the message bundle to have proper header titles and labels.
  2. Save all files. clean, install, and deploy the project. Open a browser and run https://localhost:8443/ch03/employee_form.html:
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.218.45