在Spring 3中,如果类路径上有任何JSR 303验证程序框架,则可以启用@ mid :annotation-driven来通过@Valid
注释支持JSR303 bean验证 。
注意
Hibernate Validator是JSR 303的参考实现
在本教程中,我们向您展示如何通过@Valid
批注将Hibernate验证器与Spring MVC集成,以HTML形式执行bean验证。
使用的技术:
- Spring 3.0.5。发布
- Hibernate Validator 4.2.0.Final
- JDK 1.6
- Eclipse 3.6
- Maven 3
1.项目依赖
在JBoss公共存储库中可以找到Hibernate验证器。
<repositories>
<repository>
<id>JBoss repository</id>
<url>http://repository.jboss.org/nexus/content/groups/public/</url>
</repository>
</repositories>
<properties>
<spring.version>3.0.5.RELEASE</spring.version>
</properties>
<dependencies>
<!-- Spring 3 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Hibernate Validator -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.2.0.Final</version>
</dependency>
</dependencies>
2. JSR303 Bean验证
一个简单的POJO,带有Hibernate验证程序注释。
注意
有关详细说明,请参考此Hibernate验证程序文档 。
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.控制器+ @Valid
为了使验证@Valid
,只需通过@Valid
注释“ JSR注释的模型对象”。 仅此而已,其他内容只是正常的Spring MVC表单处理。
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.错误讯息
默认情况下,如果验证失败。
-
@NotEmpty
将显示“可能不为空” -
@Range
将显示“必须在1到150之间”
您可以轻松地覆盖它,使用“键”和消息创建属性。 要知道哪个@annotation绑定到哪个键,只需调试它并查看“ BindingResult result
”中的值。 通常,键为“ @Annotation Name.object.fieldname ”。
文件:messages.properties
NotEmpty.customer.name = Name is required!
Range.customer.age = Age value must be between 1 and 150
5. mvc:注释驱动
启用“ mvc:annotation-driven
”,以使Spring MVC通过@Valid
支持JSR303验证器,并绑定属性文件。
<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"
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">
<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"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<!-- bind your messages.properties -->
<bean class="org.springframework.context.support.ResourceBundleMessageSource"
id="messageSource">
<property name="basename" value="messages" />
</bean>
</beans>
6. JSP页面
最后一个,带有Spring表单标签库的普通JSP页面。
文件: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" />
<table>
<tr>
<td>Customer Name :</td>
<td><form:input path="name" /></td>
<td><form:errors path="name" cssClass="error" /></td>
</tr>
<tr>
<td>Customer Age :</td>
<td><form:input path="age" /></td>
<td><form:errors path="age" cssClass="error" /></td>
</tr>
<tr>
<td colspan="3"><input type="submit" /></td>
</tr>
</table>
</form:form>
</body>
</html>
档案:Done.jsp
<html>
<body>
<h2>Done</h2>
</body>
</html>
6.演示
URL:http:// localhost:8080 / SpringMVC / customer –客户表单页面,带有2个用于输入名称和年龄的文本框。
URL:http:// localhost:8080 / SpringMVC / customer / signup –如果您没有填写表格并单击“提交”按钮,则将显示您的自定义验证错误消息。
下载源代码
下载它– SpringMVC-Bean-Validation-JSR303-Example-2.zip (9 KB)
参考文献
翻译自: https://mkyong.com/spring-mvc/spring-3-mvc-and-jsr303-valid-example/