Format a JSF converted value - jsf

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?

Related

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.

Converter can't get rich:dataGrid rows as custom parameters

I need a converter with custom parameters, I've made it and it works well, except when I use it with row elements and I can't understand why, ideas?
Examples
This works well:
<h:outputText value="#{bean.value}">
<cc:converter param="#{bean.attribute}" />
</h:outputText>
also this one:
<h:outputText value="#{bean.value}">
<cc:converter param="fixedValue" />
</h:outputText>
this one doesn't work at all, param is null inside the converter:
<rich:dataGrid value="#{bean.list}" var="row">
<h:outputText value="#{row.value}">
<cc:converter param="#{row.attribute}" />
</h:outputText>
</rich:dataGrid>
See BalusC link. It describes why your converter doesn't work. Converter without parameters works fine inside datatable in JSF 1.2. For simulate conversion you can use getter method in object which represents row like getConvertedValue and move your code from converter into this method. After that in datatable you can call #{row.convertedValue} for displaying the converted value.

How to display java.util.Date in 2013-07-15 22:00:45 format

I have a variable stored in TIMESTAMP format in db. It stores time like this:
2013-07-15 22:00:45
I want to take this and use it in a h:outputText item. I make a database search, and store the results in a resultset.
java.util.Date dt= res.getTimestamp(5);
Then i want to use this date, which was read from database, in an outputtext item. But doing the following does not work:
<h:outputText value="#{list.date}"
Where list is an ArrayList. I am sure that list.date is the date I read from database but I cannot get its value with the format I specified. So how can I do this?
<h:outputText value="#{list.date}">
<f:convertDateTime type="both" pattern="YYYY-MM-dd HH:mm:ss"/>
</h:outputText>
I guess you can use JSF's convertDateTime tag.
e.g. :
<h:outputText value="#{list.date}">
<f:convertDateTime pattern="d-M-yyyy" />
</h:outputText>
See http://www.jsftoolbox.com/documentation/help/12-TagReference/core/f_convertDateTime.html

JSF 2.0: outputText does not print date

I have the following code:
<h:outputText value="#{java.util.Calendar.getInstance().getTime()}">
<f:convertDateTime pattern="MM/dd/yyyy" type="date" />
</h:outputText>
which prints nothing.
When I use the following code:
<h:outputText value="#{group.effectiveDate}">
<f:convertDateTime pattern="MM/dd/yyyy" type="date" />
</h:outputText>
it works. Does anyone know why invoking java.util.Calendar.getInstance().getTime() does not work? I'm using Mojarra.
java.util.Calendar.getInstance().getTime() isn't a valid EL expression. Remember that you can only access beans through EL.
This would either be ones you explicitly create, or ones that are exposed by the container.
when the EL parser sees #{java.util}, it looks for a managed bean named java which has a getUtil() method.
Otherwise, it will probably say something along the lines of "Unable to find managed bean: 'java'".

Formatting a double in JSF

I have a problem similar to the one found here : JSF selectItem label formatting.
What I want to do is to accept a double as a value for my and display it with two decimals. Can this be done in an easy way?
I've tried using but that seems to be applied on the value from the inputText that is sent to the server and not on the initial value in the input field.
My code so far:
<h:inputText id="december" value="#{budgetMB.december}" onchange="setDirty()" styleClass="StandardBlack">
<f:convertNumber maxFractionDigits="2" groupingUsed="false" />
</h:inputText>
EDIT: The above code actually works. I was fooled by JDeveloper that didn't update the jsp page even when I did a explicit rebuild of my project and restarted the embedded OC4J server. However, after a reboot of my computer everything was fine.
If I'm not misunderstanding your requirement, I was able to achieve formatting of the value in the input box during the rendering of the view with:
<h:inputText id="text1" value="#{...}">
<f:convertNumber pattern="#,###,##0.00"/>
</h:inputText>
I was using the Standard Faces Components in my vendor-branded Eclipse so I'm assuming the pattern attribute is part of standard JSF.
If what you are trying to do is make the value of the input text field change on screen (to correct user input), you should probably look into using one of the JSF ajax frameworks like Rich Faces.
A possible example would look like this:
<h:inputText id="december" value="#{budgetMB.december}" styleClass="StandardBlack">
<f:convertNumber maxFractionDigits="2" groupingUsed="false" />
<a4j:support event="onblur" reRender="december" />
</h:inputText>
I haven't tested this, but I think it may work.
It seems you're actually formatting a currency. There already exists a specific formatter to handle currencies that you can assign many options to:
<f:convertNumber type="currency" />
Some interesting attributes of this tag are: locale, currencyCode, integerOnly, currencySymbol and pattern.

Resources