spspringmvc数据验证

上传人:第*** 文档编号:32818433 上传时间:2018-02-12 格式:DOC 页数:8 大小:36.50KB
返回 下载 相关 举报
spspringmvc数据验证_第1页
第1页 / 共8页
spspringmvc数据验证_第2页
第2页 / 共8页
spspringmvc数据验证_第3页
第3页 / 共8页
spspringmvc数据验证_第4页
第4页 / 共8页
spspringmvc数据验证_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《spspringmvc数据验证》由会员分享,可在线阅读,更多相关《spspringmvc数据验证(8页珍藏版)》请在金锄头文库上搜索。

1、SpringMVC 数据验证在这里我们采用 Hibernate-validator 来进行验证,Hibernate-validator 实现了 JSR-303 验证框架支持注解风格的验证。首先我们要到 http:/hibernate.org/validator/下载需要的 jar 包,这里以 4.3.1.Final 作为演示,解压后把 hibernate-validator-4.3.1.Final.jar、jboss-logging-3.1.0.jar、validation-api-1.0.0.GA.jar 这三个包添加到项目中。配置之前项目中的 springservlet-config.xm

2、l 文件,如下:复制代码复制代码其中中的classpath:validatemessages 为注解验证消息所在的文件,需要我们在 resources 文件夹下添加。在 com.demo.web.controllers 包中添加一个 ValidateController.java 内容如下:复制代码package com.demo.web.controllers;import java.security.NoSuchAlgorithmException;import javax.validation.Valid;import org.springframework.stereotype.Con

3、troller;import org.springframework.ui.Model;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 com.demo.web

4、.models.ValidateModel;ControllerRequestMapping(value = /validate)public class ValidateController RequestMapping(value=/test, method = RequestMethod.GET)public String test(Model model)if(!model.containsAttribute(contentModel)model.addAttribute(contentModel, new ValidateModel();return validatetest;Req

5、uestMapping(value=/test, method = RequestMethod.POST)public String test(Model model, Valid ModelAttribute(contentModel) ValidateModel validateModel, BindingResult result) throws NoSuchAlgorithmException/如果有验证错误 返回到 form 页面if(result.hasErrors()return test(model);return validatesuccess; 复制代码其中Valid Mo

6、delAttribute(contentModel) ValidateModel validateModel 的Valid 意思是在把数据绑定到ModelAttribute(contentModel) 后就进行验证。在 com.demo.web.models 包中添加一个 ValidateModel.java 内容如下:复制代码package com.demo.web.models;import org.hibernate.validator.constraints.Email;import org.hibernate.validator.constraints.NotEmpty;import

7、 org.hibernate.validator.constraints.Range;public class ValidateModelNotEmpty(message=name.not.empty)private String name; Range(min=0, max=150,message=age.not.inrange)private String age;NotEmpty(message=email.not.empty)Email(message=email.not.correct)private String email;public void setName(String n

8、ame)this.name=name;public void setAge(String age)this.age=age;public void setEmail(String email)this.email=email;public String getName()return this.name;public String getAge()return this.age;public String getEmail()return this.email;复制代码在注解验证消息所在的文件即 validatemessages.properties 文件中添加以下内容:name.not.em

9、pty=u540Du79F0u4E0Du80FDu4E3Au7A7Au3002age.not.inrange=u5E74u9F84u8D85u51FAu8303u56F4u3002email.not.correct=u90AEu7BB1u5730u5740u4E0Du6B63u786Eu3002email.not.empty=u7535u5B50u90AEu4EF6u4E0Du80FDu60DFu6050u3002其中 name.not.empty 等分别对应了 ValidateModel.java 文件中 message=”xxx”中的 xxx 名称,后面的内容是在输入中文是自动转换的 AS

10、CII 编码,当然你也可以直接把 xxx 写成提示内容,而不用另建一个 validatemessages.properties 文件再添加,但这是不正确的做法,因为这样硬编码的话就没有办法进行国际化了。在 views 文件夹中添加 validatetest.jsp 和 validatesuccess.jsp 两个视图,内容分别如下:复制代码Insert title herename:age:email:复制代码复制代码Insert title here验证成功!复制代码其中特别要指出的是 validatetest.jsp 视图中的 modelAttribute=xxx后面的名称 xxx 必须与

11、对应的 Valid ModelAttribute(xxx) 中的 xxx 名称一致,否则模型数据和错误信息都绑定不到。即会显示模型对应属性的错误信息,当 path=*时则显示模型全部属性的错误信息。下面是主要的验证注解及说明:注解适用的数据类型说明AssertFalseBoolean, boolean验证注解的元素值是 falseAssertTrueBoolean, boolean验证注解的元素值是 trueDecimalMax(value=x )BigDecimal, BigInteger, String, byte,short, int, long and the respective w

12、rappers of the primitive types. Additionally supported by HV: any sub-type of Number andCharSequence.验证注解的元素值小于等于 DecimalMax 指定的 value 值DecimalMin(value=x )BigDecimal, BigInteger, String, byte,short, int, long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-

13、type of Number andCharSequence.验证注解的元素值小于等于 DecimalMin 指定的 value 值Digits(integer=整数位数, fraction=小数位数)BigDecimal, BigInteger, String, byte,short, int, long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type of Number andCharSequence.验证注解的元素值的整数位数和小数位数上限Futu

14、rejava.util.Date, java.util.Calendar; Additionally supported by HV, if theJoda Time date/time API is on the class path: any implementations ofReadablePartial andReadableInstant.验证注解的元素值(日期类型)比当前时间晚Max(value=x)BigDecimal, BigInteger, byte, short,int, long and the respective wrappers of the primitive

15、types. Additionally supported by HV: any sub-type ofCharSequence (the numeric value represented by the character sequence is evaluated), any sub-type of Number.验证注解的元素值小于等于Max 指定的 value 值Min(value=x)BigDecimal, BigInteger, byte, short,int, long and the respective wrappers of the primitive types. Ad

16、ditionally supported by HV: any sub-type of CharSequence (the numeric value represented by the char sequence is evaluated), any sub-type of Number.验证注解的元素值大于等于Min 指定的 value 值NotNullAny type验证注解的元素值不是 nullNullAny type验证注解的元素值是 nullPastjava.util.Date, java.util.Calendar; Additionally supported by HV, if theJoda Time da

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 建筑/环境 > 工程造价

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号