JSR303校验
Posted 腾飞新星
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JSR303校验相关的知识,希望对你有一定的参考价值。
由于Validation框架通过硬编码完成数据校验,在实际开发中会显得比较麻烦,因此现代开发更加推荐使用JSR303完成数据校验。
JSR303是java为bean数据合法性校验锁提供的一个标准规范,叫做Bean Validation。它是一个运行时的数据验证框架,在验证之后验证的错误信息会被马上返回。
JSR303是一个规范,它的核心接口是javax.validation.Validator,该接口根据目标对象类中所标注的校验注解进行数据校验,并得到校验结果。
目前有两个实现,第一个是Hibernate Validator(下载地址http://hibernate.org/validator/),第二个Apache bval
package com.domain; import java.io.Serializable; import java.util.Date; import javax.validation.constraints.Pattern; import org.hibernate.validator.constraints.Email; import org.hibernate.validator.constraints.Length; import org.hibernate.validator.constraints.NotBlank; import org.hibernate.validator.constraints.Range; import org.springframework.format.annotation.DateTimeFormat; public class User implements Serializable{ @NotBlank(message="NotBlank.user.loginname") private String loginname; @NotBlank(message="NotBlank.user.password") @Length(min=6,max=8,message="Length.user.password") private String password; @NotBlank(message="NotBlank.user.username") private String username; @Email(message="Email.user.youxiang") private String youxiang; @Range(min=15,max=60,message="Range.user.nianling") private String nianling; //日期使用注解方式转换 @DateTimeFormat(pattern="yyyy-MM-dd") private Date shengri; @Pattern(regexp="[1],[3,8],[3,6,9][0-9]{8}",message="Patter.user.dianhua") private String dianhua; public String getLoginname() { return loginname; } public void setLoginname(String loginname) { this.loginname = loginname; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getYouxiang() { return youxiang; } public void setYouxiang(String youxiang) { this.youxiang = youxiang; } public String getNianling() { return nianling; } public void setNianling(String nianling) { this.nianling = nianling; } public Date getShengri() { return shengri; } public void setShengri(Date shengri) { this.shengri = shengri; } public String getDianhua() { return dianhua; } public void setDianhua(String dianhua) { this.dianhua = dianhua; } }
package com.controller; import javax.validation.Valid; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.Errors; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import com.domain.User; @Controller public class UserController { @RequestMapping(value="/{formName}") public String loginForm(@PathVariable String formName,Model model) { User user = new User(); model.addAttribute("user", user); return formName; } //使用@Valid注解对提交的数据进行校验 @RequestMapping(value="/login",method=RequestMethod.POST) public String login(@Valid @ModelAttribute User user,Errors errors,Model model) { if(errors.hasErrors()) { return "registerForm"; } return "success"; } }
<form:form modelAttribute="user" method="post" action="login"> <table> <tr> <td>登录名</td> <td><form:input path="loginname"/></td> <td><form:errors path="loginname" cssStyle="color:Red"></form:errors></td> <td></td> </tr> <tr> <td>密码</td> <td><form:input path="password"/></td> <td><form:errors path="password" cssStyle="color:Red"></form:errors></td> <td></td> </tr> <tr> <td>用户名</td> <td><form:input path="username"/></td> <td><form:errors path="username" cssStyle="color:Red"></form:errors></td> </tr> <tr> <td>年龄</td> <td><form:input path="nianling"/></td> <td><form:errors path="nianling" cssStyle="color:Red"></form:errors></td> </tr> <tr> <td>邮箱</td> <td><form:input path="youxiang"/></td> <td><form:errors path="youxiang" cssStyle="color:Red"></form:errors></td> </tr> <tr> <td>生日</td> <td><form:input path="shengri"/></td> <td><form:errors path="shengri" cssStyle="color:Red"></form:errors></td> </tr> <tr> <td>电话</td> <td><form:input path="dianhua"/></td> <td><form:errors path="dianhua" cssStyle="color:Red"></form:errors></td> </tr> <tr> <td><input type="submit" value="提交"></td> </tr> </table> </form:form>
<context:component-scan base-package="com.controller"></context:component-scan> <mvc:annotation-driven></mvc:annotation-driven> <!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/content/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basenames" value="classpath:messages"></property> </bean>
以上是关于JSR303校验的主要内容,如果未能解决你的问题,请参考以下文章
Java Hibernate Validator JSR-303验证