如何集中< f:convertDateTime> JSF中的时区? [英] How to centralize <f:convertDateTime> timezone in JSF?

查看:147
本文介绍了如何集中< f:convertDateTime> JSF中的时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将以下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?

我可以覆盖<f:convertDateTime>吗?

推荐答案

唯一的方法是依靠系统默认时区(而不是JSF内部使用的UTC).因此,如果您对生产运行时环境具有100%的控制权,并且生产系统平台时区为EST,则只需将以下上下文参数添加到web.xml:

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>

通过这种方式,JSF将使用由

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

如果您无法控制它,那么最好的选择是创建一个拥有该属性的应用程序范围的bean并引用它:

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}" />

可以扩展 DateTimeConverter<f:convertDateTime>之后的/a>类,如下所示,以便获得具有所有属性的转换器,但是如果不将其包装在自定义标记中(则需要和一些XML样板):

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"));
    }

}

用作<f:converter converterId="defaultDateConverter" />

这篇关于如何集中&lt; f:convertDateTime&gt; JSF中的时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆