|
|
Note: Simple conversions (int, short date, double, boolean, etc.) are done automatically when you use injection, in other words, when your parameters are being injected in an object automatically by Mentawai. In this scenario, the InjectionFilter and the VOFilter will try to find by reflection the type of the parameter being injected. However, for more complicated conversions and/or to take control of the process if you ever need to, you can define the conversion rules as this tutorial explains.
Type conversion can be useful when you want to provide your actions with ready-to-use Java objects instead of strings. The simplest case is when you want to parse a date string to a java.util.Date object. Although the String-to-Object conversion is the most common in web applications, a converter can deal with any Object-to-Object conversion.
Like Validation, Mentawai has two approaches you can choose when converting action input parameters:
ConversionFilter: The conversion is done by a conversion filter that extends org.mentawai.filter.ConversionFilter. That way the conversion is completely decoupled from the action and can be applied to different actions.
ConverterFilter: The conversion is coded inside the action which can implement the Convertable interface. That way it is not necessary to create a new conversion filter for each action and the conversion logic is placed inside the action it relates to. The conversion is still performed by a filter, but this filter is now a global filter called org.mentawai.filter.ConverterFilter.
You should note that, differently from validation, a conversion does not return a result and does not show an error message to the user in case of failure. If the conversion succeeds, the object is created, otherwise, if the conversion fails for any reason, an org.mentawai.converter.ConversionException is thrown. It is your responsibility to validate the input before trying to convert it, therefore you should use a org.mentawai.filter.ValidationFilter before an ConversionFilter or ConverterFilter , unless you can guarantee somehow that the input can be converted without errors.
ConversionFilter Filter
The first approach is to create a new filter to perform the conversions, so you should create a filter inheriting from org.mentawai.filter.ConversionFilter like the code below demonstrates. (You can also download the war file for this example here)
In the code above we are adding a converter to the field date. Mentawai comes with some pre-built converters in the package org.mentawai.converter and you can also create you own converters for your special needs.
Note that the filter substitutes the String value for the java.util.Date value in the action input, like the code below demonstrates:
You can add your conversion filter in front of the actions you want their input converted to Java objects, like below:
Convertable Interface
Starting in version 1.2.1, Mentawai introduced the org.mentawai.converter.Convertable interface. Basically, although creating a conversion filter will keep your action simpler and will allow you to re-use the conversion schema in different actions, some people have argued that they would rather have everything inside a single class for clarity and also to avoid having to code another class (the conversion filter) for every conversion. They have a valid point.
So you can also place the conversion schema above inside your action if it is implementing the Convertable interface. Check this new approach below:
Although the conversion code is now inside the action, the conversion process itself is still done by a filter. When implementing the Convertable interface, you should use the org.mentawai.filter.ConverterFilter as a global filter. Things you should note about the ConverterFilter:
- This filter will check whether the action implements Convertable and if yes it will initialize the converters by calling the initConverters method and perform the conversion of the action input.
- Don't confuse this filter with the ConversionFilter, which is an abstract class described in the section above.
- You will usually want to use this filter as a global filter, because it will ignore the actions that do not implement Convertable.
- This filter has a performance cache that will cache the converters for each action, in other words, the initConverters method is called only once and then cached for subsequent invocations of the action. The conversion process will be performed with the converters in the cache.
So now for your ApplicationManager you should have:
Creating more converters
You can easily create your own converters by implementing the org.mentawai.converter.Converter interface. You can also use one of the abstract converters that comes with Mentawai:
- BasicConverter: Convert a single input value. (Ex: IntConverter)
- LocaleConverter: Convert a single input value considering its locale. (Ex: DateConverter)
Refer to the API documentation for more details.
|