




已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
SpringMVC数据验证在这里我们采用Hibernate-validator来进行验证,Hibernate-validator实现了JSR-303验证框架支持注解风格的验证。首先我们要到/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.xml文件,如下:复制代码 复制代码其中中的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.Controller;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.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; RequestMapping(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 ModelAttribute(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 org.hibernate.validator.constraints.Range;public class ValidateModel NotEmpty(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 name) =name; public void setAge(String age) this.age=age; public void setEmail(String email) this.email=email; public String getName() return ; public String getAge() return this.age; public String getEmail() return this.email; 复制代码在注解验证消息所在的文件即perties文件中添加以下内容:name.not.empty=u540Du79F0u4E0Du80FDu4E3Au7A7Au3002age.not.inrange=u5E74u9F84u8D85u51FAu8303u56F4u3002email.not.correct=u90AEu7BB1u5730u5740u4E0Du6B63u786Eu3002email.not.empty=u7535u5B50u90AEu4EF6u4E0Du80FDu60DFu6050u3002其中name.not.empty等分别对应了ValidateModel.java文件中message=”xxx”中的xxx名称,后面的内容是在输入中文是自动转换的ASCII编码,当然你也可以直接把xxx写成提示内容,而不用另建一个perties文件再添加,但这是不正确的做法,因为这样硬编码的话就没有办法进行国际化了。在views文件夹中添加validatetest.jsp和validatesuccess.jsp两个视图,内容分别如下:复制代码Insert title here name: age: email: 复制代码复制代码Insert title here 验证成功!复制代码其中特别要指出的是validatetest.jsp视图中的modelAttribute=xxx后面的名称xxx必须与对应的Valid ModelAttribute(xxx) 中的xxx名称一致,否则模型数据和错误信息都绑定不到。即会显示模型对应属性的错误信息,当path=*时则显示模型全部属性的错误信息。下面是主要的验证注解及说明:注解适用的数据类型说明AssertFalseBoolean, boolean验证注解的元素值是falseAssertTrueBoolean, boolean验证注解的元素值是trueDecimalMax(value=x)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.验证注解的元素值小于等于 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-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.验证注解的元素值的整数位数和小数位数上限Futurejava.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 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. Additionally 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 date/time API is on the class path: any implementations ofReadablePartial andReadableInstant.验证注解的元素值(日期类型)比当前时间早Pattern(regex=正则表达式, flag=)String. Additionally supported by HV: any sub-type of CharSequence.验证注解的元素值与指定的正则表达式匹配Size(min=最小值, max=最大值)String, Collection, Map and arrays. Additionally supported by HV: any sub-type of CharSequence.验证注解的元素值的在min和max(包含)指定区间之内,如字符长度、集合大小ValidAny non-primitive type(引用类型)验证关联的对象,如账户对象里有一个订单对象,指定验证订单对象NotEmptyCharSequence,Collection, Map and Arrays验证注解的元素值不为null且不为空(字符串长度不为0、集合大小不为0)Range(min=最小值, max=最大值)CharSequence, Collection, Map and Arrays,BigDecimal, BigInteger, CharSequence, byte, short, i
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 防火门培训知识课件
- 企业文化建设章程的法律要素
- 地热能热泵技术-第1篇-洞察及研究
- 2025年高考政治总复习主观题必背知识复习宝典
- 纺织工艺参数优化-洞察及研究
- 部队司机安全培训课件
- 河南省商丘市永城市第三初级中学2025年统编版六年级下册小升初分班考试语文试卷(无答案)
- 2024-2025学年广州市黄广中学高一(下)开学考政治试题
- 基于区块链的分级花生溯源系统在品质保障与品牌溢价中的实践验证
- 国际市场准入壁垒下围裙产品环保认证标准本土化适配路径探析
- 蜂蛰伤的治疗指南讲课件
- GB/T 45785-2025压缩空气站能源绩效评价
- 非物质文化遗产保护与乡村振兴的协同发展路径
- 窑炉施工安全管理制度
- QGDW10936-2018物料主数据分类与编码规范
- 大学生劳动教育论文2000字论文
- 2025年卫生系统招聘考试医学基础知识新版真题卷(附详细解析)
- 广东省广州市2023-2024学年二年级下学期数学期末试卷(含答案)
- 有机食品连锁超市商业运营计划
- 机器学习赋能空间环境:特征识别与深度分析的创新探索
- 2025-2030年中国压裂砂行业市场现状供需分析及投资评估规划分析研究报告
评论
0/150
提交评论