SpringMVC实现Validation校验过程详解

网友投稿 227 2022-12-20


SpringMVC实现Validation校验过程详解

这篇文章主要介绍了SpringMVC实现Validation校验过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

一、概述

对前端的校验大多数通过js在页面校验,这种方法比较简单,如果对安全性考虑,还要在后台校验。

springmvc使用JSR-303(javaEE6规范的一部分)校验规范,springmhttp://vc使用的是Hibernate Validator(和Hibernate的ORM)

二、步骤

2.1 引入 Hibernate Validator

org.hibernate

hibernate-validator

5.4.1.Final

2.2 配置

class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">

class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">

class="org.springframework.context.support.ReloadableResourceBundleMessageSource">

classpath:CustomValidationMessages

class="org.springframework.context.support.ReloadableResourceBundleMessageSource">

classpath:CustomValidationMessages

class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">

class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">

class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">

class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">

class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">

class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">

class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">

2.3 创建CustomValidationMessages

在classpath下创建CustomValidationMessages.properties

# 校验提示信息:还需要在java中配置

items.name.length.error=商品长度请限制在1-30之间items.createtime.is.notnull=请输入商品生产日期

2.4 校验规则

商品信息提交时校验 ,商品生产日期不能为空,商品名称长度在1到30字符之间

public class Items {

private Integer id;

//商品名称的长度请限制在1到30个字符

@Size(min=1,max=30,message="{items.name.length.error}")

private String name;

private Float price;

private String pic;

//请muuivnRIkn输入商品生产日期

@NotNull(message="{items.createtime.is.notnull}")

private Date createtime;

private String detail;

}

2.5 捕获错误

需要修改controller方法,在要校验的pojo前边加上@Validated,

public String editItemSubmit(Model model,Integer id,

@Validated @ModelAttribute(value="itemsCustom") ItemsCustom itemsCustom,

BindingResult bindingResult,

//上传图片

MultipartFile pictureFile

)throws Exception{

//输出校验错误信息

//如果参数绑定时有错

//输出校验错误信息

//如果参数绑定时有错

if(bindingResult.hasErrors()){

//获取错误

List errors = bindingResult.getAllErrors();

//准备在页面输出errors,页面使用jstl遍历

model.addAttribute("errors", errors);

for(ObjectError error:errors){

//输出错误信息

System.out.println(error.getDefaultMessage());

}

//如果校验错误,回到商品修改页面

return "editItem";

}

}

2.6 在页面上展示错误

${error.defaultMessage }

2.7 分组校验

需求

针对不同的controller方法通过分组校验达到个性化校验的目的,修改商品修改功能,只校验生产日期不能为空。

第一步:创建分组接口

public interface ValidGroup1 {

//接口不定义方法,就是只标识 哪些校验 规则属于该 ValidGroup1分组

}

第二步:定义校验规则属于哪个分组

//请输入商品生产日期

//通过groups指定此校验属于哪个分组,可以指定多个分组 @NotNull(message="{items.createtime.is.notnull}",groups={ValidGroup1.class})

private Date createtime;

第三步:在cmuuivnRIknontroller方法定义使用校验的分组

public String editItemSubmit(Model model,Integer id,

@Validated(value={ValidGroup1.class}) @ModelAttribute(value="itemsCustom") ItemsCustom itemsCustom,

BindingResult bindingResult,

//上传图片

MultipartFile pictureFile

)throws Exception{

//...其他代码省略...

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java transient 关键字是干啥的
下一篇:SpringMVC RESTful支持实现过程演示
相关文章

 发表评论

暂时没有评论,来抢沙发吧~