Hibernate Validator利用注解进行数据验证 工程测试
maven pom.xml配置依赖库数据模型Goods控制器GoodsController数据输入addgoods.jsp数据展示showgoods.jspspring-servlet.xml配置文件测试
maven pom.xml配置依赖库
<!-- https
://mvnrepository
.com
/artifact
/org
.hibernate
.validator
/hibernate
-validator
-->
<dependency
>
<groupId
>org
.hibernate
.validator
</groupId
>
<artifactId
>hibernate
-validator
</artifactId
>
<version
>6.0.16.Final
</version
>
</dependency
>
<!-- https
://mvnrepository
.com
/artifact
/javax
.validation
/validation
-api
-->
<dependency
>
<groupId
>javax
.validation
</groupId
>
<artifactId
>validation
-api
</artifactId
>
<version
>2.0.1.Final
</version
>
</dependency
>
<!-- https
://mvnrepository
.com
/artifact
/jboss
.logging
/jboss
-logging
-spi
-->
<dependency
>
<groupId
>jboss
.logging
</groupId
>
<artifactId
>jboss
-logging
-spi
</artifactId
>
<version
>1.0</version
>
</dependency
>
<!-- https
://mvnrepository
.com
/artifact
/com
.fasterxml
/classmate
-->
<dependency
>
<groupId
>com
.fasterxml
</groupId
>
<artifactId
>classmate
</artifactId
>
<version
>1.3.4</version
>
</dependency
>
数据模型Goods
package pojo
;
import org
.hibernate
.validator
.constraints
.Range
;
import org
.springframework
.format
.annotation
.DateTimeFormat
;
import javax
.validation
.constraints
.NotEmpty
;
import javax
.validation
.constraints
.Past
;
import java
.util
.Date
;
public class Goods {
@
NotEmpty(message
="{goods.gname.required}")
private String gname
;
@
NotEmpty(message
="{goods.gdesciption.required}")
private String gdescription
;
@
Range(min
=0,max
=100,message
="{gprice.invalid}")
private double gprice
;
@
DateTimeFormat(pattern
= "yyyy-MM-dd")
@
Past(message
="{gdate.invalid}")
private Date gdate
;
public String
getGname() {
return gname
;
}
public void setGname(String gname
) {
this.gname
= gname
;
}
public String
getGdescription() {
return gdescription
;
}
public void setGdescription(String gdescription
) {
this.gdescription
= gdescription
;
}
public double getGprice() {
return gprice
;
}
public void setGprice(double gprice
) {
this.gprice
= gprice
;
}
public Date
getGdate() {
return gdate
;
}
public void setGdate(Date gdate
) {
this.gdate
= gdate
;
}
}
控制器GoodsController
package control
;
import interCepetor
.logonInterCepetor
;
import org
.apache
.commons
.logging
.Log
;
import org
.apache
.commons
.logging
.LogFactory
;
import org
.springframework
.beans
.factory
.annotation
.Autowired
;
import org
.springframework
.stereotype
.Controller
;
import org
.springframework
.ui
.Model
;
import org
.springframework
.validation
.BindingResult
;
import org
.springframework
.web
.bind
.annotation
.RequestMapping
;
import pojo
.Goods
;
import service
.GoodsServiceImpl
;
import javax
.validation
.Valid
;
@Controller
@
RequestMapping("/goods")
public class GoodsController {
public static Log log
= LogFactory
.getLog(logonInterCepetor
.class);
@Autowired
private GoodsServiceImpl goodsService
;
@
RequestMapping("/input")
public String
input(Model model
) {
model
.addAttribute("goods", new Goods());
return "addgoods";
}
@
RequestMapping("/save")
public String
save(@Valid Goods goods
, BindingResult result
, Model model
) {
log
.info("this is info check" + result
.hasErrors());
if (result
.hasErrors()) {
log
.info(("this if info check" + result
.getFieldErrors()));
return "addgoods";
}
goodsService
.save(goods
);
model
.addAttribute("goodsList", goodsService
.getGoods());
return "showgoods";
}
}
数据输入addgoods.jsp
<%@ page language
="java" contentType
="text/html; charset=UTF-8"
pageEncoding
="UTF-8"%>
<%@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=UTF-8">
<title
>添加商品
</title
>
</head
>
<body
>
<form
:form modelAttribute
="goods" action
="/goods/save" method
="post">
<fieldset
>
<legend
> 添加一件商品
</legend
>
<P
>
<label
>商品名
:</label
>
<form
:input path
="gname" />
</p
>
<p
>
<label
>商品详情
:</label
>
<form
:input path
="gdescription" />
</p
>
<P
>
<label
>商品价格
:</label
>
<form
:input path
="gprice" />
</p
>
<P
>
<label
>创建日期
:</label
>
<form
:input path
="gdate" />
(yyyy
-MM
-dd
)
</p
>
<p id
="buttons">
<input id
="reset" type
="reset">
<input id
="submit" type
="submit" value
="添加">
</p
>
</fieldset
>
<!-- 取出所有验证错误
-->
<form
:errors path
="*" />
</form
:form
>
</body
>
</html
>
数据展示showgoods.jsp
<%@ page language
="java" contentType
="text/html; charset=UTF-8"
pageEncoding
="UTF-8"%>
<%@taglib prefix
="c" uri
="http://java.sun.com/jsp/jstl/core"%>
<%@page isELIgnored
="false" %>
<!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=UTF-8">
<title
>Insert title here
</title
>
</head
>
<body
>
<table
>
<tr
>
<td
>商品名
</td
>
<td
>商品详情
</td
>
<td
>商品价格
</td
>
<td
>创建日期
</td
>
</tr
>
<c
:forEach items
="${goodsList}" var
="goods">
<tr
>
<td
>$
{goods
.gname
}</td
>
<td
>$
{goods
.gdescription
}</td
>
<td
>$
{goods
.gprice
}</td
>
<td
>$
{goods
.gdate
}</td
>
</tr
>
</c
:forEach
>
</table
>
</body
>
</html
>
spring-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
-4.0.xsd
http
://www
.springframework
.org
/schema
/mvc http
://www
.springframework
.org
/schema
/mvc
/spring
-mvc
-4.0.xsd
http
://www
.springframework
.org
/schema
/context http
://www
.springframework
.org
/schema
/context
/spring
-context
-4.0.xsd"
>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<!--前缀
-->
<property name
="prefix" value
="/WEB-INF/jsp/"/>
<!--后缀
-->
<property name
="suffix" value
=".jsp"/>
</bean
>
<context
:component
-scan base
-package
="control"/>
<context
:component
-scan base
-package
="service"/>
<!-- 配置消息属性文件
-->
<bean id
="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<!-- 资源文件名
-->
<property name
="basenames">
<list
>
<value
>/WEB
-INF
/resource
/errorMessages
</value
>
</list
>
</property
>
<!-- 资源文件编码格式
-->
<property name
="fileEncodings" value
="utf-8" />
<!-- 对资源文件内容缓存的时间,单位为秒
-->
<property name
="cacheSeconds" value
="120" />
</bean
>
<!-- 注册校验器
-->
<bean id
="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<!-- hibernate 校验器
-->
<property name
="providerClass" value
="org.hibernate.validator.HibernateValidator" />
<!-- 指定校验使用的资源文件,在文件中配置校验错误信息,如果不指定则默认使用 classpath下的 VaiidationMessages
.properties
-->
<property name
="validationMessageSource" ref
="messageSource" />
</bean
>
<!--开启 Spring的 Valid 功能
-->
<mvc
:annotation
-driven conversion
-service
="conversionService" validator
="validator" />
<!-- 注册格式化转换器,因为用到日期转换
-->
<bean id
="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"/>
</beans
>
测试