Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format the date using <f:convertDateTime> and display it in the <h:outputText>

Tags:

jsf

I want to display something like "2010-10-20 by Mary" in the h:outputText. The date value is stored inside the MBean 's field called date1 while the user name is stored inside the MBean 's field called username. I use the following EL expression and UI control:

<h:outputText value="#{MBean.date1} by #{MBean.username}">
   <f:convertDateTime pattern="YYYY-MM-DD" timeZone="#{configMB.timeZone}" />
</h:inputText>

The value can be displayed .However, it ignores the date format specified by f:convertDateTime. No matter how I change the data format, it always display something like "2010-06-08 12:35:22.0 by Mary". How can I solve this problem??

Update :Zenzen 's solution works with the following code changes.

<h:outputFormat value="{0, date, yyyy-MM-dd} by #{1}">
    <f:param value="#{MBean.date1}" />
    <f:param value="#{MBean.username}" />
</h:outputFormat>

However can I format the value of a read-only h:inputText using the method likes h:outputFormat and <f:param>? Sometimes the value displayed is so long and using <h:outputFormat> will generate the span tag which encloses the formatted message .I want to have an effect like <input type="text"> , which the UI control has the fixed length and user can scroll to see the message if the message is too long. Or alternative , how can I format the span tag that make the behavior looks like a <input type="text"> using css or javascript?

like image 366
Ken Chan Avatar asked Aug 18 '10 10:08

Ken Chan


1 Answers

You can do something like this:

<h:outputFormat value="{0, date, yyyy-MM-dd} by #{MBean.username}">
    <f:param value="#{MBean.date1}" />
</h:outputFormat>

I'm sure it will work with value="{0, date, yyyy-MM-dd}" not sure if evertyhing will be ok after adding "by #{MBean.username}" though.

like image 60
Mateusz Dymczyk Avatar answered Sep 22 '22 16:09

Mateusz Dymczyk