当前位置:编程学习 > JAVA >>

spring-mvc学习整理 form validation

使用@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

01
public class LeadsForm {
02
    /**
03
     * <a href="http://my.oschina.net/u/244147" target="_blank" rel="nofollow">@see</a>  javax.validation.constraints.NotNull 使用标准中的注释类
04
     */
05
    <a href="http://my.oschina.net/notnull" target="_blank" rel="nofollow">@NotNull</a>
06
    @Size()
07
    private String  contactName;
08
    <a href="http://my.oschina.net/notnull" target="_blank" rel="nofollow">@NotNull</a>
09
    @Pattern(regexp = "^1\\d{10}$")
10
    private String  contactTel;
11
 
12
    //~~GETTERS && SETTERS
13
}
2.controller

01
/**
02
 * Renhenet.com Inc.
03
 * Copyright (c) 2004-2012 All Rights Reserved.
04
 */
05
package com.renhenet.web;
06
import javax.validation.Valid;
07
 
08
import org.springframework.stereotype.Controller;
09
import org.springframework.ui.ModelMap;
10
import org.springframework.validation.BindingResult;
11
import org.springframework.web.bind.annotation.ModelAttribute;
12
import org.springframework.web.bind.annotation.RequestMapping;
13
import org.springframework.web.bind.annotation.RequestMethod;
14
 
15
import sample.LeadsForm;
16
 
17
/**
18
 *
19
 * <a href="http://my.oschina.net/arthor" target="_blank" rel="nofollow">@author</a>  xiaoxia.dong
20
 * @version $Id: PostController.java,v 1.1 2012/08/10 14:38:11 dongxx Exp $
21
 */
22
@Controller
23
@RequestMapping("/post.html")
24
public class PostController {
25
 
26
    @ModelAttribute("leadsForm")
27
    public void modelAttribute(LeadsForm leadsForm) {
28
    }
29
 
30
    //使用 @Valid
31
    @RequestMapping(method = RequestMethod.POST)
32
    public String processSubmit(@Valid LeadsForm leadsForm, BindingResult result, ModelMap map) {
33
        if (result.hasErrors()) {
34
           
35
        }
36
 
37
        return "post";
38
    }
39
}
使用@Valid annatation 就不用在xml文件中配置 validator。其实使用了接口org.springframework.validation.Validator,默认实现类org.springframework.validation.beanvalidation.LocalValidatorFactoryBean

3.使用velocity模板,建立macro。spring的RequestContext可以通过$springMacroRequestContext变量在velocity中使用。

另外error.code刚好是@NotNull @Pattern等的类名

01
#macro( getErrorCode $objectName $field)
02
#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
03
#end
04
 
05
#macro( Pattern $objectName $field $message)
06
    #if("#getErrorCode($objectName,$field)"=="Pattern")
07
        $!message
08
    #end
09
#end
10
#macro( NotNull $objectName $field $message)
11
    #if("#getErrorCode($objectName,$field)"=="NotNull")
12
        $!message
13
    #end
14
#end
15
#macro( Size $objectName $field $message)
16
    #if("#getErrorCode($objectName,$field)"=="Size")
17
        $!message
18
    #end
19
#end
模板页面这样使用
 
01
<form class="form-horizontal" method="post">
02
  <fieldset>
03
    <legend>快速报名</legend>
04
    <div class="control-group">
05
      <label class="control-label" for="input01">姓名</label>
06
      <div class="controls">
07
        <input type="text" class="input-xlarge" name="contactName" value="$!leadsForm.contactName">
08
        <p class="help-block">字母,数字,汉字皆可 #getErrorCode("leadsForm","contactName")</p>
09
      </div>
10
    </div>
11
    <div class="control-group">
12
      <label class="control-label" for="input01">手机号码</label>
13
      <div class="controls">
14
         <input type="text" class="input-xlarge" name="contactTel">
15
        <p class="help-block">#Pattern("leadsForm","contactTel","手机号码的格式不正确")</p>
16
        
17
      </div>
18
    </div>
19
    <div class="control-group">
20
      <label class="control-label" for="input01">地址</label>
21
      <div class="controls">
22
         <select name="contactRegion" id="user_region" onchange='select_region("user_hot","H",this.value)' class="span2">
23
         <option value="">-请选择-</option>
24
         </select>
25
         #NotNull("leadsForm","contactRegion","请选择地区")
26
         <select name="contactRegionHot" id="user_hot" class="span2">
27
  &nb

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,