Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7073239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:54:20+00:00 2026-05-28T05:54:20+00:00

I am trying to validate a list of object on Struts2. Suppose you had

  • 0

I am trying to validate a list of object on Struts2.

Suppose you had a class called INVOICE.
It has 3 properties called: product, quantity, price

And I have a list of INVOICE called INVOICES.

In jsp file, if you iterate over the list , it will generate these inputs.

invoices[0].product
invoices[0].quantity
invoices[0].price
invoices[1].product
invoices[1].quantity
invoices[1].price 

Now after submission, I need to validate these values.

As you can see, Struts2 no longer support collection validation.
http://www.opensymphony.com/webwork/api/com/opensymphony/xwork/validator/validators/CollectionFieldValidator.html

I found a way to do it with annotations. Something like

@RequiredStringValidator(message="Product name is required")
public void setProduct(String product) {
    this.product = product;
}

But I am not allowed to put validation annotations on bean class due to coding standard of the project. Because I need to keep MESSAGES on properties file.

I read that I could do this validation with VisitorValidation as mentioned here.
http://struts.apache.org/2.0.11.2/docs/using-visitor-field-validator.html

But I couldn’t understand how to do this by seeing those examples.

Is there any way to do it with using only validation XMLs? Thank you

ANSWER:

I did it with VisitorFieldValidator, and I still had problems as I commented on correct answer.

The problem went away when I put MyModel-validation.xml in my model package.

As the correct answer highlighted:

manage your validations for your models in one place, where they belong, next to your model classes.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:54:21+00:00Added an answer on May 28, 2026 at 5:54 am

    Indeed visitor field validation would be your bet bet. Struts2 documentation is not famous as being the most comprehensive, but you can get more information on the visitor field validator here, which includes a simple example.

    The VisitorFieldValidator allows you to forward validation to object properties of your action using the object’s own validation files. This allows you to use the ModelDriven development pattern and manage your validations for your models in one place, where they belong, next to your model classes. The VisitorFieldValidator can handle either simple Object properties, Collections of Objects, or Arrays.

    In your action, you can place validation for the invoices collection as:

    <validators>
        <field name="invoices">
            <field-validator type="visitor">
                <message></message>
            </field-validator>
        </field>
    </validators>
    

    Then simply proceed to create a Invoice-validation.xml which would hold the validation logic for your Invoice bean and place it along with the Invoice class. For example:

    <validators>
        <field name="product">
            <field-validator type="requiredstring">
                <message>product is required.</message>
            </field-validator>
        </field>
    </validators>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to validate an ID. I have this class called ManejadorTickets in which
I'm trying to validate a field with comma separated list of strings(particular form) or
I have a List<> of custom objects. This custom type has a property called
So basically I am trying to validate my drop down list (combobox) using the
Trying to validate a comma-separated email list in the textbox with asp:RegularExpressionValidator , see
I'm trying to validate a list of email addresses using the following code: Public
I am trying to implement an interface class, that contains a list of objects.
I'm having problems trying to validate a drop down list, I've looked at similar
When trying to validate my site, I get the following error: Line 188, column
While trying to validate my forms i get the following error: Expected a {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.