Spring MVC Form Handling Example

By Arvind Rai, August 25, 2013
Spring MVC has provided tag library to create Form. The Form will be associated with a bean. When we submit the form, the associated bean will automatically be populated. The bean will be used for further processing. The component required for form handling has been discussed step by step. First of all find the controller.

Create Spring MVC Controller

UserController.java
package com.concretepage.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
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.concretepage.bean.User;
@Controller
public class UserController {
	@RequestMapping(value="user", method = RequestMethod.GET)
	public ModelAndView user(){
		return new ModelAndView("user","command",new User());
	}
	@RequestMapping(value="createUser", method = RequestMethod.POST)
	public String createUser(@ModelAttribute("user") User user,ModelMap model) {
		model.addAttribute("name",user.getName());
		model.addAttribute("age",user.getAge());
		model.addAttribute("location",user.getLocation());
		return "redirect:pages/success.jsp";
	}
} 

ModelAndView in Spring MVC

org.springframework.web.servlet.ModelAndView plays both model and view. It holds data for both. ModelAndView has different constructors. The one we have used is
public ModelAndView(String viewName, String modelName,Object modelObject) 
viewName: the page which we are looking for.
modelname: any name which represents model
modelObject: the bean which will be associated to Form

ModelAttribute in Spring MVC

org.springframework.web.bind.annotation.ModelAttribute is used as annotation for the method or method arguments. ModelAttribute binds a named model attribute to the method or any arguments. ModelAttribute must be defined by name. In our example ModelAttribute("user") has been defined by "user".

ModelMap in Spring MVC

org.springframework.ui.ModelMap is used when working with UI tools. ModelMap is the implementation of Map. ModelMap carries the data to view where value can be found by request.

Create Spring MVC Form

Our jsp should not have generic html tag. The page must be created by Spring tags.
user.jsp
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head><title>Create User</title></head>
<body>
  <form:form action="createUser" method="post">
   Enter User Name:<form:input  path="name"/> <br/>
   Enter age :<form:input path="age"/><br/>
   Enter location :<form:input path="location"/><br/>
   <input type="submit">
   
  </form:form>
</body>
</html> 
success.jap
Name : ${param.name}<br/>
Age : ${param.age}<br/>
Location : ${param.location}<br/> 

Configure Controller and ViewResolver in Spring MVC

We need to provide suffix and prefix for the URL rendering. And define the base package for the controller.
dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans  
   http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context.xsd">
   <context:component-scan base-package="com.concretepage.controller" />
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	  <property name="prefix" value="/pages/"/>
	  <property name="suffix" value=".jsp"/> 
   </bean>
</beans> 

Configure web.xml for Spring MVC

web.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">
	<display-name>Spring MVC Application</display-name>
	<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>/</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>
</web-app> 

Run the application

Find the package structure, user form and success user creation message. Access URL as http://<ip>:<port>/spring-mvc-1/user
Spring MVC Form Handling Example

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us