Spring MVC Form Handling Example
we will learn Spring MVC Form handling without using database. Here, we will use @Controler, @RequestMapping and @ModelAttribute annotations.
To display the input form, we are going to use <form:form> tag of spring framework. Let’s see a simple example to store form data in a model object and display data of a list.
Following is the content of Student.java file.
Student.java
package com.codeNuclear;
public class Student
{
private String firstname;
private String lastname;
private int age;
private int id;
public Student()
{}
public Student(String firstname,String lastname,int age,int id)
{
super();
this.firstname = firstname;
this.lastname = lastname;
this.id = id;
this.age = age;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
Following is the content of StudentController.java file.
StudentController.java
package com.codeNuclear;
import java.util.ArrayList;
import java.util.List;
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;
@Controller
public class StudentController
{
@RequestMapping("/studentform")
public ModelAndView showform()
{
return new ModelAndView("studentform","command",new Student());
}
@RequestMapping(value="/save",method = RequestMethod.POST)
public ModelAndView save(@ModelAttribute("stu")Student stu,ModelMap model)
{
model.addAttribute("firstname",stu.getFirstname());
model.addAttribute("lastname",stu.getLastname());
model.addAttribute("age",stu.getAge());
model.addAttribute("id",stu.getId());
System.out.println("First Name:"+stu.getFirstname()+"\n"+"Last Name:"+stu.getLastname()+"\n"+"Age:"+stu.getAge()+"\n"+"ID:"+stu.getId());
return new ModelAndView("redirect:/viewstudent");
}
@RequestMapping("/viewstudent")
public ModelAndView viewstudent()
{
List<Student> list=new ArrayList<Student>();
list.add(new Student("Alger","David",17,108));
list.add(new Student("Kevin","Paul",19,109));
list.add(new Student("Edward","Joseph",17,110));
list.add(new Student("Smith","John",18,111));
return new ModelAndView("viewstudent","list",list);
}
}
Here the first service method showform(), we have passed a blank Student object in the ModelAndView object with the name “command” because the Spring framework expects an object with the name “command” if you are using tags in your JSP file. So, when showform() method is called , it returns studentform.jsp view.
The second service method save() will be called against a POST method on the MVCFormExample/studentform URL. You will prepare your model object based on the submitted information. Finally a result view will be returned from the service method viewstudent(), which will result in rendering viewstudent.jsp.
Following is the content of Spring Web configuration file web.xml.
web.xml
<?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" 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">
<display-name>MVCFormExample</display-name>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/</welcome-file>
</welcome-file-list>
</web-app>
Following is the content of another Spring Web configuration file spring-servlet.xml
spring-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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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.codeNuclear"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
Following is the content of index.jsp file.
index.jsp
<%@ 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>Spring MVC Form</title>
</head>
<body>
<a href="studentform">Add Student</a>
<a href="viewstudent">View Students</a>
</body>
</html>
Following is the content of studentform.jsp file.
studentform.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>Add Student</title>
</head>
<body>
<h2>Add Student</h2>
<form:form method="post" action="save">
<table >
<tr>
<td>First Name : </td>
<td><form:input path="firstname" /></td>
</tr>
<tr>
<td>Last Name : </td>
<td><form:input path="lastname" /></td>
</tr>
<tr>
<td>Age :</td>
<td><form:input path="age" /></td>
</tr>
<tr>
<td>ID :</td>
<td><form:input path="id" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Save" /></td>
</tr>
</table>
</form:form>
</body>
</html>
Following is the content of viewstudent.jsp file.
viewstudent.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>View Students</title>
</head>
<body>
<h2>View Students Information</h2>
<table border="2" width="20%" cellpadding="2">
<tr><th>First Name</th><th>Last Name</th><th>Age</th><th>ID</th></tr>
<c:forEach var="stu" items="${list}">
<tr>
<td>${stu.firstname}</td>
<td>${stu.lastname}</td>
<td>${stu.age}</td>
<td>${stu.id}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
Output:


