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

Steps:

  • 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) {
		super();
		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;

@Controller
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"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

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

	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix">
			<value>\WEB-INF\jsp\</value>
		</property>
		<property name="suffix">
			<value>.jsp</value>
		</property>
	</bean>

</beans>

 

  • 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">
	<servlet>
		<servlet-name>dispatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>dispatcher</servlet-name>
		<url-pattern>*.web</url-pattern>
	</servlet-mapping>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<welcome-file-list>	 
	   <welcome-file>helloWorld.web</welcome-file>
	</welcome-file-list>

</web-app>

 

  • 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"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

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

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

</body>
</html>

 

  • 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 *