如何集中< f:convertDateTime> JSF中的时区? [英] How to centralize <f:convertDateTime> timezone in 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>
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>类,如下所示,以便获得具有所有属性的转换器,但是如果不将其包装在自定义标记中(则需要
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" />
这篇关于如何集中< f:convertDateTime> JSF中的时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!