struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Knuf !" <atsizza.k...@gmail.com>
Subject Fwd: Visitor field validation
Date Tue, 09 Apr 2013 09:13:50 GMT
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

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message