user2361862
user2361862

Reputation: 95

jsf output text with fn substring

I want to format a date in jsf which is xmlgregoriancalendar type. I've come across post which say I need custom converter. Does anybody found solution which does not require custom date converter. I was trying following but I gives me error saying...

Element type "h:outputText" must be followed by either attribute specifications, ">" or "/>".

This is what I tried on jsf

<h:outputText value="#{fn:substringBefore(aClip.lastTransmittedDate,"T")}">
     <f:convertDateTime pattern="dd.MM.yyyy" />
</h:outputText>

Can anybody point out the explain the error I'm getting?

Upvotes: 0

Views: 3799

Answers (2)

BalusC
BalusC

Reputation: 1108722

Apart from your EL syntax error (basically, with that nested double quote you're ending the attribute value too soon that it becomes value="#{fn:substringBefore(aClip.lastTransmittedDate," which is completely wrong EL syntax), this is after all absolutely not the right approach.

The <f:convertDateTime> converts Date to String and vice versa. It does not convert String in date pattern X to String in date pattern Y at all as you incorrectly seemed to expect.

Given a XMLGregorianCalendar, you need XMLGregorianCalendar#toGregorianCalendar() to get a concrete java.util.Calendar instance out of it which in turn offers a getTime() method to get a concrete java.util.Date instance out of it, ready for direct usage in <f:convertDateTime>.

Provided that your environment supports EL 2.2, this should do:

<h:outputText value="#{aClip.lastTransmittedDate.toGregorianCalendar().time}">
    <f:convertDateTime pattern="dd.MM.yyyy" />
</h:outputText>

Or, if your environment doesn't support EL 2.2, then change the model in such way that you have for example a getter returning java.util.Calendar:

public Calendar getLastTransmittedDateAsCalendar() {
    return lastTransmittedDate.toGregorianCalendar();
}

so that you can use

<h:outputText value="#{aClip.lastTransmittedDateAsCalendar.time}">
    <f:convertDateTime pattern="dd.MM.yyyy" />
</h:outputText>

You can even add a getter returning the concrete java.util.Date.

Upvotes: 4

GKlimov
GKlimov

Reputation: 409

Actually this error points that you have not well-formed XML. In EL you should use single quotes to indicate String. In your case value="#{fn:substringBefore(aClip.lastTransmittedDate,'T')}"

What about date converter, you should see on the type of #{aClip.lastTransmittedDate} property. If it is date, just use

<h:outputText value="#{aClip.lastTransmittedDate}">
    <f:convertDateTime pattern="dd.MM.yyyy" />
</h:outputText>

If it is not date (i.e. String), <f:convertDateTime/> won't work, and you should reformat it in Java code. For example, create another getter that will return modified string representing property value.

Upvotes: 1

Related Questions