addPropertyNode-在1.0版中是否等效? [英] addPropertyNode - equivalent in version 1.0?
问题描述
可能相对简单. Java 7文档 ConstraintViolationBuilder接口指定:
Relatively simple probably. The Java 7 documentation for the ConstraintViolationBuilder interface specifies:
addNode
ConstraintValidatorContext.ConstraintViolationBuilder.NodeBuilderCustomizableContext addNode(String name)
addNode
ConstraintValidatorContext.ConstraintViolationBuilder.NodeBuilderCustomizableContext addNode(String name)
已弃用. 从1.1开始-被addPropertyNode(String)和addBeanNode()
取代
将节点添加到ConstraintViolation将与之关联的路径.名称描述单个属性.特别是不允许使用点(.).
Deprecated. since 1.1 - replaced by addPropertyNode(String) and addBeanNode()
Adds a node to the path the ConstraintViolation will be associated to. name describes a single property. In particular, dot (.) is not allowed.
现在"该文档还包括提到的方法.
"Now" the documentation also includes the methods mentioned.
我需要使用它来验证一些数据,并且代码正在JBoss AS 7.1.1-Final上运行.
入门"-jBoss 7页面提到:"Java SE 7可以与JBoss AS 7一起使用".现在,我想要要实现的事情很简单:
I had a requirement to validate some data using this, and the code is running on a JBoss AS 7.1.1-Final.
The "Getting Started"-Page of jBoss 7 mentions: "Java SE 7 can be used with JBoss AS 7". Now what I want to achieve is simple:
我有一个Validator implements ConstraintValidator<AnnotationType, DomainClass>
,在那里我想创建一个漂亮的ConstraintViolationException,可以由JSF/Primefaces很好地处理以显示一条消息并将一个字段标记为无效,但是
I have a Validator implements ConstraintValidator<AnnotationType, DomainClass>
, there I want to create a nice-looking ConstraintViolationException, that can be nicely handled by JSF / Primefaces to show a message and mark a field as invalid, but alas:
@Override
public boolean isValid(DomainClass instance, ConstraintValidatorContext context) {
// validation logic
if (valid) {
return true;
} else {
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate(message)
.addPropertyNode("locationName").addConstraintViolation();
return false;
}
}
这就是问题所在...如上面链接的文档所述:addPropertyNode()
仅在Bean验证的1.1版开始可用.不幸的是,JBoss AS 7仅包含BeanValidation版本1.0(在入门页面"中可见).
And here's where it gets problematic... As mentioned in the documentation linked above: addPropertyNode()
is only available as of version 1.1 of the Bean Validation. Unfortunately JBoss AS 7 just includes BeanValidation version 1.0 (as visible in Getting Started Page).
我分析了工作验证的堆栈跟踪,发现ConstraintViolationImpl实例(由hibernate使用)中的propertyPath使用多个点.
I analyzed stacktraces of working validations, and saw, the propertyPath in ConstraintViolationImpl instances (as used by hibernate) uses multiple dots.
文档明确指出:尤其是不允许使用点(.)." .
现在有两种可能性:
- 将应用程序服务器更改为Wildfly(或类似的实现JSR 349的服务器)
- 仅使用
addNode()
方法解决使用JSR 303的问题.
- Change the application server to Wildfly (or similar, where JSR 349 is implemented)
- Solve the problem using JSR 303 only using the
addNode()
-method.
出于这个问题的目的,我们排除了可能性1(不切实际,可能需要做的工作).
For the purpose of this question, we rule out possibility 1 (impractical, possible required work).
尤其是在占位符中需要什么才能使其与JSF Faces-Validation一起正常工作:
In particular what is required in the placeholder for this to work properly with JSF Faces-Validation:
@Override
public boolean isValid(DomainClass instance, ConstraintValidatorContext context) {
// validation logic
if (valid) {
return true;
} else {
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate(message)
.addNode(/* Some awesome String */).addConstraintViolation();
return false;
}
}
最后但并非最不重要的一点是,我完全...并覆盖了一种更简单的方法:(
Or last but not least, I am totally ... and overread a simpler approach to this :(
推荐答案
似乎最后一个选择实际上是正确的选择:( 我很笨我缺少树木的森林...
It seems the last option actually is the correct one :( I am dumb I am missing the forest for the trees...
从文档中看不出来的很明显:.addNode()
调用应该被链接!
What is not quite obvious from the documentation: The .addNode()
calls are supposed to be chained!
@Override
public boolean isValid (DomainClass instance, ConstraintValidatorContext context) {
//validation logic
if (valid) {
return true;
} else {
context.disableDefaultConstraintViolation();
context.buildConstrainViolationWithTemplate(message)
.addNode("tree").addNode("nodes").addNode("to")
.addNode("property").addConstraintViolation();
return false;
}
}
这解决了我的问题.另外,我想在此提及JSR-303维护第4.2节-违反约束定义了构建propertyPath的正确规则:
This solves my problem. Additionally I want to mention here, that the JSR-303 Section 4.2 - Constraint Violation defines the correct rules for building the propertyPath:
Path
由Node
组成,并根据以下规则构建:
Path
is made of Node
s and is built according to the following rules:
- 如果失败的对象是根对象,则将名称设置为null的Node添加到路径.
- 遍历关联时:
- 将名称等于关联属性名称(字段名称或Java Bean属性名称)的Node对象添加到Path
- 如果关联是List或数组,则添加的以下Node对象包含getIndex中的索引值.
- 如果关联是Map,则添加的以下Node对象(代表给定的Map条目)包含getKey中的键值
- 对于所有Iterable或Map,添加的以下Node对象都标记为inIterable(isInIterable)
- if the failing object is the root object, a Node with name set to null is added to the Path.
- When an association is traversed:
- a Node object whose name equals the name of the association property (field name or Java Bean property name) is added to Path
- if the association is a List or an array, the following Node object added contains the index value in getIndex.
- if the association is a Map, the following Node object added (representing a given map entry) contains the key value in getKey
- for all Iterable or Map, the following Node object added is marked as inIterable (isInIterable)
- 将Node对象添加到名称等于属性名称(字段名称或Java Bean属性名称)的Path中
- 属性路径被认为是完整的
- 将Node对象添加到名称为null的Path
- 属性路径被认为是完整的
这篇关于addPropertyNode-在1.0版中是否等效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!