Working with custom value types in metawidget

70 Views Asked by At

Imagine the following form/bean:

public class MyForm {

    private BankAccountNumber accountNumber;

    // getter + setter
}

where BankAccountNumber is a custom value object.

When I use metawidget and jsf with an instance of this class, no input field is generated for the accountNumber property (only the label is shown).

The problem is that metawidget does not know the BankAccountNumber type and wouldn't know what kind of UIInput should be used.

The only solution I found so far was to annotate my property with @UiAttribute(name = "type", value = "java.lang.String") and to have a javax.faces.Converter registered for the BankAccountNumber class. That way, metawidget generates an HtmlnputText.

Is it the best solution?

I think I could also register my own InspectionResultProcessor to change BankAccountNumber to String everywhere but I don't even know if mapping my custom type to String is the right way to do it in the first place.

1

There are 1 best solutions below

0
On BEST ANSWER

The recommended approach is to register your own WidgetBuilder to match on an attribute type of BankAccountNumber and return the appropriate widget. You can return null for every other type, and rely on using a CompositeWidgetBuilder to have the widget building process 'fall through' to a regular HtmlWidgetBuilder for most types.

Quite what sort of UIInput you return for a type of BankAccountNumber is up to you. But if you use a HtmlInputText then you'll probably need to register JSF Converters in the usual way (this bit isn't specific to Metawidget) so that JSF knows how to convert from HtmlInputText into a BankAccountNumber.

For an example of registering a custom WidgetBuilder (and combining it with a CompositeWidgetBuilder), see section 2.4.4 and 2.4.5 of the User Guide: http://metawidget.org/doc/reference/en/html/ch02s04.html