Spring MVC 学习笔记 十 使用jsr 303进行校验

Spring MVC在使用了<mvc:annotation-driven> 后,如果路径中有jsr 303的实现,将自动提供对jsr 303验证方式的支持。
[size=medium][b]一:使用hibernate validator进行数据验证[/b][/size]
选择的是hibernate validator,因此需要修改pom.xml增加对hibernate validator的支持。
		<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.1.0.Final</version>
</dependency>

新增一个测试的pojo bean ,增加jsr 303格式的验证annotation
	@NotEmpty
private String userName;
@Email
private String email;

在controller 类中的handler method中,对需要验证的对象前增加@Valid 标志

	@RequestMapping("/valid")
public String valid(@ModelAttribute("vm") [color=red]@Valid[/color] ValidModel vm, BindingResult result) {
if (result.hasErrors()) {
return "validResult";
}

return "helloworld";
}

增加显示验证结果的jsp如下
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

<html>
<head>
<title>Reservation Form</title>
<style>
.error {
color: #ff0000;
font-weight: bold;
}
</style>
</head>

<body>
<form:form method="post" modelAttribute="vm">
<form:errors path="*" cssClass="error" />
<table>
<tr>
<td>Name</td>
<td><form:input path="userName" />
</td>
<td><form:errors path="userName" cssClass="error" />
</td>
</tr>
<tr>
<td>email</td>
<td><form:input path="email" />
</td>
<td><form:errors path="email" cssClass="error" />
</td>
</tr>

<tr>
<td colspan="3"><input type="submit" />
</td>
</tr>
</table>
</form:form>
</body>
</html>

访问 http://localhost:8080/springmvc/valid?userName=winzip&email=winzip
查看验证结果。
[size=medium][b]二:自定义jsr 303格式的annotation[/b][/size]
参考hibernate validator 4 reference 手册中3.1节,增加一个自定义要求输入内容为定长的annotation验证类
新增annotation类定义

@Target( { METHOD, FIELD, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Constraint(validatedBy = FixLengthImpl.class)
public @interface FixLength {

int length();
String message() default "{net.zhepu.web.valid.fixlength.message}";

Class<?>[] groups() default {};

Class<? extends Payload>[] payload() default {};
}

及具体的验证实现类如下

public class FixLengthImpl implements ConstraintValidator<FixLength, String> {
private int length;
@Override
public boolean isValid(String validStr,
ConstraintValidatorContext constraintContext) {
if (validStr.length() != length) {
return false;
} else {
return true;
}
}

@Override
public void initialize(FixLength fixLen) {
this.length = fixLen.length();
}
}

为使自定义验证标注的message正常显示,需要修改servlet context配置文件,新增messageSource bean,如下
	<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
p:fallbackToSystemLocale="true" p:useCodeAsDefaultMessage="false"
p:defaultEncoding="UTF-8">
<description>Base message source to handle internationalization
</description>
<property name="basenames">
<list>
<!-- main resources -->
<value>classpath:valid/validation</value>
</list>
</property>
</bean>

表示spring 将从路径valid/validation.properties中查找对于的message。
新增valid bean 引用新增的messageSource bean,表示valid message将从messageSource bean 注入。
	<bean id="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"
p:validationMessageSource-ref="messageSource">
<description>Enable the bean validation provider, and configure it to
use the messageSource when resolving properties</description>
</bean>


修改 <mvc:annotation-driven> 增加对validator bean的引用
<mvc:annotation-driven validator="validator" />


最后修改之前新增的pojo bean ,新增一个mobileNO属性并增加对自定义标注的引用
	@FixLength(length=11)
private String mobileNO;

在前端jsp中也增加新字段的支持
			<tr>
<td>mobileno</td>
<td><form:input path="mobileNO" />
</td>
<td><form:errors path="mobileNO" cssClass="error" />
</td>
</tr>


可访问url http://localhost:8080/springmvc/valid?userName=winzip&email=winzip&mobileNO=138188888
来查看修改的结果。

[size=medium][b]三 json输入的验证[/b][/size]
Spring mvc 3.0.5中对于json格式的输入直接使用@valid标注有问题,目前这个bug还未修复 (见 SPR-6709),预计在3.1 m2版本中会修复。
在此之前,可以通过如下几种方式来对json(或xml)格式的输入来进行验证。
1:在handler method中直接对输入结果进行验证

	@RequestMapping("/validJson1")
@ResponseBody
public JsonResult processSubmitjson(@RequestBody ValidModel vm,
HttpServletRequest request) {
JsonResult jsonRst = new JsonResult();

Set<ConstraintViolation<ValidModel>> set = validator.validate(vm);
for (ConstraintViolation<ValidModel> violation : set) {

String propertyPath = violation.getPropertyPath().toString();
;
String message = violation.getMessage();
log.error("invalid value for: '" + propertyPath + "': "
+ message);
}
if (!set.isEmpty()){
jsonRst.setSuccess(false);
jsonRst.setMsg("输入有误!");
return jsonRst;
}

jsonRst.setSuccess(true);
jsonRst.setMsg("输入成功!");
return jsonRst;
}



可通过修改后的helloworld.jsp中的json valid test1按钮进行调用测试。

2:将此验证逻辑封装为一个AOP,当需验证的对象前有@valid标注和@RequestBody标注时开始验证
新增handler method如下
   @RequestMapping("/validJson2")
@ResponseBody
public JsonResult testJson4(@RequestBody @Valid ValidModel vm){
log.info("handle json for valid");
return new JsonResult(true,"return ok");
}

这里没有对输入值做任何验证,所有的验证都在AOP中完成。
修改pom.xml增加对AOP相关类库的引用。

		<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.11</version>
</dependency>

<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${org.springframework.version}</version>
</dependency>

修改servet context xml,增加对aop的支持。
	<!-- enable Spring AOP support -->
<aop:aspectj-autoproxy proxy-target-class="true" />

最后,新增AOP类
public class CustomerValidatorAOP {
private Validator validator;

@Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
private void controllerInvocation() {
}

@Around("controllerInvocation()")
public Object aroundController(ProceedingJoinPoint joinPoint) throws Throwable {

MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
Method method = methodSignature.getMethod();
Annotation[] annotationList = method.getAnnotations();
/* for(Annotation anno:annotationList){
System.out.println(ResponseBody.class.isInstance(anno));
}
*/
Annotation[][] argAnnotations = method.getParameterAnnotations();
String[] argNames = methodSignature.getParameterNames();
Object[] args = joinPoint.getArgs();

for (int i = 0; i < args.length; i++) {
if (hasRequestBodyAndValidAnnotations(argAnnotations[i])) {
Object ret = validateArg(args[i], argNames[i]);
if(ret != null){
return ret;
}
}
}

return joinPoint.proceed(args);
}

private boolean hasRequestBodyAndValidAnnotations(Annotation[] annotations) {
if (annotations.length < 2)
return false;

boolean hasValid = false;
boolean hasRequestBody = false;

for (Annotation annotation : annotations) {
if (Valid.class.isInstance(annotation))
hasValid = true;
else if (RequestBody.class.isInstance(annotation))
hasRequestBody = true;

if (hasValid && hasRequestBody)
return true;
}
return false;
}


private JsonResult validateArg(Object arg, String argName) {
BindingResult result = getBindingResult(arg, argName);
validator.validate(arg, result);
if (result.hasErrors()) {
JsonResult jsonresult = new JsonResult();
jsonresult.setSuccess(false);
jsonresult.setMsg("fail");
return jsonresult;
}
return null;
}

private BindingResult getBindingResult(Object target, String targetName) {
return new BeanPropertyBindingResult(target, targetName);
}

@Required
public void setValidator(Validator validator) {
this.validator = validator;
}

这里只考虑了输入为json格式的情况,仅仅作为一种思路供参考,实际使用时需要根据项目具体情况进行调整。
可通过修改后的helloworld.jsp中的json valid test2按钮进行调用测试。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值