What are converters?

When sending a request, VRaptor tries to read the parameters and set the values you annotated with the @Parameter. To achieve that task, VRaptor must convert these parameters to the java object, because any parameter is sent as String.

For example, if you read a parameter person.id=10, and your model class looks like:

public classe Person {

        private Integer id;
        //more fields

        public void setId(Integer id) {
                ...
        }

        //other getters and setters
}

... VRaptor has to convert the String value 10 to an integer. That's for different basic converters are installed by default.

Basic converters

Some basic converters are included in the .jar file ...

Those converters can translate Strings into:

  1. boolean, Boolean
  2. char, Character
  3. float, Float, double, Double (locale based)
  4. byte, Byte, short, Short, int, Integer, long, Long
  5. String (of course)
  6. BigDecimal
  7. java.util.Calendar, java.util.Date (locale based)

Conversion error

The standard way to throw conversion exceptions is throwing a ConversionException where its first argument is a key to what has hapenned.

After trying to inject all parameters, VRaptor collects all ConversionExceptions which were thrown and uses them to set up the errors variable (see validation) as this is a validation issue.

If any of such exception's is thrown, the default behaviour of "invalid validation" happens: the logic returns "invalid" and goes to the invalid page (see validation documentation to check how to redirect the user to the same form).

Default conversion exception keys

Check the default converters javadoc to see which keys they use for their error messages.

Those keys should not change between compatible VRaptor's releases.

There are two important error messages which the user should be aware ok:

  1. no_converter_found = No converter was found to cope with the select type.
  2. invalid_converter = Unable to instantiate converter (no default constructor, for example).

LocaleCalendarDateConverter

The LocaleCalendarDateConverter is the locale based converter which is installed by default.

It searches for an user locale at several scopes using the JSTL defined key javax.servlet.jsp.jstl.fmt.locale. First it tries the request scope, then session scope, then application scope (servlet context).

If none is found, it looks for a servlet init parameter (configured on web.xml) and finally it gets the locale from the request (the locale configured on the user's browser).

In order to create a init parameter to your default locale, add the following to your web.xml:

<context-param>
        <param-name>javax.servlet.jsp.jstl.fmt.locale</param-name>
        <param-value>en_US</param-value>
</context-param>

Important: the JSTL key differs on the different scopes. For request scope, the key should be javax.servlet.jsp.jstl.fmt.locale.request. For session scope javax.servlet.jsp.jstl.fmt.locale.session and for application scope javax.servlet.jsp.jstl.fmt.application.

Other converters

  1. FixedPatternCalendarConverter