Okay, so thanks to your suggestion this is what I end up doing, good news is it seems to work, too! :) To successfully make Struts validate the collections inside the collection, just use a "recursive" visitor validation on fields, making the first visitor "point" to the second and so on: 1) BlahBlahClass-validation.xml (field name now points to the collection containing the other collections, foo.bars, and NOT foo.bars.properties as it was before): <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd "> <validators> <!-- Field-Validator Syntax --> <field name="foo.bars"> <field-validator type="visitor"> <param name="appendPrefix">false</param> <param name="context">myContext</param> <message/> </field-validator> </field> </validators>
2) wrote a brand new Bar-myContext-validation.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd"> <validators> <!-- Field-Validator Syntax --> <field name="properties"> <field-validator type="visitor"> <param name="appendPrefix">false</param> <param name="context">myContext</param <message /> </field-validator> </field> </validators> 3) renamed last xml file to Property-myContext-validation.xml, withsame content: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd "> <validators> <field name="propId"> <field-validator type="requiredstring"> <message key="required"/> </field-validator> </field> </validators> Thank you! Ciao, Andrea On Tue, Apr 9, 2013 at 12:24 PM, Martin Gainty <mgai...@hotmail.com> wrote: > when field-validator-type is visitor validation happens at the outermost > enclosing class > > if you want to validate the individual attributes of the enclosing class > you will need to specify context and append that context to each attr e.g. > > <!-- Field Validator Syntax --> > <field name="property"> > <field-validator type="visitor"> > <param name="context">myContext</param> > <param name="appendPrefix">true</param> > </field-validator> > </field> > > action class getProperty() can now reference the individual attributes of > property e.g. > property.propId > > XWork will look for Property-myContext-validation.xml for the validators > > http://struts.apache.org/release/2.0.x/docs/visitor-validator.html > > HTH > > Martin > ______________________________________________ > Verzicht und Vertraulichkeitanmerkung/Note de déni et de confidentialité > > Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene > Empfaenger sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte > Weiterleitung oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht > dient lediglich dem Austausch von Informationen und entfaltet keine > rechtliche Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von > E-Mails koennen wir keine Haftung fuer den Inhalt uebernehmen. > Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le > destinataire prévu, nous te demandons avec bonté que pour satisfaire > informez l'expéditeur. N'importe quelle diffusion non autorisée ou la copie > de ceci est interdite. Ce message sert à l'information seulement et n'aura > pas n'importe quel effet légalement obligatoire. Étant donné que les email > peuvent facilement être sujets à la manipulation, nous ne pouvons accepter > aucune responsabilité pour le contenu fourni. > > > > > > Date: Tue, 9 Apr 2013 11:13:50 +0200 > > Subject: Fwd: Visitor field validation > > From: atsizza.k...@gmail.com > > To: user@struts.apache.org > > > > Hello everyone! > > First of all, I'm completely new to the Struts framework, so please > forgive > > me if my question is dumb or something. > > I'm currently working on an existing EE application which uses Struts 2.x > > for managing the frontend. The problem/doubt I have is the following. > > Suppose I have a "backing bean" containing a Foo object which in turn > > contains a collection of Bar objects, i.e: > > public class BlahBlahClass extends ActionSupport { > > private Foo foo = new Foo(); > > [...] > > > > public Foo getFoo() > > { return foo; } > > > > public void setFoo(Foo foo) > > { this.foo = foo; } > > } > > > > public class Foo implements Serializable { > > private List<Bar> bars = new ArrayList<Bar>(); > > > > [...] > > > > public List<Bar> getBars() > > { return bars; } > > > > public void setBars(List<Bar> bars) > > { this.bars = bars; } > > } > > > > public class Bar implements Serializable { > > private String objId = new String(); > > private Property property1 = new Property(); > > > > [...] > > > > public String getObjId() > > { return objId; } > > public void setObjId(String objId) > > { this.objId = objId; } > > > > public Property getProperty1() > > { return property1; } > > public void setProperty1(Property prop1) > > { this.property1 = prop1; } > > } > > > > public class Property implements Serializable { > > private String propId = new String(); > > > > [...] > > > > public String getPropId() > > { return propId; } > > public void setPropId(String propId) > > { this.propId = propId; } > > } > > > > In my web application, I wanted to validate the actual attributes of Bar > > object, so I did like this: > > 1) I created an xml file named BlahBlahClass-validation.xml with > following > > content: > > <?xml version="1.0" encoding="UTF-8"?> > > <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator > > 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd"> > > <validators> > > <!-- Field-Validator Syntax --> > > <field name="foo.bars"> > > <field-validator type="visitor"> > > <param name="appendPrefix">false</param> > > <message/> > > </field-validator> > > </field> > > </validators> > > 2) I created another xml file named Bar-validation.xml with following > > content: > > <?xml version="1.0" encoding="UTF-8"?> > > <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator > > 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd"> > > <validators> > > <field name="objId"> > > <field-validator type="requiredstring"> > > <message key="required"/> > > </field-validator> > > </field> > > <field name="property1.propId"> > > <field-validator type="requiredstring"> > > <message key="required"/> > > </field-validator> > > </field> > > </validators> > > > > So far so good, because Struts works exactly like I thought, and if user > > doesn't insert the required ids, validation fails. > > Now imagine I have another "backing bean", but this time with > > a collection attribute in the Bar object, so that instead of a collection > > of objects Foo now in the end contains a collection of collections , > i.e.: > > > > public class Bar implements Serializable { > > private String objId = new String(); > > private List<Property> properties = new ArrayList<Property>(); > > > > [...] > > > > public String getObjId() > > { return objId; } > > public void setObjId(String objId) > > { this.objId = objId; } > > > > public List<Property> getProperties() > > { return properties; } > > public void setProperties(List<Properties> props) > > { this.properties = props; } > > } > > > > I wanted to validate the attributes of every Property contained in the > Bar > > collection of Foo, so the 2 xml files I wrote are: > > 1) BlahBlahClass-validation.xml with following content: > > <?xml version="1.0" encoding="UTF-8"?> > > <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator > > 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd"> > > <validators> > > <!-- Field-Validator Syntax --> > > <field name="foo.bars.properties"> > > <field-validator type="visitor"> > > <param name="appendPrefix">false</param> > > <message/> > > </field-validator> > > </field> > > </validators> > > > > 2) Property-validation.xml with following content: > > <?xml version="1.0" encoding="UTF-8"?> > > <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator > > 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd"> > > <validators> > > <field name="propId"> > > <field-validator type="requiredstring"> > > <message key="required"/> > > </field-validator> > > </field> > > </validators> > > > > But this way Struts doesn't correctly validate the propId of Property > > because I think it's actually unable resolve the bars collection index > from > > the <field name="foo.bars.properties"> part. Am I right? If so, is there > a > > way to tell Struts to correctly traverse a collection inside another > > collection? > > > > Thank you and sorry for my long post! :) > > Andrea > >