使用@Valid annatation的表单校验
需要引入的依赖 :
javax.validation.validation-api-1.0.0.GA.jar 校验标准接口
hibernate-annotations-3.4.0.GA.jar,hibernate-validator-4.0.1.GA.jar hibernate提供的实现包
slf4j-api-1.5.0.jar,slf4j-nop-1.5.0.jar 日志依赖包
1.pojo Form
public class LeadsForm {
/**
* @see javax.validation.constraints.NotNull 使用标准中的注释类
*/
@NotNull
@Size()
private String contactName;
@NotNull
@Pattern(regexp = "^1\\d{10}$")
private String contactTel;
//~~GETTERS && SETTERS
}
2.controller
/**
* Renhenet.com Inc.
* Copyright (c) 2004-2012 All Rights Reserved.
*/
package com.renhenet.web;
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.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import sample.LeadsForm;
/**
*
* @author xiaoxia.dong
* @version $Id: PostController.java,v 1.1 2012/08/10 14:38:11 dongxx Exp $
*/
@Controller
@RequestMapping("/post.html")
public class PostController {
@ModelAttribute("leadsForm")
public void modelAttribute(LeadsForm leadsForm) {
}
//使用 @Valid
@RequestMapping(method = RequestMethod.POST)
public String processSubmit(@Valid LeadsForm leadsForm, BindingResult result, ModelMap map) {
if (result.hasErrors()) {
}
return "post";
}
}
使用@Valid annatation 就不用在xml文件中配置 validator。其实使用了接口org.springframework.validation.Validator,默认实现类org.springframework.validation.beanvalidation.LocalValidatorFactoryBean
3.使用velocity模板,建立macro。spring的RequestContext可以通过$springMacroRequestContext变量在velocity中使用。
另外error.code刚好是@NotNull @Pattern等的类名
#macro( getErrorCode $objectName $field)
#set($errors = $springMacroRequestContext.getErrors($objectName))#if($errors && $errors.fieldErrors && $errors.fieldErrors.size()>0)#foreach($e in $errors.fieldErrors)#if("$!field"=="$e.field")$e.code#end#end#end
#end
#macro( Pattern $objectName $field $message)
#if("#getErrorCode($objectName,$field)"=="Pattern")
$!message
#end
#end
#macro( NotNull $objectName $field $message)
#if("#getErrorCode($objectName,$field)"=="NotNull")
$!message
#end
#end
#macro( Size $objectName $field $message)
#if("#getErrorCode($objectName,$field)"=="Size")
$!message
#end
#end
模板页面这样使用
<form class="form-horizontal" method="post">
<fieldset>
<legend>快速报名</legend>
<div class="control-group">
<label class="control-label" for="input01">姓名</label>
<div class="controls">
<input type="text" class="input-xlarge" name="contactName" value="$!leadsForm.contactName">
<p class="help-block">字母,数字,汉字皆可 #getErrorCode("leadsForm","contactName")</p>
</div>
</div>
<div class="control-group">
<label class="control-label" for="input01">手机号码</label>
<div class="controls">
<input type="text" class="input-xlarge" name="contactTel">
<p class="help-block">#Pattern("leadsForm","contactTel","手机号码的格式不正确")</p>
</div>
</div>
<div class="control-group">
<label class="control-label" for="input01">地址</label>
<div class="controls">
<select name="contactRegion" id="user_region" onchange='select_region("user_hot","H",this.value)' class="span2">
<option value="">-请选择-</option>
</select>
#NotNull("leadsForm","contactRegion","请选择地区")
<select name="contactRegionHot" id="user_hot" class="span2">
<option value="">-请选择-</option>
</select>
<input type="text" class="input-xlarge" name="contactAddress">
#Size("leadsForm","contactAddress","请输入详细地址")
</div>
</div>
</fieldset>
<input type="submit" value="报名">
</form>