You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 18 Next »

WebWork has one of the most advanced type conversion abilities in any web-based framework in any Java language. Generally, you don't need to do anything to take advantage of it, other than name your HTML inputs (form elements and other GET/POST parameters) names that are valid OGNL expressions.

A Simple Example

Error formatting macro: snippet: java.lang.NullPointerException
Error formatting macro: snippet: java.lang.NullPointerException

WebWork ships with a helper base class that makes converting to and from Strings very easy. The class is com.opensymphony.webwork.util.WebWorkTypeConverter. This class makes it very easy for you to write type converters that handle converting objects to Strings as well as from Strings. From the JavaDocs for this class:

Error formatting macro: snippet: java.lang.NullPointerException

Built in Type Conversion Support

Error formatting macro: snippet: java.lang.NullPointerException

Relationship to Parameter Names

The best way to take advantage of WebWork's type conversion is to utilize complete objects (ideally your domain objects directly), rather than submitting form values on to intermediate primitives and strings in your action and then converting those values to full objects in the execute() method. Some tips for achieving this are:

  • Use complex OGNL expressions - WebWork will automatically take care of creating the actual objects for you.
  • Use JavaBeans! WebWork can only create objects for you if your 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 you are expecting WebWork to create the Person object for you, a setPerson() must also exist.
  • 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, so you can use the iterator tag's status attribute if you're using JSP Tags or the ${foo_index} special property if you're using FreeMarker Tags.

  • FOr multiple select boxes, you obviously can't name each individual item using index notation. Instead, name your element simply people.name and WebWork will understand that it should create a new Person object for each selected item and set it's name accordingly.

Advanced Type Conversion

WebWork also has some very advanced, 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 an easy way to distinguish the difference between an input validation problem from an input type conversion problem.

Null Property Handling

Error formatting macro: snippet: java.lang.NullPointerException
Error formatting macro: snippet: java.lang.NullPointerException

Collection and Map Support

WebWork supports ways to determine the object type found in collections. This is done via an ObjectTypeDetermine. The default implementation is provided. The JavaDocs explain how map and colelction support is determined:

Error formatting macro: snippet: java.lang.NullPointerException

There is also an optional ObjectTypeDeterminer that utilizes Java 5 generics. See the J2SE 5 Support page for more information.

Type Conversion Error Handling

Error formatting macro: snippet: java.lang.NullPointerException

There are two ways the error reporting can occur:

  1. globally, using the Conversion Error Interceptor
  2. on a per-field basis, using the Conversion Field Validator

By default, the conversion interceptor is included in webwork-default.xml in the default stack, so if you don't want conversion errors reporting globally, you'll need to change the interceptor stack and add additional validation rules.

  • No labels