views:

469

answers:

1

I am trying to register different converter instances in the faces-config, using a standard converter class to which different parameters are passed.

The code below registers two DateTimeConverters, the first one for dates including time and the second one for time only. But the pattern property never gets set. Can this be done?

<converter>
    <converter-id>dateTimeConverter</converter-id>
    <converter-class>javax.faces.convert.DateTimeConverter</converter-class>
    <property>
        <property-name>pattern</property-name>
        <suggested-value>yyyy-MM-dd HH:mm:ss</suggested-value>
    </property>
</converter>

<converter>
    <converter-id>timeConverter</converter-id>
    <converter-class>javax.faces.convert.DateTimeConverter</converter-class>
    <property>
        <property-name>pattern</property-name>
        <suggested-value>HH:mm:ss</suggested-value>
    </property>
</converter>
+3  A: 

This is unfortunately not possible through faces-config.xml. The <property> declaration which you're trying is not used during runtime.

If all you want is to control the pattern at one place, then best what you can do is to create a custom converter. For this particular purpose it isn't that hard. Just extend DateTimeConverter and set the pattern during construction. Here's a basic example:

public MyDateTimeConverter extends DateTimeConverter() {
    public MyDateTimeConverter() {
        setPattern("yyyy-MM-dd HH:mm:ss");
    }
}

You can of course get the pattern from somewhere else, e.g. a properties file or xml file in classpath.

Register this converter as follows:

<converter>
    <converter-for-class>java.util.Date</converter-for-class>
    <converter-class>com.example.MyDateTimeConverter</converter-class>
</converter>

That should be it. No need for f:converter or UIOutput#setConverterId().

BalusC
Thanks for your explanation. But what is the use of these properties in faces-config.xml? (the property tag is part of the xsd).
Jurgen H