Concept Overview
Please see this tutorial for an introduction to Spring MVC. In this tutorial we see how Spring can be used for presenting and submitting forms. The example in this tutorial does three things 1. It presents a form to add a new ‘Person’. 2. It validates the form 3. It creates the new Person object from the values entered in the form and displays the added person. A single Controller is used to handle all the above functionalities. The Handler has a method that handles the GET request (showing the form to add a new person) and another method that handles the POST request (when the user submits the form). The parameters in the form are bound to the fields of the person object which is passed in the handler method. Note the @valid annotation. This performs validation for the form fields. The validation rules are specified in the Person bean.
- commons-beanutils.jar
- commons-collections.jar
- commons-digester.jar
- hibernate-validator-4.2.0.Final.jar
- jstl-1.2.jar
- org.springframework.aop.jar
- org.springframework.asm.jar
- org.springframework.beans.jar
- org.springframework.context.jar
- org.springframework.context.support.jar
- org.springframework.core.jar
- org.springframework.expression.jar
- org.springframework.web.jar
- org.springframework.web.servlet.jar
- retrotranslator-runtime-1.2.3.jar
- servlet-api-2.5.jar
- slf4j-api-1.6.1.jar
- springmvc.jar
- springmvcformprocessing.jar
- validation-api-1.0.0.GA.jar
Create the Person class which models a Person with his name and age. This represents the model in MVC pattern.
Note that annotation based validation of form input for name and age members is achieved using @Size, @Min and @NotNull annotations (see lines 8-11 below).
The validator ensures that the name of the person should have a minumum of 3 and maximum of 30 characters and that the age should not be empty and should have a value greater than 0. This demonstrates the implementation of form validation within Spring MVC.
Create the PersonService class with a method to fetch a list of all the persons (see lines 19-21 below) and also to add a new Person (see lines 23-25 below). PersonService class will be used by PersonController (described later) to add a new Person and also fetch the list of all persons.
Create the PersonController class. This class is the Controller in MVC pattern and is declared as such by using the
@Controller
annotation (see line 17 below).
When a request URL ends with
/allpersons
then Spring MVC passes the control to
showPersonListPage
method (see lines 32-35 below) by using the
@RequestMapping
annotation (see line 31 below).
PersonController class delegates the call to
PersonService
class to fetch all the persons and binds it to the model name
persons
(see line 33 below) as a request attribute. This model name will be used in the view JSP (described later).
PersonController.showPersonListPage() method returns
person_list
which is the name of the view JSP (see line 34 below).
Similarly, when a request URL ends with
/person?new
then Spring MVC passes the control to
showNewPersonForm
method (see lines 38-41 below) by using the
@RequestMapping
annotation (see line 37 below) and
@RequestParam
annotation (see line 38 below). (Please see related trail
Spring MVC Control Input
for further details on accessing request parameters within Controller)
PersonController class creates an instance of Person and binds it to to the model name ‘person’ and returns
new_person
which is the name of the view JSP (see line 40 below).
Also, when a request URL ends with
/person/add
then Spring MVC passes the control to
addPersonFromForm
method (see lines 43-50 below).
If the form has validation errors then the control is redirected to the
new_person
view (see lines 45-47 below).
If the form does not have validation erros then PersonController class delegates the call to
PersonService.addPerson()
method to create a new person (see line 48 below) and redirects to
/addPersons
(see line 49 below) to display the new added Person in the list.
Create the new_person.jsp as shown below which allows the user to add a new Person. This represents the view in MVC pattern.
Create and HTML form to take name and age of the person as inputs (see lines 9-24 below).
When the ‘Submit’ button is pressed, then call the ‘/springmvcformprocessing/person/add’ URL to create a new Person (see line 7 below).
Create the person_list.jsp as shown below which displays the list of all Persons. This represents the view in MVC pattern.
Get the list of persons by using the name of the model
persons
(see line 17 below). Note that this model was set in PersonController.showPersonListPage() method with the same model name.
Iterate the list persons and display the list in a table (see lines 18-23 below).
Create the web.xml configuration as shown below.
Create a servlet mapping with name
person
(see lines 13-16 below) and define the corresponding servlet using Spring
DispatcherServlet
class (see lines 7-11 below).
Note that Spring automatically will look for a configuration file called
person-servlet.xml
corresponding to the servlet mapping name person. We shall describe the details of
person-servlet.xml
later.
Create the Spring configuration by the name
person-servlet.xml
as shown below.
Configure Spring such that annotation based wiring will be used (see lines 14-15 below).
Configure Spring default validator which will perform validations (see line 17 below) defined in Person class
Configure Spring such that the prefix
/views
and the suffix
.jsp
should be added to the name of the view JSPs specified in various methods in PersonController (see lines 19-27 below).
This sample program has been packaged as a jar installer which will copy the source code (along with all necessary dependencies)on your machine and automatically run the program for you as shown in the steps below. As this sample program contains Java Server Pages (JSPs), you will need Java Development Kit (JDK preferably 1.5 or higher) on your machine so that the JSPs can be complied locally. Note that no other setup is required on your machine! Also please ensure that the port 8080 is not being used by any other program on your machine.
(Alternatively you can go the folder containing the springmvcformprocessing-installer.jar and execute the jar using
java -jar springmvcformprocessing-installer.jar
command)
This source code for this program is downloaded in the folder specified by you (say, C:\Temp) as an eclipse project called
springmvcformprocessing
. All the required libraries have also been downloaded and placed in the same location. You can open this project from Eclipe IDE and directly browse the source code. See below for details of the project structure.