Type Conversion

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

...

Application-wide converters can be specified in a file called xwork-convertersconversion.properties located in the root of the classpath.

...

For an example, see the following two classes:

Code Block
borderStylesolid
titleMyAction.java
borderStylesolid
/**
 * @return a Collection of Foo objects
 */
public Collection getFooCollection()
{
    return foo;
}
Code Block
borderStylesolid
titleFoo.java
borderStylesolid
/**
 * @return a unique identifier
 */
public Long getId()
{
    return id;
}

...

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

Code Block
borderStylesolid
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
borderStylesolid
titleMyBeanAction.java
borderStylesolid
public 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
borderStylesolid
titleMyBeanAction-conversion.properties
borderStylesolid
KeyProperty_beanList=id
Element_beanList=MyBean
CreateIfNull_beanList=true

...

  • The List does not have null values added for unavailable id values. This approach avoids the risk of OutOfMemoryErrors!
Code Block
borderStylesolid
titleMyBeanAction.jsp
borderStylesolid
<s:iterator value="beanList" id="bean">
  <stextfield name="beanList(%{bean.id}).name" />
</s:iterator>

...