我可以在xxxx-validation.xml中为List配置验证吗? [英] Can i configure validation in xxxx-validation.xml for List?

查看:124
本文介绍了我可以在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中进行Part的验证?

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?

推荐答案

您可以使用访问者验证器;

您应该添加< validator type =visitor> 与您的 BookingAction-validation.xml 中的 parts 对象相关的代码段,以激活访问者验证;

You should add the <validator type="visitor"> snippet related to parts object in your BookingAction-validation.xml to activate the Visitor validation;

然后,您需要在包下创建一个 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验证拦截器将使用第二个文件来验证List的每个元素。

Struts2 Validation Interceptor will take care of validating each element of the List by using this second file.

如果你在另一个 Action <中包含 List< Part> 对象,那么副作用很好/ code>,您对 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.

阅读 此详细示例 ,尤其是访问者验证示例(以及以下使用Expression Validator的访问者验证)part。

Read this detailed example, especially the Visitor Validation Example (and the following Visitor Validation with the Expression Validator) part.

这篇关于我可以在xxxx-validation.xml中为List配置验证吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆