Spring MVC Form Handling Example - Spring MVC Framework

What is Spring MVC Form Handling Example?

The below example explains the readers on how to write a web based application with the use of HTML forms with Spring Web MVC framework. Firstly, a working Eclipse IDE should be in place and follow the steps below to create a Dynamic Form based Web Application.

Step

Description

1

Create a project with a nameHelloWebunder a packagecom.wisdomjobsas explained in theSpring MVC - Hello World Examplechapter.

2

Create a Java classesStudent,StudentControllerunder thecom.wisdomjobspackage.

3

Create a view filesstudent.jsp,result.jspunderjspsub-folder.

4

The final step is to create the content of all the source and configuration files and export the application as explained below.

Student.java


StudentController.java

In the above example, we are creating a service method called student(). We are passing a blank Student object in ModelAndView object. In this object, command name is specified because the framework expects an object with “command” name if we are using <form:form> tags in JSP file. Student.jsp view is returned if student() method is called.

addStudent() is the next method which is called against a POST method on HelloWeb/addStudent URL. Model object is prepared based on the submitted information. A “result” view is returned from service method at the end which results in showing result.jsp.

student.jsp

result.jsp

Once the source and configuration files are created export the application. Do right click on the application and Export> WAR File option and save HelloWeb.war file in Tomcat’s webapps folder.

Start the Tomcat Server and using a standard browser check if you are having access to other web pages from webapps folder. Try the URL http://localhost:8080/HelloWeb/index and you must see the below result if everything is fine with Spring Web Application:

Spring Student Form

spring_student_form

Submit the form once the required information is filled. You must see the below result if everything is fine with Spring Web Application:

Spring Student Form Result

spring_student_form_result

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Spring MVC Framework Topics