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

Compare with Current View Page History

« Previous Version 6 Next »

WebWork has very 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.

(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:

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.

NOTE: Type conversion should not be used as a substitute for i18n. It is not recommended to use this feature to print out properly formatted dates. Rather, you should use the i18n features of WebWork (and consult the JavaDocs for JDK's MessageFormat object) to see how a properly formatted date should be displayed.

  • No labels