SpringMVC学习--校验
- 简介
项目中,通常使用较多是前端的校验,比如页面中js校验。对于安全要求较高点建议在服务端进行校验。
服务端校验:
控制层conroller:校验页面请求的参数的合法性。在服务端控制层conroller校验,不区分客户端类型(浏览器、手机客户端、远程调用)
业务层service(使用较多):主要校验关键业务参数,仅限于service接口中使用的参数。
持久层dao:一般是不校验的。
- springmvc校验
springmvc使用hibernate的校验框架validation。
校验思路:页面提交请求的参数,请求到controller方法中,使用validation进行校验。如果校验出错,将错误信息展示到页面。
- 环境准备
hibernate的校验框架validation所需要jar包:
- 配置校验器
<!-- 校验器 -->
<bean id="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<!-- hibernate校验器-->
<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>
- 校验器注入到处理器适配器中
<mvc:annotation-driven conversion-service="conversionService"
validator="validator"></mvc:annotation-driven>
- 在POJO中添加校验规则
//字符长度在1-30个字符之间
//message提示校验出错显示的信息
@Size(min=1,max=30,message="{items.length.error.message}")
private String name;
//创建日期非空校验
@NotNull(message="{items.createtime.isNull}")
private Date createtime;
- 校验信息配置文件
CustomValidationMessages.properties
- 捕获校验错误和页面显示校验错误信息
// 商品信息修改提交
@RequestMapping("/editItemsSubmit")
public String editItemsSubmit(HttpServletRequest request, Integer id,
@Validated ItemsCustom itemsCustom,BindingResult bindingResult) throws Exception {
//获取校验错误信息
if(bindingResult.hasErrors()){
//输出错误信息
List<ObjectError> allerrors = bindingResult.getAllErrors();
for (ObjectError objectError : allerrors) {
System.out.println(objectError.getDefaultMessage());
}
//将错误信息回显到页面
request.setAttribute("allerrors", allerrors);
return "items/editItems";
}
// 调用service更新商品信息,页面需要将商品信息传到此方法
itemsService.updateItems(id, itemsCustom); // 重定向到商品查询列表
// return "redirect:queryItems.action";
// 页面转发
return "forward:queryItems.action";
// return "success";
}
页面显示错误信息:
<!-- 显示错误信息 -->
<c:if test="${allErrors!=null }">
<c:forEach items="${allErrors }" var="error">
${ error.defaultMessage}<br/>
</c:forEach>
</c:if>
- 校验分组
在pojo中定义校验规则,而pojo是被多个 controller所共用,当不同的controller方法对同一个pojo进行校验,但是每个controller方法需要不同的校验。
解决方法:定义多个校验分组(其实是一个java接口),分组中定义有哪些规则,每个controller方法使用不同的校验分组。
1、校验分组
public interface ValidGroup1 {
//接口中不需要定义任何方法,仅是对不同的校验规则进行分组
//此分组只校验商品名称长度 }
2、在校验中添加分组信息
@Size(min=1,max=30,message="{items.length.error.message}",groups={ValidGroup1.class})
private String name;
3、在controller方法使用指定分组的校验
public String editItemsSubmit(HttpServletRequest request, Integer id,
@Validated(value={ValidGroup1.class}) ItemsCustom itemsCustom,BindingResult bindingResult) throws Exception
主要添加如下信息:@Validated(value={ValidGroup1.class})
注解的主要用法:
@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=) 被注释的元素必须在合适的范围内
最新文章
- 【转】Unix NetWork Programming——环境搭建(解决unp.h等源码编译问题)
- 【poj3254】 Corn Fields
- 关于python文件操作
- ansible 2.2的源码编译安装
- cnblogs美化及插件
- HTML5储存
- 【原创】FAT32系统硬盘的数据恢复实例,这篇文章也是从自己QQ空间的,转移过来的
- Zedboard甲诊opencv图像处理(四)
- python中os.walk浏览目录和文件
- yeoman 使用问题总结
- Overfitting&;Underfitting Problems
- IE浏览器-在Win7系统的安装和卸载
- 通过TCP实现显示屏截图请求及回传
- (五十)Quartz2D生成图片的一些应用
- 仓储repository概念
- linux_grub resue模式恢复
- 【论文阅读】Deep Mixture of Diverse Experts for Large-Scale Visual Recognition
- 查看linux系统硬盘目录占用大小
- java学习第05天(数组常见操作、数组中的数组)
- ADNI数据集相关概念整理