top of page

Spring MVC : Handle an HTML form using @RequestParam annotation and using @ModelAttribute annotation


In this article we are going to explain how to handle an HTML form using @RequestParam and @ModelAttribute. We also going to show you other uses of @ModelAttribute.


Technologies in this article :

  1. Maven 4.0.0

  2. JDK 1.8

  3. Spring 4.2.5.RELEASE.

1. @RequestParam Annotation :

This annotation is used at method argument level.It is used to catch request parameters value.

Steps for using @RequestParam are as follows :


Step 1.

Create a Html form for accepting user information.


File : formRequest.jsp

In this form we created four input fields with names like sName, sAge, sDob and sHobbies.

Form action is "formRequest" and method is "post".

Step 2.

Now create controller method to render this form.

In this controller method mapping is done for URL "formRequest" and default request method is "get".

Step 3.

Now create controller method to process the form.

In this controller method mapping is done for URL "formRequest" and default request method is "post".

As we can see that we catch all the request parameter with corresponding name using @RequestParam annotation and added all the request parameter in model object to view these values on jsp page.

So if we have 10 request parameter than we have to write 10 times @RequestParam.


Step 4.

Create Html page to view request parameter values.

File : detailsRequest.jsp

On this page we rendered all the model object using EL syntax.

Disadvantage -

When we have more number of request parameters than we have to write @RequestParam at that many times. It makes code lengthy and repetitive.


2. @ModelAttribute Annotation :


This annotation can be used in two ways one at method level and other one at method argument level.

For now we will discuss @ModelAttribute at method argument level. By using this annotation we can solve the problem which is arises in the case of @RequestParam annotation, when we have more number of request parameters.


Steps for using @ModelAttribute are as follows :


Step 1.

For this approach first you have to create a simple pojo class with all the fields which we are accepting from user to enter in form.


File : studentModel.jsp


As we can see we created a class encapsulated all the fields required in form with name studentName, studentAge, studentDob and studentHobbies.


Step 2.

Create a Html form for accepting user information corresponding to studentModel class.


File : formModel.jsp



In this form we created four input fields with names like studentName, studentAge, studentDob and studentHobbies.

Form action is "formModel" and method is "post".

Note* : The names for input field should be the same as corresponding studentModel class field name.


Step 3.

Now create controller method to render this form.


In this controller method mapping is done for URL "formModel" and default request method is "get".

Step 4.

Now create controller method to process the form.

In this controller method mapping is done for URL "formModel" and default request method is "post".

As we can see that we used @ModelAttribute annotation with Student class at method argument. @ModelAttribute annotation binds corresponding request parameters to its corresponding fields in Student class object using corresponding getter methods of student class.

So we can see if we have more number of request parameters than we have to encapsulate it in a class and use @ModelAttribute with that field only one time and we only need to add only this class object in model attribute to retrieve all the values of request parameters on jsp page.


Step 5.

Create Html page to view request parameter values.

To render request parameters value on jsp page use syntax like

${ [modelObject].[corresponding field name in model class] }.




Summary : We can see that by using @RequestParam and @ModelAttribute we can get request parameters. But if we have more number of request parameters than there is lengthy and repetitive code in @RequestParam approach and if we have less number of request parameter than we have to do more work in @ModelAttribute approch comparative to @RequestParam approach.

So if we have more number of request parameter than use @Modelttribute and if we have less number of request parameter than use @RequestParam.


3. @ModelAttribute Annotation (Method level) :

As we can see that in all our jsp pages we used ${schoolName} to render school name on all jsp pages. For this in all controller we have to add model attribute with name "schoolname" and corresponding value in each controller method.

To solve this problem spring provide a solution.

If we have a scenario in which we have common model attributes in all controller than for this scenario create a method and add all common model attribute in model object and annotate this method with @ModelAttribute. By this all the objects added in this method to model object will provided to all controller method.

Syntax -

In this model attribute with name "schoolName" will be provided to all controllers by defining like above.

Find Source Code @GitHub.

In next post we will explain Data Binding with a User-Defined Type, BindingResult (concept).

For updates of our next post please subscribe below and don't forget to like. In case of any queries write in comment section.

Featured Posts
Recent Posts
Archive
Related Posts
Follow Us
  • Facebook Basic Square
  • Twitter Basic Square
  • Google+ Basic Square
bottom of page