我可以在 xxxx-validation.xml 中为 List 配置验证吗?
现在在我的保存操作中,我定义了一个称为预订的模型,如下所示:
Now in my save action,i defined a model which called booking and it is as following:
Class BookingAction {
private Booking booking;
...
}
Class Booking {
private String bookingNo;
private String status;
...
private List<Part>parts = new ArrayList<Part>();
...
}
Class Part {
private String partNo;
...
}
我还为该操作定义了一个验证 xml 文件,例如
I also defined a validation xml file for that action,e.g
<validators>
<field name="booking.status">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>${getText("MandatoryFieldEmpty",{"%{getText("BookingMain.status")}"})}</message>
</field-validator>
</field>
<field name="booking.bookedBy">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>${getText("MandatoryFieldEmpty",{"%{getText("BookingMain.bookedBy")}"})}</message>
</field-validator>
</field>
....
我也可以为 Part 定义这种配置吗?
Can I define this kinda of configuration for Part too?
零件在列表中,并且列表是 Booking 的财产,有人可以告诉我是否可以在 BookingAction-validation.xml 中验证零件吗?
The Part is in a List and the List is Booking's property, does anybody could tell me if i could have Part's validation in BookingAction-validation.xml?
推荐答案
你可以使用访客验证器;
您应该在 BookingAction-validation.xml
中添加与 parts
对象相关的 <validator type="visitor">
片段激活访客验证;
You should add the <validator type="visitor">
snippet related to parts
object in your BookingAction-validation.xml
to activate the Visitor validation;
然后,您需要在 Part
对象的包下创建一个 Part-validation.xml
(而不是包Action
对象),并在那里为单个 Part 元素指定规则.
Then, you'll need to create an Part-validation.xml
under the package of the Part
Object (instead of the package of the Action
object), and specify there the rule for a single Part element.
Struts2 Validation Interceptor
将使用第二个文件来验证 List 的每个元素.
Struts2 Validation Interceptor
will take care of validating each element of the List by using this second file.
作为一个很好的副作用,如果您在另一个 Action
中包含一个 List<Part>
对象,则您对 Part
对象的验证将是已经存在,无需在另一个文件中重写(您只需在 Action-validation.xml 文件中声明验证器片段).
As a nice side effect, if you include a List<Part>
object in another Action
, your validation for Part
object will be already there, with no need to rewrite it in another file (you will only need to declare the validator snippet in your Action-validation.xml file).
编辑
仅在某些特定情况下,您可以使用更具体的 Bean-context-validation.xml
文件指定不同的上下文来触发对同一 bean 的进一步补充验证.
You can specify different contexts for triggering a further, complementary validation of the same bean by using a more specific Bean-context-validation.xml
file, only in some specific cases.
阅读这个详细的例子,尤其是 Visitor Validation Example
(以及下面的 Visitor Validation with the Expression Validator
)部分.
Read this detailed example, especially the Visitor Validation Example
(and the following Visitor Validation with the Expression Validator
) part.
相关文章