jsf output text with fn substring - jsf

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?

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.

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.

Related

Type-appropriate searching in dropdowns and search boxes in frontend

I have used custom converter classes with annotations like #FacesConverter(forClass = Date.class) to override the JSF default string representation of Date and Boolean objects in the frontend. I have two problems with this:
Date works as expected – all Date objects have a consistent representation in the frontend. However, Boolean works only if I change the annotation to #FacesConverter("mypackage.presentation.BooleanFormatConverter") and then explicitly invoke it in my xhtml, each time I want to use it, as <f:converter converterId="mypackage.presentation.BooleanFormatConverter" />.
I am changing the string representation of booleans to Yes/No, and I have filter boxes on boolean columns on my datatable with those string values. However, it does not seem to recognise that the string values correspond to the boolean true/false. I don't know how to fix this without explicitly changing the type of the booleans to string in the backend, then treating those as string columns in my datatable (and for other reasons, I'd rather keep types distinct and avoid explicitly converting everything to a string in the backend).
Quite simple in the end, following Kukeltje's suggestion: a boolean in the backing bean (which does not need to be done per-column) and changing the xhtml:
<p:selectCheckboxMenu value="#{myView.yesNoCheckbox}" label="Select" onchange="PF('tableE').filter()" scrollHeight="150" filter="true" filterMatchMode="contains">
<f:selectItem itemValue="#{true}" itemLabel="Yes"/>
<f:selectItem itemValue="#{false}" itemLabel="No" />
</p:selectCheckboxMenu>
the only disadvantage is that this part must be done manually per column (my datatable has an obscene number of columns, which is why I wanted a simple global way of doing this, such as a converter).

JSF outputText and PrimeExtensions inputNumber [duplicate]

I am using JSF 2 and RichFaces 3. Here in the picture shown below, numbers are being displayed as what they are in the database.
But I want to display them as 6749395.20 if fraction part is there and 5095138.00 if no fraction part is there.
As of now I have tried something like this.
<rich:column>
<f:facet name="header">
<h:outputText value="Total Amount"/>
</f:facet>
<h:outputText value="#{rr[2]}">
<f:convertNumber type="number" groupingUsed="true" minFractionDigits="2" pattern="#0.00"/>
</h:outputText>
</rich:column>
Actually I am showing all of them together, but I have tried with all of them as all possible combinations with type, groupingUsed, minFractionDigits and pattern.
Why does it not work? How is this caused and how can I solve it?
That can happen if the value is not a Number at all, for example a String. You're then basically using the wrong type for the data it represents. To represent currencies in Java, you should be using BigDecimal. Also, make sure that the type in the database table is right, i.e. it should not be a varchar, but a decimal.
Once you've fixed the data type, then the <f:convertNumber> will work as you told it to do. Note that the pattern attribute will override the groupingUsed and minFractionDigits. You should use either the pattern or the others. Also, type="number" is already the default, so it can be removed.
So, either use
<f:convertNumber pattern="#0.00" />
or
<f:convertNumber groupingUsed="true" minFractionDigits="2" />
Note that they generate different formats. You probably want to set grouping to false.
You can also use type="currency", it will then automatically apply the right pattern as per the UIViewRoot#getLocale():
<f:convertNumber type="currency" />
See also the tag library documentation and the DecimalFormat javadoc.

Display amount in format $###,###,###.## using f:convertNumber

I would like to display the amount in $12,050,999.00 format.
I tried as follows:
<h:outputText value="#{sampleBean.Amount}">
<f:convertNumber pattern="###,###" currencySymbol="$" type="currency"/>
</h:outputText>
However, it didn't display the amount in the desired format. I got 12,050,999 instead.
The desired format is shown in the below image:
How can I achieve this?
Your pattern is wrong for a currency. You should be using pattern="¤#,##0.00".
<f:convertNumber pattern="¤#,##0.00" currencySymbol="$" />
However, there's more at matter: in your original code you also specified the type attribute, which is correct, but this is mutually exclusive with the pattern attribute whereby the pattern attribute gets precedence.
You should actually be omitting the pattern attribute and stick to the type attribute.
<f:convertNumber type="currency" currencySymbol="$" />
Note that this uses the locale as available by UIViewRoot#getLocale() which is expected to be an English/US based locale in order to get the right final format for the USD currency. You'd like to explicitly specify it in either the <f:view>:
<f:view locale="en_US">
or in the locale attribute of the <f:convertNumber>:
<f:convertNumber type="currency" currencySymbol="$" locale="en_US" />
See also:
Does <f:convertNumber> use the right number separator when using patterns to format currency?

Format a JSF converted value

Today I noticed ocpsoft has a nice time library adapted to use in JSF 2 as a converter. The strong point of that is you can use the converter directly in the date displayed in the view and it converts it into a string telling the user something like 6 hours ago or 17 hours from now. I think my best is to combine both, the JSF converted date and this one to display something like 26-03-2013 17:00 (4 hours from now). I can achieve something similar with the following code:
<h:outputText value="#{task._StartDate}" style="padding:2px;">
<f:convertDateTime pattern="dd-MM-yyyy HH:mm" timeZone="GMT+1" />
</h:outputText>
<h:outputText value="#{task._StartDate}">
<f:converter converterId="org.ocpsoft.PrettyTimeConverter" />
</h:outputText>
My problem comes when I want to put the second value into parenthesis. The PrettyTimeConverter accepts only a date as a value and I can't write the parenthesis there directly. Also JSF is not accepting the following:
<h:outputFormat value="({0})">
<f:param value="#{task._StartDate}">
<f:converter converterId="org.ocpsoft.PrettyTimeConverter" />
</f:param>
</h:outputFormat>
With that I have the following error:
<f:converter> Parent not an instance of ValueHolder:
javax.faces.component.UIParameter#1492636
Any idea about how to achieve that avoiding writing both parentheses using specific h:outputText tags?
You can just put those parentheses directly in template text without the need for another <h:outputText>s.
<h:outputText value="#{task._StartDate}" style="padding:2px;">
<f:convertDateTime pattern="dd-MM-yyyy HH:mm" timeZone="GMT+1" />
</h:outputText>
(<h:outputText value="#{task._StartDate}">
<f:converter converterId="org.ocpsoft.PrettyTimeConverter" />
</h:outputText>)
See also:
Is it suggested to use h:outputText for everything?

JSF. Convert value for h:commandLink display

I am passing Status object to h:commandLink value. So it is displayed on the page. The problem is, displayed string is
packages.entity.Status#db2674c8.
I created converter for Status with annotation
#FacesConverter(forClass = Status.class, value = "statusConverter")
but it doesn't work. I tried to explicitly set it:
<h:commandLink value="#{result.status}" action="/view">
<f:converter converterId="statusConverter" />
</h:commandLink>
Then I got an error: /search-form.xhtml #57,58 <f:converter> Parent not an instance of ValueHolder: javax.faces.component.html.HtmlCommandLink#53e387f3
which is quite true, h:commandLink is not ValueHolder. Is there some way to convert value for h:commandLink?
Interesting, I'd intuitively expect it to work here, but the UICommand does indeed not extend UIOutput (while the UIInput does). It's maybe worth an enhancement request to JSF boys.
You can go around this issue by displaying it using <h:outputText>.
<h:commandLink action="/view">
<h:outputText value="#{result.status}">
<f:converter converterId="statusConverter" />
</h:outputText>
</h:commandLink>
Or just without explicit <f:converter> since you already have a forClass=Status.class
<h:commandLink action="/view">
<h:outputText value="#{result.status}" />
</h:commandLink>
Converters can not be attached to command components (h:commandLink, h:commandButton)
You could create a composite component or use a method in your backing bean for that.
As you pointed out an h:commandLink is not a ValueHolder so it does not support a converter. The value attribute actually dictates the text that is displayed.
Converters are used to convert a value that is an Object into a String for representation in html and then on the flip side to convert that String back into an instance of an object.
In your example I'm guessing that result.status is an object which you'd like to convert to a string? If so you may just need to reference an actual String attribute of the object, like:
<h:commandLink value="#{result.status.statusMessage}" action="/view" />

Resources