Spring 3 MVC and JSR303 @Valid example
Spring 3 MVC and JSR303 @Valid example
凯文加内特 发表于2年前
Spring 3 MVC and JSR303 @Valid example
  • 发表于 2年前
  • 阅读 117
  • 收藏 0
  • 点赞 0
  • 评论 0


In Spring 3, you can enable “mvc:annotation-driven” to support JSR303 bean validation via @Valid annotation, if any JSR 303 validator framework on the classpath.

Hibernate Validator is the reference implementation for JSR 303

In this tutorial, we show you how to integrate Hibernate validator with Spring MVC, via @Valid annotation, to perform bean validation in a HTML form.

Technologies used :

  1. Spring 3.0.5.RELEASE

  2. Hibernate Validator 4.2.0.Final

  3. JDK 1.6

  4. Eclipse 3.6

  5. Maven 3

1. Project Dependencies

Hibernate validator is available at JBoss public repository.

			<id>JBoss repository</id>


		<!-- Spring 3 dependencies -->



		<!-- Hibernate Validator -->


2. JSR303 Bean Validation

A simple POJO, annotated with Hibernate validator annotation.

Refer to this Hibernate validator documentation for detail explanation

package com.mkyong.common.model;import org.hibernate.validator.constraints.NotEmpty;import org.hibernate.validator.constraints.Range;public class Customer {

	@NotEmpty //make sure name is not empty
	String name;

	@Range(min = 1, max = 150) //age need between 1 and 150
	int age;

	//getter and setter methods}

3. Controller + @Valid

For validation to work, just annotate the “JSR annotated model object” via @Valid. That’s all, other stuff is just a normal Spring MVC form handling.

package com.mkyong.common.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.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import com.mkyong.common.model.Customer;@Controller
@RequestMapping("/customer")public class SignUpController {

	@RequestMapping(value = "/signup", method = RequestMethod.POST)
	public String addCustomer(@Valid Customer customer, BindingResult result) {

		if (result.hasErrors()) {
			return "SignUpForm";
		} else {
			return "Done";


	@RequestMapping(method = RequestMethod.GET)
	public String displayCustomerForm(ModelMap model) {

		model.addAttribute("customer", new Customer());
		return "SignUpForm";


4. Error Message

By default, if validation failed.

  1. @NotEmpty will display “may not be empty”

  2. @Range will display “must be between 1 and 150″

You can override it easily, create a properties with “key” and message. To know which @annotation bind to which key, just debug it and view value inside “BindingResult result“. Normally, the key is “@Annotation Name.object.fieldname“.

File : messages.properties

NotEmpty.customer.name = Name is required!Range.customer.age = Age value must be between 1 and 150

5. mvc:annotation-driven

Enable “mvc:annotation-driven” to make Spring MVC supports JSR303 validator via @Valid, and also bind your properties file.

<beans xmlns="http://www.springframework.org/schema/beans"

	<context:component-scan base-package="com.mkyong.common.controller" />

         <!-- support JSR303 annotation if JSR 303 validation present on classpath -->
	<mvc:annotation-driven />

	<bean id="viewResolver"
		<property name="prefix">
		<property name="suffix">
        <!-- bind your messages.properties -->
	<bean class="org.springframework.context.support.ResourceBundleMessageSource"
		<property name="basename" value="messages" />

6. JSP Pages

Last one, normal JSP page with Spring form tag library.

File : SignUpForm.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%><html><head><style>.error {
	color: #ff0000;}.errorblock {
	color: #000;
	background-color: #ffEEEE;
	border: 3px solid #ff0000;
	padding: 8px;
	margin: 16px;}</style></head><body>
	<h2>Customer SignUp Form - JSR303 @Valid example</h2>

	<form:form method="POST" commandName="customer" action="customer/signup">
		<form:errors path="*" cssClass="errorblock" element="div" />
				<td>Customer Name :</td>
				<td><form:input path="name" /></td>
				<td><form:errors path="name" cssClass="error" /></td>
				<td>Customer Age :</td>
				<td><form:input path="age" /></td>
				<td><form:errors path="age" cssClass="error" /></td>
				<td colspan="3"><input type="submit" /></td>

File : Done.jsp


6. Demo

URL : http://localhost:8080/SpringMVC/customer – Customer form page, with 2 text boxes for name and age.

Spring MVC JSR303 demo page

URL : http://localhost:8080/SpringMVC/customer/signup – If you didn’t fill in the form and click on the “submit” button, your customized validation error messages will be displayed.

Spring MVC JSR303 demo page - error message

Download Source Code


  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 319
博文 663
码字总数 86580
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
* 支付类型