Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: added section to show a simple code snippet of how to create a converter and indicate that if a conversion error has occurred and to make struts2 know about it, XWorkException or preferable TypeConversionException should be thrown (suggested by Gregory in WebWork Wiki)

...

  • 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.

Creating a Type Converter

To create a type converter one would need to extends StrutsTypeConverter.

Code Block

 public class MyConverter extends WebWorkConverter {
    public Object convertFromString(Map context, String[] values, Class toClass) {
       .....
    }

    public String convertToString(Map context, Object o) {
       .....
    }
 }
Note

To allow Struts to recognize that a conversion error has occurred, the converter class need to throw XWorkException or preferably TypeConversionException.

Advanced Type Conversion

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

...