Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

datetime - How to centralize <f:convertDateTime> timezone in JSF?

I am using the following JSF converter for my date inputs.

<f:convertDateTime dateStyle="medium" timeZone="EST" type="date" />

I want to centralize the converter so that I can change the timezone or the datestyle in one place. How is that possible?

Can I override the <f:convertDateTime>?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The only way is relying on system default timezone (instead of UTC as internally used by JSF). So, if you have 100% control over the production runtime environment and the production system platform timezone is EST, then just add the following context parameter to web.xml:

<context-param>
    <param-name>javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE</param-name>
    <param-value>true</param-value>
</context-param>

This way JSF will use the system platform timezone as obtained by TimeZone#getDefault() as converter's default timezone instead of UTC.

If you have no control over it, then your best bet is to create an application scoped bean holding that property and reference it instead:

<f:convertDateTime ... timeZone="#{app.timeZone}" />

You can extend the DateTimeConverter class behind <f:convertDateTime> as follows in order to get a converter with all properties already set, but you would not be able to declare additional attributes from the view side on without wrapping it in a custom tag (which requires a TagHandler and some XML boilerplate):

@FacesConverter("defaultDateConverter")
public class DefaultDateConverter extends DateTimeConverter {

    public DefaultDateConverter() {
        setDateStyle("medium");
        setType("date");
        setTimeZone(TimeZone.getTimeZone("EST"));
    }

}

Use it as <f:converter converterId="defaultDateConverter" />


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...