Spring MVC: Handling Controller Input For Request Query Parameter

Concept Overview

Please see this tutorial for an introduction to Spring MVC.

In This tutorial we present an example on how to retrieve the parameters from a request in the Spring MVC framework. We use annotations to declare a Controller and then in the method that handles our request we declare a variable to be a @RequestParam. This variable is then mapped from the request using the name provided in RequestParam. Look at this tutorial to see the different types of parameters that the Handler method can take.

Sample Program Overview

The sample program in this tutorial uses a variable passed in the request to retrieve a list of Person objects

Required Libraries
  • commons-beanutils.jar
  • commons-collections.jar
  • commons-digester.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
  • springmvc.jar



Source Code

Create the Person class which models a Person with his name and age. This represents the model in MVC pattern.

Create the PersonService class with a method to fetch a list of all the persons for a given age (see lines 19-27 below). PersonService class will be used by PersonController (described later).

Create the PersonController class. This class is the Controller in MVC pattern and is declared as such by using the
@Controller
annotation (see line 13 below).

When the request URL contains
/persons
(e.g. http://localhost:8080/springmvccontrollerinput/persons) then Spring MVC passes the control to
showPersonListForGivenAge
method (see lines 28-32 below) because of the
@RequestMapping
annotation (see line 27 below).

Also note that if the URL contains a request parameter
age=10
(e.g. http://localhost:8080/springmvccontrollerinput/persons?age=10), then this parameter can be accessed within the Controller by using
@RequestParam
(see line 28 below). This demonstrates the usage of handling request parameter input within controller.

PersonController class delegates the call to
PersonService
class to fetch all the persons with the given age and binds it to the model name
persons
(see lines 29-30 below) as a request attribute. This model name will be used in the view JSP (described later).

PersonController.showPersonListForGivenAge() method returns
person_list
which is the name of the view JSP (described later).

Create the person_list.jsp as shown below. 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.showPersonListForGivenAge() method with the same model name.

Iterate the list of 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 such that the prefix
/views
and the suffix
.jsp
should be added to the name of the view JSP which is specified in PersonController.showPersonListForGivenAge() method (see lines 18-26 below).


Finally, we need to deploy this web program in a web container and access it using a browser to verify that it executes correctly.


Running Sample Program

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.

Download And Automatically Run Sample Program
  • Save the springmvccontrollerinput-installer.jar on your machine
  • Execute/Run the jar using Java Runtime Environment


  • (Alternatively you can go the folder containing the springmvccontrollerinput-installer.jar and execute the jar using
    java -jar springmvccontrollerinput-installer.jar
    command)

  • You will see a wizard as shown below. Enter the location of Java Development Kit (JDK) and Click ‘Next’ button.
  • You will see a wizard page as shown below
  • Enter the location of the directory where you want the program to install and run (say, C:\Temp)
  • The installer will copy the program on your machine and automatically start the inbuilt webserver on your machine as shown below.
  • Click on the URL http://localhost:8080/springmvccontrollerinput/persons?age=10 The expected output indicating that the program has run successfully on your machine is shown in the image below.
  • Browsing the Program

    This source code for this program is downloaded in the folder specified by you (say, C:\Temp) as an eclipse project called
    springmvccontrollerinput
    . 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.

    Leave a Comment