Spring Web Flow Converter

738 Views Asked by At

I have a Spring MVC and Web Flow 2.3 application.

All I would like to know is how you have a select box in SWF with some default value say, for example, 'Please Select' that is not bound to any value in the backing list.

In line with the docs I have created a conversion service:

public class ApplicationConversionServiceFactoryBean extends FormattingConversionServiceFactoryBean {

        // formatters
        registry.addFormatter(...);

        // converters
        registry.addConverter(...);

}

This all works unless I want to perform what would seem to be a perfectly simple task of having 'Please Select' in a select box.

A formatter cannot return null from its Parse Method so you cannot use that. Switching from a formatter to a one-way converter (String > Object) fixes then issue in the MVC stuff however the SWF still complains about missing converter for Object > String. Using this setup do I actually need to create another converter implementation for Object > String (essentially have two converters for every conversion).

Yes, there are other converters TwoWayConverter, ObjectToString etc. etc. however I do not see that these can be added in the above as they are all all the wrong type to be added using:

registry.addConverter(...);

The documentation around conversion is confusing to say the least:

http://docs.spring.io/spring-webflow/docs/2.3.x/reference/htmlsingle/spring-webflow-reference.html#converter-options

1

There are 1 best solutions below

0
On

You can do atleast adding binding null value or empty string to your model. So you can use default value for select one menu. Like this:

<sf:option value="">Please select...</sf:option>

But in your Formatter or Converter, you should check your model property is null or empty String and if so return null or empty String as you like. Like this:

public YourEnum parse(String value, Locale local) throws ParseException {
    if(YourEnum.valueOf(YourEnum.class, value.toUpperCase()) == YourEnum.VALUEONE) {
        return YourEnum.VALUEONE;
    } else if(YourEnum.valueOf(YourEnum.class, value.toUpperCase()) == YourEnum.VALUETWO) {
        return YourEnum.VALUETWO;
    } else {
        return null;
    }
}