Spring MVC submit Form

Spring MVC submit Form

Below tutorial will show you how to submit form to controller using Spring MVC submit Form. Whenever you start working on any project this would be the first step and it’s easy if you know how to do it but probably you will be stuck and spend more time to understand framework if you are new. To keep you on track without waste time please follow below steps:

Below are needed

  • Eclipse 3.0 or above (Download eclipse from http://www.eclipse.org/downloads/ site)
  • JDK 1.6 or above (Download from here: http://www.oracle.com/technetwork/java/javase/downloads/index.html)
  • Tomcat 6 or above (Please follow link to install and configure tomcat in eclipse:Configure and Run Tomcat server in eclipse
  • Spring jars which is already included in project for download in the bottom


  • Create dynamic web project in eclipse name: SpringMVCProject (Please use this link if you are not familiar how to create dynamic project in eclipse: Create Dynamic Web Project Eclipse)
  • Create package com.javahorn.beans inside src folder and create classes inside it with same name from below:

package com.javahorn.beans;

public class Users {

	private String name;
	private String dob;
	private String email;
	private String phone;

	public Users(String name, String dob, String email, String phone,
			String address, Long pincode, String country) {
		this.name = name;
		this.dob = dob;
		this.email = email;
		this.phone = phone;

	public Users() {

	public String getName() {
		return name;

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

	public String getDob() {
		return dob;

	public void setDob(String dob) {
		this.dob = dob;

	public String getEmail() {
		return email;

	public void setEmail(String email) {
		this.email = email;

	public String getPhone() {
		return phone;

	public void setPhone(String phone) {
		this.phone = phone;



  • Create controller name TestController.java and copy paste below code:

package com.javahorn.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.javahorn.beans.Users;

public class TestController {

	@RequestMapping(value = "/helloWorld.web", method = RequestMethod.GET)
	public ModelAndView printWelcome(@ModelAttribute("user") Users user) {

		ModelAndView mav = new ModelAndView("SubmitForm");
		mav.addObject("message", "Hello World!!!");
		return mav;


	@RequestMapping(value = "/submitForm.web", method = RequestMethod.POST)
	public ModelAndView submitForm(@ModelAttribute("user") Users user) {

		Users users=new Users();
		ModelAndView mav = new ModelAndView("SubmitForm");
		mav.addObject("users", users);
		return mav;




  • Create dispatcher-servlet.xml inside WEB-INF folder and copy paste below content

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"

	<context:component-scan base-package="com.javahorn.controller" />
	<context:component-scan base-package="com.javahorn.beans" />

		<property name="prefix">
		<property name="suffix">



  • Copy below web.xml content in your file

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">







  • Create folder name jsp inside WEB-INF folder and create jsp file name SubmitForm.jsp. Copy paste below jsp content in it

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>

<form:form commandName="user" action="submitForm.web" method="post">     

                <legend>User details</legend>
                        <label for=name>Name</label>
                        <form:input path="name"  type="text" placeholder="First and last name" />
                        <label for=name>Date</label>
                    <form:input path="dob" type="date" required="true" />
                        <label for=email>Email</label>
                        <form:input path="email" type="text" required="true" />
                        <label for=phone>Phone</label>
                        <form:input path="phone" type="text" required="true" />
                <button type=submit>Save User Details!</button> 



  • Finally after all set up you should see below project structure:

Spring MVC submit Form

  • That’s it. Now right click project –> Click Run As –> Run on Server as below:

Spring MVC Add Multiple Rows


  • You will see below. Enter data to test it. All data will be submitted to controller you put debug point or do system.out.println to see the data where it will return same jsp page with data entered in the form.

Spring MVC submit Form

  • That’s it.

download2 Download project here: SpringMVCProject

Leave a Reply

Your email address will not be published. Required fields are marked *

I am not Robot *