Spring 3.2 MVC Form Handling

In this example, you will learn about handling forms in Spring 3.2 MVC.

Spring 3.2 MVC Form Handling

Spring 3.2 MVC Form Handling

In this example, you will learn about handling forms in Spring 3.2 MVC.

In the given below example, a login page is given in which you need to enter following correct information :

  • User Name (Admin)
  • Password (root)

The project structure is given below :

Given below the application flow followed by the code used in the application :

When you execute the application, the first page which will appear to you is :

If fill incorrect information, it will redirect you on a error page :

If login is correct, it will show you the following code :

Codes :

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" 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">
<display-name>Spring3.2Form Handling</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<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>*.html</url-pattern>
</servlet-mapping>

</web-app>

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: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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

<context:component-scan base-package="net.roseindia.controller" />

<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>

<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/messages" />
</bean>

</beans>

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>Insert title here</title>
</head>
<body>
<jsp:forward page="loginForm.html"></jsp:forward>
</body>
</html>

FormController.java

package net.roseindia.controller;

import java.util.Map;

import net.roseindia.model.LoginForm;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class FormController {
@RequestMapping("/loginForm.html")
public String showForm(Map<String, LoginForm> model) {
LoginForm loginForm = new LoginForm();
model.put("loginForm", loginForm);
return "loginform";
}

@RequestMapping("/login.html")
public String processForm(LoginForm loginForm, BindingResult result,
Map<String, LoginForm> model) {
String userName = "Admin";
String password = "root";
if (result.hasErrors()) {
return "loginform";
}
loginForm = (LoginForm) model.get("loginForm");

if (!loginForm.getUserName().equals(userName)
|| !loginForm.getPassword().equals(password)) {
return "loginerror";
}

model.put("loginForm", loginForm);
return "loginsuccess";
}

}

LoginForm.java

package net.roseindia.model;

import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.NotEmpty;

public class LoginForm {
@NotEmpty
@Size(min = 1, max = 50)
private String userName;

@NotEmpty
@Size(min = 1, max = 20)
private String password;

public void setUserName(String userName) {
this.userName = userName;
}

public String getUserName() {
return userName;
}

public void setPassword(String password) {
this.password = password;
}

public String getPassword() {
return password;
}

}

loginform.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

<!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>Spring3Example</title>
</head>
<body>
<h3>Login Form</h3>
<font color="blue">
<h5>User Name="Admin" and Password="root"</h5>
</font>
<form:form action="login.html" commandName="loginForm">
<table>
<tr>
<td>User Name:<FONT color="red"><form:errors
path="userName" /></FONT></td>
</tr>

<tr>
<td><form:input path="userName" /></td>
</tr>

<tr>
<td>Password:<FONT color="red"><form:errors
path="password" /></FONT></td>
</tr>

<tr>
<td><form:password path="password" /></td>
</tr>

<tr>
<td><input type="submit" value="Submit" /></td>
</tr>

</table>
</form:form>
</body>
</html>

loginsuccess.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="core" uri="http://java.sun.com/jsp/jstl/core"%>

<!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 3.2 Form Handling Example</title>
</head>
<body>
<h3>Welcome <core:out value="${loginForm.userName}" /></h3>
<table>
<tr>
<td><a href="loginForm.html">Back</a></td>
</tr>
</table>
</body>

</html>

loginerror.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>Spring3Example</title>

</head>

<body>

<h3>Login Error !!! Click below to login again</h3>

<table>

<tr>

<td><a href="loginForm.html">Retry</a></td>

</tr>

</table>

</body>

</html>

Download Source Code