How to send form fields from jsp to server in Spring MVC / How to submit a form to Spring Controller

Suppose we have a jsp page which has a form whose data should be sent to the server. The form has fields which are directly related to the fields of an entity on the server. An entity is a simple java class which has some fields and their getter and setter methods.
Let’s say the entity is a User (which means we have a User class on the server and each field of the jsp form is linked to a field of User class. When we say linked, it simply means that the name attribute of the form element is the same as the name of the corresponding entity field.
We just enter the values and save the form and all the form fields should be sent to the server. The jsp page would look something like:

User Registration Form

This comprises of a simple html form tag along with various input boxes. The code would look something like:

Personal Information
Contact Details

Note : Formatting and css classes have been removed from this code for keeping it simple.

Create an entity class (User in this case) on the server with the required fields. Let’s create a User class which will look something like:

    public class User {
	private String name;
	
	private int age;

	/*Getters and Setters*/
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
   }

Only two fields have been shown here. Add other fields such as address, email etc. as per your needs.
Now create a method in the Controller class as below:

  @RequestMapping(value = "/adduser",method=RequestMethod.POST)
  public void saveUser(User user){
     //add user handling logic here
  }

When you will press the submit button on jsp, the control will be transferred to this method. The fields of user object will automatically be populated with the values entered on the jsp. Now you can write user handling logic here. You can save it to database, write details to a file or simply show them on another jsp page in non-editable form.

Let’s tweak in :

1. The name attribute of input boxes on jsp should match with the fields of entity on the server otherwise the field values will not be populated when request will be sent from jsp to controller.
2. The value given in @RequestMapping annotation should match the jsp’s form action attribute otherwise this controller method will not be called and nothing will happen on submit button press.
3. The value of method attribute in @RequestMapping annotation should exactly match the jsp’s form tag method attribute otherwise the request handler method will never be invoked.
4. The type of button at the end of the form should be “submit” otherwise the form will not be submitted when the button will be pressed.

You are here. Great!!! Means you enjoyed the post till this point. Mark your impression by providing feedback / comment in the space below. It will be an honor.

Leave a Reply