JSF ConvertDateTime 与数据表中的时区
尝试输出数据表中的项目列表,如下所示:
<t:dataTable value="#{mybean.list}" var="item">
<h:column>
<h:outputText value="#{item.time}">
<f:convertDateTime pattern="yyyy-MM-dd HH:mm:ssZ" timeZone="#{item.timeZone}" />
</h:outputText>
</h:column>
</t:dataTable>
它始终以 GMT 格式设置时间。如果我使用字符串常量或不是数据表变量的 bean(如“#{mybean.timeZone}”),它会按预期工作。
Trying to output a list of items in a datatable, like this:
<t:dataTable value="#{mybean.list}" var="item">
<h:column>
<h:outputText value="#{item.time}">
<f:convertDateTime pattern="yyyy-MM-dd HH:mm:ssZ" timeZone="#{item.timeZone}" />
</h:outputText>
</h:column>
</t:dataTable>
It always formats the time in GMT. It works as expected if I use a string constant or a bean which isn't the datatable variable (like '#{mybean.timeZone}').
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,这就是
标签的本质。当要构建视图时,将在实例化转换器的地方构建标签的单个实例。它的所有属性仅被读取和设置一次。在构建视图时,#{item}
解析为null
(它仅在视图渲染期间可用),因此timeZone
code> 属性将为null
,然后默认为 UTC。当要呈现视图时,表的每一行都会重复使用相同的转换器实例。有几种方法可以解决这个问题。我可以想到自定义转换器或 EL 函数。我认为自定义转换器毕竟是最好的,因为它也可以在输入组件中重用。下面的启动示例应该适合您(为简洁起见,空检查和省略):
可以将其用作这样,
每次调用转换器时(而不是在其构造期间)都会解析时区。
更新:OmniFaces
< /a> 恰好解决了这个问题,无需自定义转换器。Unfortunately, that's the nature of
<f:xxx>
tags. When the view is to be built, a single instance of the tag is been built where the converter is instantiated. All of its attribtues are been read and set only once. At the moment the view is been built, the#{item}
resolves tonull
(it's only available during rendering of the view), so thetimeZone
attribute will benull
and then default to UTC. When the view is to be rendered, the very same converter instance is been reused for each row of the table.There are several ways to solve this. I can think of a custom converter or an EL function. I think a custom converter is after all the best as it can then also be reused in input components. The following kickoff example should work out for you (nullchecks and on omitted for brevity):
which can be used as
This way the timezone is resolved everytime the converter is invoked instead of during its construction.
Update: the OmniFaces
<o:converter>
solves exactly this problem without the need for a custom converter.