springmvc validator和springContext validator的区别
1.springContext validator 依赖于代理实现
MethodValidationInterceptor
Set<ConstraintViolation<Object>> result; try {
result = execVal.validateParameters(
invocation.getThis(), methodToValidate, invocation.getArguments(), groups);
}
catch (IllegalArgumentException ex) {
// Probably a generic type mismatch between interface and impl as reported in SPR-12237 / HV-1011
// Let's try to find the bridged method on the implementation class...
methodToValidate = BridgeMethodResolver.findBridgedMethod(
ClassUtils.getMostSpecificMethod(invocation.getMethod(), invocation.getThis().getClass()));
result = execVal.validateParameters(
invocation.getThis(), methodToValidate, invocation.getArguments(), groups);
}
if (!result.isEmpty()) {
throw new ConstraintViolationException(result);
} Object returnValue = invocation.proceed();
2.springmvc validator 在 RequestResponseBodyMethodProcessor 中处理
@Override
public void validate(Object target, Errors errors, Object... validationHints) {
if (this.targetValidator != null) {
processConstraintViolations(
this.targetValidator.validate(target, asValidationGroups(validationHints)), errors);
}
}
主要区别:mvc会帮我们对返回的Set<ConstraintViolation<Object>> violations 进行封装,易于返回
protected void processConstraintViolations(Set<ConstraintViolation<Object>> violations, Errors errors) {
for (ConstraintViolation<Object> violation : violations) {
String field = determineField(violation);
FieldError fieldError = errors.getFieldError(field);
if (fieldError == null || !fieldError.isBindingFailure()) {
try {
ConstraintDescriptor<?> cd = violation.getConstraintDescriptor();
String errorCode = determineErrorCode(cd);
Object[] errorArgs = getArgumentsForConstraint(errors.getObjectName(), field, cd);
if (errors instanceof BindingResult) {
// Can do custom FieldError registration with invalid value from ConstraintViolation,
// as necessary for Hibernate Validator compatibility (non-indexed set path in field)
BindingResult bindingResult = (BindingResult) errors;
String nestedField = bindingResult.getNestedPath() + field;
if (nestedField.isEmpty()) {
String[] errorCodes = bindingResult.resolveMessageCodes(errorCode);
ObjectError error = new ObjectError(
errors.getObjectName(), errorCodes, errorArgs, violation.getMessage());
error.wrap(violation);
bindingResult.addError(error);
}
else {
Object rejectedValue = getRejectedValue(field, violation, bindingResult);
String[] errorCodes = bindingResult.resolveMessageCodes(errorCode, field);
FieldError error = new FieldError(errors.getObjectName(), nestedField,
rejectedValue, false, errorCodes, errorArgs, violation.getMessage());
error.wrap(violation);
bindingResult.addError(error);
}
}
else {
// got no BindingResult - can only do standard rejectValue call
// with automatic extraction of the current field value
errors.rejectValue(field, errorCode, errorArgs, violation.getMessage());
}
}
catch (NotReadablePropertyException ex) {
throw new IllegalStateException("JSR-303 validated property '" + field +
"' does not have a corresponding accessor for Spring data binding - " +
"check your DataBinder's configuration (bean property versus direct field access)", ex);
}
}
}
}
if (binder.getBindingResult().hasErrors() && isBindExceptionRequired(binder, parameter)) {
throw new MethodArgumentNotValidException(parameter, binder.getBindingResult());
}
springmvc validator和springContext validator的区别的更多相关文章
- jQuery validator plugin之Validator
Validator.destroy() Destroys this instance of validator freeing up resources and unregistering event ...
- springMVC Model ModelMap 和 ModelAndView的区别(转)
原文地址:springMVC Model ModelMap 和 ModelAndView的区别 近来在看代码,发现controller里有不同的处理返回数据的方式,而自己一直在用ModelAndVie ...
- 简述Spring容器与SpringMVC的容器的联系与区别
简述Spring容器与SpringMVC的容器的联系与区别 2017年07月04日 10:55:07 阅读数:6260 摘要: 在Spring整体框架的核心概念中,容器的核心思想是管理Bean的整个生 ...
- java:Springmvc框架3(Validator)
1.springmvcValidator: web.xml: <?xml version="1.0" encoding="UTF-8"?> < ...
- springMVC校验器(validator)
springmvc使用的是Hibernate Validator(和Hibernate的ORM无关)来完成校验功能 一.普通校验 1.导入jar包 2.编写校验错误配置文件 3.配置校验错误信息文件 ...
- SpringMVC学习记录(六)--Validator验证
一.基于Validator接口的验证. 首先创建User实例,并加入几个属性 public class User { private String username; private String p ...
- 【SSM 1】SpringMVC、Spring和Struts的区别
导读:近期做到的项目中,用到的框架师SSM(SpringMVC+Spring+Mybatis),那么在这之前用过SSH,这里主要是区分一下SpringMVC和Struts,但是由于SpringMVC和 ...
- spring-mvc.xml 和 application-context.xml的区别
转自:https://www.cnblogs.com/binlin1987/p/7053016.html application-context.xml是全局的,应用于多个serverlet,配合li ...
- springmvc上下文与springcontext上下文的关系
内容摘自:springmvc与spring上下文的关系 原理区别 一直不清楚springmvc-servlet.xml配置与spring.xml两个配置文件出现的上下文关系.今天找到一上面的文章,倒是 ...
随机推荐
- 《深入理解java虚拟机》读书笔记一——第二章
第二章 Java内存区域与内存溢出异常 1.运行时数据区域 程序计数器: 当前线程所执行的字节码的行号指示器,用于存放下一条需要运行的指令. 运行速度最快位于处理器内部. 线程私有. 虚拟机栈: 描述 ...
- Yii2 JWT
Yii2 JWT 这个扩展为Yii framework 2.0提供了JWT集成(需要PHP 5.6+).它包括基本的HTTP身份验证支持. 目录 安装 依赖关系 基本用法 创建 从字符串分析 验证 令 ...
- 用C#调用外部DLL
1.有时候需要用C#调用外部的dll,例如c++写的dll,首先需要保证dll的编译环境与本项目的环境是相同的,例如都是x86位或者x64位 2.调用声明和dll内的声明一致: function Te ...
- url跳转问题
window.history.replaceState(null, null, "/callPlanning/1") //替换路径不刷新页面 window.location.rep ...
- Java上转型和下转型
Java 转型问题其实并不复杂,只要记住一句话:父类引用指向子类对象. 什么叫父类引用指向子类对象,且听我慢慢道来. 从2个名词开始说起:向上转型(upcasting) .向下转型(downcasti ...
- PLSQL无法连接(不存在或找不到oci.dll)
问题说明:新系统安装plsql后,连接不上Oracle,连接时出现过两种报错 1.找不到OCI.dll文件 2.不能初始化OCI.dll文件,即OCI.dll文件错误 解决方案 plsql连接Orac ...
- ansible笔记(13):变量(二)
1.谈一谈[Gathering Facts]:使用setup模块查看 当我们运行一个playbook时,默认都会运行一个名为“[Gathering Facts]”的任务,前文中已经大致的介绍过这个默认 ...
- pytest学习4-fixtures
源码注释: def fixture(scope="function", params=None, autouse=False, ids=None, name=None): &quo ...
- Windows7自定义主题
一.破解主题限制 Windows系统默认只能允许用户使用系统自带主题(非壁纸),即使用户安装了第三方主题,Windows也会限制很多地方,导致第三方主题用起来怪怪的. 故此,想要一个可以自定义主题的W ...
- es2.0的语法学习
确定文档和查询有多么相关的过程被称为打分(scoring):将查询作为输入,使用不同的手段来确定每一篇文档的得分,将每一个因素最后通过公式综合起来,返回该文档的最终得分.这个综合考量的过程,就是我们希 ...