Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
402 views
in Technique[技术] by (71.8m points)

java - Can i configure validation in xxxx-validation.xml for 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;
  ...
}

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>
....

Can I define this kinda of configuration for Part too?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use the Visitor Validator;

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

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 will take care of validating each element of the List by using this second file.

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).


EDIT

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...