Spring MVC Validation Example

StudentController.java

In StudentController class, we have added @Controller annotation which indicates that an annotated class is a "Controller" (e.g. a web controller)

@RequestMapping annotation is used for mapping web requests to corresponding handler classes and handler methods

@ModelAttribute annotation is used for backing up the model object and persisted across requests

package com.student.controller;

import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
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.student.bean.Student;

@Controller
public class StudentController {

   
@RequestMapping(value = "/student", method = RequestMethod.GET)
   
public ModelAndView student() {
       
return new ModelAndView("studentForm", "student", new Student());
   
}

   
@RequestMapping(value = "/addStudent", method = RequestMethod.POST)
   
public String addStudent(@ModelAttribute("student") @Valid Student student, BindingResult result, ModelMap model) {
       
if(result.hasErrors()) {
           
return "studentForm";
       
}
       
model.addAttribute("name", student.getName());
        model.addAttribute
("age", student.getAge());
        model.addAttribute
("studId", student.getStudId());
       
return "studentDetail";
   
}
}

spring-servlet.xml

The name for spring-servlet.xml is must match the servlet-name we have provided on web.xml. In our case we have provided servlet-name as spring, so we need to append "-servlet.xml"

Below code is used for Spring auto detection feature to find the packages where the controller is defined

<context:component-scan base-package="com.student" />
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

	<mvc:annotation-driven />
	<context:component-scan base-package="com.student.controller" />

	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		id="viewResolver">
		<property name="prefix" value="/WEB-INF/views/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	
	<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
		<property name="basename" value="/WEB-INF/messages" />
   </bean>

</beans>

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_3_0.xsd"
	id="WebApp_ID" version="3.0">

	<display-name>SpringMVCExample</display-name>
	<welcome-file-list>
		<welcome-file>/</welcome-file>
	</welcome-file-list>

	<servlet>
		<servlet-name>spring</servlet-name>
		<servlet-class>
			org.springframework.web.servlet.DispatcherServlet
		</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>spring</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

messages.properties

NotNull.student.studId=Student ID must not be blank.
Size.student.name=Student Name must be between 5 to 20 characters.
NotNull.student.age=Student Age must be Atleast 5 year old.
Run

Spring MVC Validation Example

Spring MVC Validation Example

Spring MVC Validation Example

Spring MVC Validation Example





Previous 1 2








Your email address will not be published. Required fields are marked *