*JSR303* 在工程中引入所需的jar 文件,依赖。
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.4.1.Final</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
//漏了这个,后补 上
jboss-logging-3.1.0.CR2.jar
2.POJO: 根据需求;给需要验证的属性增加相应的验证注解; 以teacher为例:
package pojo;
//数据校验
import org.springframework.stereotype.Component;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
// 验证数据的合法性时,要不合法的显示出来在浏览器不输入就可。
@Component
public class Teacher {
@Min(message ="{agesmall}", value=18 )
private int age;
@NotNull(message = "{name_is_not_full}" )
private String name;
public int getAge () {
return age;
}
public void setAge (int age) {
this.age = age;
}
public String getName () {
return name;
}
public void setName (String name) {
this.name = name;
}
}
3. 在Controller 包下。建一个MyTheacherController类“:
package com.kgc.controller;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import pojo.Teacher;
//数据校验
@Component
@RequestMapping("/teacher")
public class MyTheacherController {
@RequestMapping("test")
public String testteacher(@Validated Teacher teacher, Errors errors){
return "result";
}
}
4.在springMVC-config.xml 配置:(还有其他工程所需的,没有注掉!!!)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.kgc.controller" />
<context:annotation-config/>
<mvc:annotation-driven validator="validatorBean"/>
<mvc:resources mapping="/aa/**" location="/imgs/a/c/" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsps/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages"/>
<property name="defaultEncoding" value="utf-8"/>
</bean>
<bean id="validatorBean"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="providerClass" value="org.hibernate.validator.HibernateValidator"/>
<property name="validationMessageSource" ref="messageSource"/>
</bean>
<!-- <bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10485760"/>
<property name="defaultEncoding" value="UTF-8"/>
</bean>-->
<!--<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="myinterceptor.TeacherInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>-->
</beans>
5. 在 resources 下建一个.properties.国际化;(messages_zh_CN.properties)
name_is_not_full=学生姓名不能为空
agesmall=年龄必须大于等于18岁
6.建jsp ;视图 result.jsp
<%@ taglib prefix="Form" uri="http://www.springframework.org/tags/form" %>
<%--
Created by IntelliJ IDEA.
User: XiaQingBo
Date: 2018-10-29
Time: 10:57
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>HELLO</h1>
<Form:form commandName="teacher">
<Form:errors path="*"/>
</Form:form>
</body>
</html>
7.运行: