We have discussed earlier a simple example of Spring MVC in which we have created multiple view pages. Here, we are going to create another Spring MVC example in which we are going to read the HTML form data.

Following are the steps to create this example:

Directory Structure of Spring MVC example

Spring MVC Example – Reading HTML Form Data
  1. Create a web request page

In this step, we are going to create a web request page named index.jsp, which is providing three links to different view pages.

index.jsp

  • Create the Controller class

In this step, we are going to create the Controller class named MainController, which returns the JSP pages. We are going to map the view pages with the method using the @RequestMapping annotation.

MainController.java

  • Add the entry of the Controller into web.xml file.

In this step, we are going to add the entry of the Controller in the web.xml file.

web.xml

  • Add the entry of bean into another XML file.

In this step, we are going to define the bean inside an XML file (usually spring-servlet.xml). We are going to keep our JSP files in a folder named view, which is available inside the WEB-INF folder. The “/WEB-INF/view/” is used as the prefix, and “.jsp” is used as the suffix for the JSP files.

spring-servlet.xml

  • Create the JSP files

In this step, we are going to create other JSP files that are required in the application. We are going to create two JSP files, one is genform, and the other is formdata. The genform.jsp file contains an HTML form, and formdata.jsp will get the form data and display it.

genform.jsp

formdata.jsp

Output

Spring MVC Example – Reading HTML Form Data 1
Spring MVC Example – Reading HTML Form Data 2
Spring MVC Example – Reading HTML Form Data 3
Spring MVC Example – Reading HTML Form Data 4

Pin It on Pinterest

Share This