Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Wiki Markup
Unlike Map and List element properties, if fooCollection(22) does not exist it will not be created. To do that, use the notation *fooCollection.makeNew\[index\]* where index is an integer 0, 1, and so on. Thus, parameter value pairs *fooCollection.makeNew\[0\]=Phil* and *fooCollection.makeNew\[1\]=John* would add two new Foo objects to fooCollection one with name property value Phil and the other with name property value Bar. Note, however, that in the case of a Set, the equals and hashCode methods should be defined such that they don't only include the id property. That will cause one element of the null id propertied Foos to be removed from the Set.

...

An advanced example for indexed Lists and Maps

Here is the model bean used within the list.
The KeyProperty for this bean is the id attribute.

Code Block
titleMyBean.java
borderStylesolid
public class MyBean implements Serializable {

    private Long id;
    private String name;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public String toString() {
        return "MyBean{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

The action has a beanList attribute initialized with an empty ArrayList.

Code Block
titleMyBeanAction.java
borderStylesolid
ublic class MyBeanAction implements Action {

    private List beanList = new ArrayList();
    private Map beanMap = new HashMap();

    public List getBeanList() {
        return beanList;
    }

    public void setBeanList(List beanList) {
        this.beanList = beanList;
    }

    public Map getBeanMap() {
        return beanMap;
    }

    public void setBeanMap(Map beanMap) {
        this.beanMap = beanMap;
    }

    public String execute() throws Exception {
        return SUCCESS;
    }
}

These conversion.properties tell the TypeConverter to use MyBean instances as elements of the List.

Code Block
titleMyBeanAction-conversion.properties
borderStylesolid
KeyProperty_beanList=id
Element_beanList=com.opensymphony.xwork.util.MyBean
CreateIfNull_beanList=true

Key_beanMap=java.lang.Long
KeyProperty_beanMap=id
Element_beanMap=com.opensymphony.xwork.util.MyBean

When submitting this via a form, the (id) value is used as KeyProperty for the MyBean instances in the beanList.
Notice the () notation! Do not use [] notation, this is for Maps only!
The value for name will be set to the MyBean instance with this special id.
The List does no longer have null values added for unavailable id values.
This avoids the risk of OutOfMemoryErrors!

Code Block
titleMyBeanAction.jsp
borderStylesolid

<ww:iterator value="beanList" id="bean">
  <ww:textfield name="beanList(%{bean.id}).name" />
</ww:iterator>

Type Conversion Error Handling

...