在 JSF 页面上显示当前日期 [英] Display Current Date on JSF Page

查看:20
本文介绍了在 JSF 页面上显示当前日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在不使用支持 bean 的情况下在 JSF 中显示当前日期(今天的)?

Is it possible to display the current date (today's) in JSF without using a backing bean?

我有以下代码片段,但没有成功.

I have the following code snippet , but it didn't work out.

<div class="leftSide">Today's date #{currentDate}</div>

<f:facet name="header">  
<h:outputText value="Today's date" />  
</f:facet>
<h:outputText value="#currentDate">
    <f:convertDateTime pattern="MM/dd/yyyy" type="date" />
</h:outputText>

推荐答案

您可以将 java.util.Date 的实例注册为 faces-config.xml.

You could register an instance of java.util.Date as a request scoped bean in faces-config.xml.

<managed-bean>
    <managed-bean-name>currentDate</managed-bean-name>
    <managed-bean-class>java.util.Date</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

这样它就可以作为 #{currentDate} 使用,而无需自定义支持 bean 类.

This way it's available as #{currentDate} without the need for a custom backing bean class.

更新:JSF 实用程序库 OmniFaces 有这样一个 bean 已注册#{now}.因此,如果您碰巧已经使用 OmniFaces,则可以直接使用它.

Update: the JSF utility library OmniFaces has such a bean already registered as #{now}. So if you happen to use OmniFaces already, you can just make use of it directly.

<h:outputText value="#{now}">
    <f:convertDateTime pattern="MM/dd/yyyy" type="date" />
</h:outputText>

这篇关于在 JSF 页面上显示当前日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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