Versions Compared

Key

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

WebWork has very one of the most advanced type conversion support thanks to XWork. For more details please consult Type Conversion.

A Simple Example

Type conversion is great for situations where you need to turn a String in to a more complex object. Because the web is type-agnostic (everything is a String in HTTP), WebWork's type conversion features are very useful. For instance, if you were prompting a user to enter in coordinates in the form of a String ("3, 22"), you could have WebWork do the conversion both from String to Point and from Point to String.

Panel
bgColor#ffffce

(info) Conversion errors happen all the time (a user enters an invalid date, for example). WebWork handles these through XWork's type conversion error handling.

Using this "point" example, if your action (or another compound object in which you are setting properties on) has a corresponding className-conversion.properties file, WebWork will use the configured type converters for conversion to and from strings. So turning "3, 22" in to new Point(3, 22) is done by merely adding the following entry to ClassName-conversion.properties:

Code Block

point = com.acme.PointConverter
# note: PointerConverter must implement ognl.TypeConverter 
#          or extend ognl.DefaultTypeConverter

Your type converter should be sure to check what class type it is being requested to convert. Because it is used for both to and from strings, you will need to split the conversion method in to two parts: one that turns Strings in to Points, and one that turns Points in to Strings.

After this is done, you can now reference your point (<ww:property value="point"/>) and it will be printed as "3, 22" again. As such, if you submit this back to an action, it will be converted back to a Point once again.

...

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

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

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:

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

Relationship to Parameter Names

Null Property Handling

Collection and Map Support

Type Conversion Error Handling

A Simple Example

Helper base class

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.