Versions Compared

Key

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

Update formatting

Struts Action 2 offers advanced Routine type conversion abilities, perhaps the most advanced type conversion facilities available in any web-based framework in any languagein the framework is transparent. Generally, all you don't need to do anything to take advantage of type conversion, other than give is ensure that HTML inputs have names that can be used in OGNL expressions. (HTML inputs are form elements and other GET/POST parameters.)

A Simple Example

Wiki Markup
{snippet:id=javadoc|javadoc=true|url=com.opensymphony.xwork.util.XWorkConverter}

...

The framework ships with a base helper class that makes simplifies converting to and from Strings very easy. The class is , org.apache.struts.action2.util.StrutsTypeConverter. This The helper class makes it easy to write type converters that handle converting objects to Strings as well as from Strings.

From the JavaDocs:

Wiki Markup
{snippet:id=javadoc|javadoc=true|url=com.opensymphony.webwork.util.WebWorkTypeConverter}

...

The best way to take advantage of the framework's type conversion is to utilize complete objects (ideally your domain objects directly). There is no need to capture form values using intermediate Strings and primitives and then convert those values to full objects in an Action method.

Here are some tips for leveraging the framework's type conversion capabilties:

  • Use complex OGNL expressions - the framework will automatically take care of creating the actual objects for you.
  • Use JavaBeans! The framework can only create objects if the objects obey the JavaBean specification and provide no-arg constructions, as well as getters and setters where appropriate.
  • Remember that person.name will call getPerson().setName(), but if in order for the framework to create the Person object for you, a setPerson() must also exist.
  • Wiki Markup
    For lists and maps, use index notation, such as _people\[0\].name_ or _friends\['patrick'\].name_. Often these HTML form elements are being rendered inside a loop. For [JSP Tags], use the iterator tag's status attribute. For [FreeMarker Tags], use the special property $\{foo_index\}\[\].
  • For multiple select boxes, it isn't possible to use index notation to name each individual item. Instead, name your element people.name and the framework will understand that it should create a new Person object for each selected item and set its name accordingly.

Advanced Type Conversion

The framework also has handles advanced type conversion cases, like null property handling and converting values in Maps and Collections, and type conversion error handling.

Null Property Handling

, yet easy-to-use, type conversion features. Null property handling will automatically create objects where null references are found. Collection and map support provides intelligent null handling and type conversion for Java Collections. Type conversion error handling provides a simple way to distinguish between an input validation problem and an input type conversion problem.

...

Wiki Markup
{snippet:id=javadoc|javadoc=true|url=com.opensymphony.xwork.util.InstantiatingNullHandler}
Wiki Markup
{snippet:id=example|javadoc=true|url=com.opensymphony.xwork.util.InstantiatingNullHandler}

Collection and Map Support

Collection and Map support provides intelligent null handling and type conversion for Java Collections.

The framework supports ways to discover the object type for elements in a collection. The discover is made via an ObjectTypeDeterminer. A default implementation is provided with the framework. The JavaDocs Javadocs explain how map Map and collection Collection support is discovered in the DefaultObjectTypeDeterminer:.

Wiki Markup
{snippet:id=javadoc|javadoc=true|url=com.opensymphony.xwork.util.DefaultObjectTypeDeterminer}

Additionally, you can create your own custom ObjectTypeDeterminer by implementing the ObjectTypeDeterminer interface. There is also an optional ObjectTypeDeterminer that utilizes Java 5 generics. See the J2SE 5 Support page for more information.

Indexing a collection by a property of that collection

It is also possible to obtain a unique element of a collection by passing the value of a given property of that element. By default, the property of the element of the collection is determined in Class-conversion.properties using KeyProperty_xxx=yyy, where xxx is the property of the bean 'Class' that returns the collection and yyy is the property of the collection element that we want to index on.

...

To enable type conversion, put the instruction KeyProperty_fooCollection=id in the MyAction-conversion.properties file. This technique allows use of the idiom fooCollection(someIdValue) to obtain the Foo object with value someIdValue in the Set fooCollection. For example, fooCollection(22) would return the Foo object in the fooCollection collection Collection whose id property value was 22.

This technique is useful, because it ties a collection element directly to its unique identifier. You are not forced to use an index. You can edit the elements of a collection associated to a bean without any additional coding. For example, parameter name fooCollection(22).name and value Phil would set name the Foo object Object in the fooCollection collection Collection whose id property value was 22 to be Phil.

 The The framework automatically converts the type of the parameter sent in to the type of the key property using type conversion.

Wiki Markup
Unlike Map and List element properties, if {{fooCollection(22)}} does not exist, it will not be created. If you would like it created, 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 objectsObjects to {{fooCollection \-\-}} one with name property value {{Phil}} and the other with name property value {{Bar}}. However, 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 causeOtherwise, one element of the null {{id}} properties Foos to be removed from the Set.

...

Type Conversion Error Handling

Type conversion error handling provides a simple way to distinguish between an input validation problem and an input type conversion problem.

Wiki Markup
{snippet:id=error-reporting|javadoc=true|url=com.opensymphony.xwork.util.XWorkConverter}

...