springmvc注解开发-高级之Validation
来源:互联网 发布:营销数据总结报告 编辑:程序博客网 时间:2024/05/22 08:12
1.1 Validation(了解)
b/s系统中对http请求数据的校验多数在客户端进行,这也是出于简单及用户体验性上考虑,但是在一些安全性要求高的系统中服务端校验是不可缺少的,本节主要学习springmvc实现控制层添加校验。
Spring3支持JSR-303验证框架,JSR-303 是JAVA EE 6 中的一项子规范,叫做BeanValidation,官方参考实现是Hibernate Validator(与Hibernate ORM 没有关系),JSR 303 用于对Java Bean 中的字段的值进行验证。
1.1.1 需求
在商品信息修改提交时对商品信息内容进行校验,例如商品名称必须输入,价格合法性校验。
1.1.2 加入jar包
1.1.3 配置validator
<!--校验器 -->
<beanid="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<!--校验器-->
<property name="providerClass"value="org.hibernate.validator.HibernateValidator"/>
<!--指定校验使用的资源文件,如果不指定则默认使用classpath下的ValidationMessages.properties -->
<property name="validationMessageSource"ref="messageSource"/>
</bean>
<!--校验错误信息配置文件 -->
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<!--资源文件名-->
<property name="basenames">
<list>
<value>classpath:CustomValidationMessages</value>
</list>
</property>
<!-- 资源文件编码格式 -->
<property name="fileEncodings"value="utf-8"/>
<!-- 对资源文件内容缓存时间,单位秒 -->
<property name="cacheSeconds"value="120"/>
</bean>
1.1.4 将validator加到处理器适配器
1.1.4.1配置方式1
<mvc:annotation-driven validator="validator"></mvc:annotation-driven>
1.1.4.2配置方式2(自学)
<!--自定义webBinder -->
<bean id="customBinder"
class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="validator"ref="validator"/>
</bean>
<!--注解适配器 -->
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="webBindingInitializer"ref="customBinder"></property>
</bean>
1.1.5 添加验证规则
publicclass Items {
private Integerid;
@Size(min=1,max=30,message="{item.name.length.error}")
private Stringname;
@NotEmpty(message="{pic.is.null}")
private Stringpic;
1.1.6 错误消息文件CustomValidationMessages
item.name.length.error=商品名称在1到30个字符之间
pic.is.null=请上传图片
如果在eclipse中编辑properties文件无法看到中文则参考“Eclipse开发环境配置-indigo.docx”添加propedit插件。
1.1.7 捕获错误
修改Controller方法:
//商品修改提交
@RequestMapping("/editItemSubmit")
public String editItemSubmit(@Validated@ModelAttribute("item") Itemsitems,BindingResult result,
@RequestParam("pictureFile") MultipartFile[] pictureFile,Model model)
throws Exception {
//如果存在校验错误则转到商品修改页面
if (result.hasErrors()) {
List<ObjectError> errors = result.getAllErrors();
for(ObjectError objectError:errors){
System.out.println(objectError.getCode());
System.out.println(objectError.getDefaultMessage());
}
return"item/editItem";
}
注意:添加@Validated表示在对items参数绑定时进行校验,校验信息写入BindingResult中,在要校验的pojo后边添加BingdingResult,一个BindingResult对应一个pojo,且BingdingResult放在pojo的后边。
商品修改页面显示错误信息:
页头:
<%@page language="java"contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core"prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt"
<%@taglib prefix="spring"uri="http://www.springframework.org/tags"%>
在需要显示错误信息地方:
<spring:hasBindErrorsname="item">
<c:forEachitems="${errors.allErrors}"var="error">
${error.defaultMessage}<br/>
</c:forEach>
</spring:hasBindErrors>
说明:
<spring:hasBindErrorsname="item">表示如果item参数绑定校验错误下边显示错误信息。
上边的方法也可以改为:
在controller方法中将error通过model放在request域,在页面上显示错误信息:
controller方法:
if(bindingResult.hasErrors()){
model.addAttribute("errors", bindingResult);
}
页面:
<c:forEachitems="${errors.allErrors}"var="error">
${error.defaultMessage }<br/>
</c:forEach>
1.1.8 分组校验
如果两处校验使用同一个Items类则可以设定校验分组,通过分组校验可以对每处的校验个性化。
需求:商品修改提交只校验商品名称长度
定义分组:
分组就是一个标识,这里定义一个接口:
publicinterface ValidGroup1 {
}
publicinterface ValidGroup2 {
}
指定分组校验:
publicclass Items {
private Integerid;
//这里指定分组ValidGroup1,此@Size校验只适用ValidGroup1校验
@Size(min=1,max=30,message="{item.name.length.error}",groups={ValidGroup1.class})
private Stringname;
//商品修改提交
@RequestMapping("/editItemSubmit")
public String editItemSubmit(@Validated(value={ValidGroup1.class})@ModelAttribute("item") Items items,BindingResultresult,
@RequestParam("pictureFile") MultipartFile[] pictureFile,Model model)
throws Exception {
在@Validated中添加value={ValidGroup1.class}表示商品修改使用了ValidGroup1分组校验规则,也可以指定多个分组中间用逗号分隔,
@Validated(value={ValidGroup1.class,ValidGroup2.class })
1.1.9 校验注解
@Null 被注释的元素必须为 null
@NotNull 被注释的元素必须不为 null
@AssertTrue 被注释的元素必须为 true
@AssertFalse 被注释的元素必须为 false
@Min(value) 被注释的元素必须是一个数字,其值必须大于等于指定的最小值
@Max(value) 被注释的元素必须是一个数字,其值必须小于等于指定的最大值
@DecimalMin(value) 被注释的元素必须是一个数字,其值必须大于等于指定的最小值
@DecimalMax(value) 被注释的元素必须是一个数字,其值必须小于等于指定的最大值
@Size(max=, min=) 被注释的元素的大小必须在指定的范围内
@Digits (integer, fraction) 被注释的元素必须是一个数字,其值必须在可接受的范围内
@Past 被注释的元素必须是一个过去的日期
@Future 被注释的元素必须是一个将来的日期
@Pattern(regex=,flag=) 被注释的元素必须符合指定的正则表达式
Hibernate Validator 附加的constraint
@NotBlank(message =) 验证字符串非null,且长度必须大于0
@Email 被注释的元素必须是电子邮箱地址
@Length(min=,max=) 被注释的字符串的大小必须在指定的范围内
@NotEmpty 被注释的字符串的必须非空
@Range(min=,max=,message=) 被注释的元素必须在合适的范围内
- springmvc注解开发-高级之Validation
- springmvc注解开发-validation校验
- springmvc注解开发-高级之异常处理
- springmvc注解开发-高级之图片上传
- springmvc注解开发-高级之 拦截器
- springmvc注解开发-validation校验-分组校验
- springmvc注解开发-高级之数据回显
- SpringMVC之注解开发
- springmvc之注解开发
- springmvc注解开发-validation校验-商品修改校验
- SpringMVC之注解开发(二)
- springmvc注解开发之requestMapping
- SpringMVC介绍之Validation
- SpringMVC介绍之Validation
- SpringMVC介绍之Validation
- SpringMVC介绍之Validation
- SpringMVC介绍之Validation
- SpringMVC介绍之Validation
- Android流式布局实现
- Mac下安装新版MySQL5.7
- 电商、商城类APP常用标签"hot"--第三方开源--LabelView
- 搜索入门-----HDU1312
- Mac SVN 命令行
- springmvc注解开发-高级之Validation
- 什么是重构,什么不是重构
- npm 命令行可执行模块 package bin
- undefined reference to错误的解决方法
- Ubuntu安装nginx、php
- 新浪微博登录界面上下拉伸图片--第三方开源--PullToZoomListViewEx(一)
- Icingaweb2监控oracle数据库的安装配置流程
- mysql中FIND_IN_SET的使用方法
- 解决nginx打开php文件总是显示下载框的问题