2:15 am - Wednesday April 26, 2017

Spring MVC form Validation with InternalResourceViewResolver Tutorial + Example

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

Spring MVC form Validation with InternalResourceViewResolver Tutorial + Example


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"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	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


http://www.springframework.org/schema/mvc


http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

">
	<!-- Enable annotation driven controllers, validation etc... -->
	<mvc:annotation-driven />
	<context:component-scan base-package="com.candidjava.controllers" />
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix">
			<value>/WEB-INF/jsp/</value>
		</property>
		<property name="suffix">
			<value>.jsp</value>
		</property>
	</bean>
	<bean id="messageSource"
		class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
		<property name="basename" value="/WEB-INF/messages" />
	</bean>

</beans>

&nbsp;

ValidationController.java

package com.candidjava.controllers;

import com.candidjava.form.ValidationForm;

import java.util.Map;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/validationform.html")
public class ValidationController {

	// Display the form on the get request
	@RequestMapping(method = RequestMethod.GET)
	public String showValidatinForm(Map model) {
		ValidationForm validationForm = new ValidationForm();
		model.put("validationForm", validationForm);
		return "validationform";
	}

	// Process the form.
	@RequestMapping(method = RequestMethod.POST)
	public String processValidatinForm(@Valid ValidationForm validationForm,
			BindingResult result, Map model) {
		if (result.hasErrors()) {
			return "validationform";
		}
		// Add the saved validationForm to the model
		model.put("validationForm", validationForm);
		return "validationsuccess";
	}

}
&nbsp;

ValidationForm.java

package com.candidjava.form;

import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.format.annotation.NumberFormat;
import org.springframework.format.annotation.NumberFormat.Style;

public class ValidationForm {
	@NotEmpty
	private String userName;
	@NotNull
	@NumberFormat(style = Style.NUMBER)
	private Integer age;
	@NotEmpty
	private String password;

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

	public String getUserName() {
		return userName;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public Integer getAge() {
		return age;
	}

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

	public String getPassword() {
		return password;
	}
}

validationform.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>Insert title here</title>
</head>
<body>
<form:form method="post" action="validationform.html"
	commandName="validationForm">
	<h4 align="center"><u>Login Page</u></h4>
	<table align="center">
		<tr>
			<td>User Name:</td>
			<td><form:input path="userName" /><font color="red"> <form:errors
				path="userName" /></font></td>
		</tr>
		<tr>
			<td>Age:</td>
					<td><form:input path="age" /><font color="red"> <form:errors path="age" /></font></td>
		</tr>
		<tr>
			<td>Password:</td>

			<td><form:password path="password" /><font color="red"> <form:errors
				path="password" /></font></td>
		</tr>
		<tr>
		<td></td>
			<td><input type="submit" value="Submit" /></td>
		</tr>
	</table>
</form:form>
</body>

</html>

validationsuccess.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>Insert title here</title>
</head>
<body>
<font >
<h2 align="center">Welcome <core:out value="${validationForm.userName}" /></h2>
</font>
<h3 align="center">You have successfully Logged in. </h3>
<p align="center"><i>Your information is  :</i></p>
<p align="center"><b>UserName :</b><core:out value="${validationForm.userName}" /></p>
<p align="center"><b>Age :</b><core:out value="${validationForm.age}" /></p>
<p align="center"><b>password :</b><core:out value="${validationForm.password}" /></p>

</body>
</html>

messages.properties

NotEmpty.validationForm.userName=User Name is required.
NotEmpty.validationForm.password=Password is required.

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 3 Login Validation</title>
</head>
<body >
<h4 align="center"><u>Spring MVC Login Validation</u></h4>
<ul>
	<li><a href="forms/validationform.html">Validation Form</a></li>
</ul>
</body>
</html>

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/j2ee" xmlns:javaee="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/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd" version="2.5">
  <javaee:display-name>Spring3LoginValidation</javaee: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>/forms/*</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

Output screenshots

  Download SpringLoginValidation  + Spring_mvc_jar

Filed in: Z-A Spring Codes

Comments are closed.