How to add a converter to parameterized messages in JSF 1.2? - converters

I'm looking for a way to convert my params in a parameterized text inside a JSF 1.2 xhtml page.
Let's say I have this parameterized message in my messages.properties (please ignore the stupidity of this example):
EXAMPLE_TEXT=Right now {0} Euro is {1} US-Dollar.
And I would like the output to look like this:
Right now 1 Euro is 1.3658 US-Dollar.
Then my xhtml code looks like this:
<h:outputFormat value="#{messages.EXAMPLE_TEXT}">
<f:param value="#{backingBean.euroValue}" />
<f:param value="#{backingBean.dollarValue}" />
</h:outputFormat>
I already have implemented a converter (let's say its ID is bigDecimalConverter) that converts numeric values to the particular formats.
My question now is: (How) can a converter be registered to the output's param values?
I imagine something like this (which unfortunately seems to not be possible):
<h:outputFormat value="#{messages.EXAMPLE_TEXT}">
<f:param value="#{backingBean.euroValue}" converter="bigDecimalConverter" />
<f:param value="#{backingBean.dollarValue}" converter="bigDecimalConverter" />
</h:outputFormat>
Asking google I found this thread, but it's no sufficient answer to my specific problem.
Thanks a lot in advance for any help!

You can have exact the same output with outputText:
1) Change output to <h:outputText value="#{messages.TEXT1} #{backingBean.euroValue} #{messages.TEXT2} #{backingBean.dollarValue} #{messages.TEXT3}" />
2) Create method in backing bean which return full String and call it in output text.
(This answer was transformed from previous comment)

Related

Displaying the same image several times with multiple p:graphicImage without multiple HTTP requests

I'm using p:graphicImage tag in my XHTML page to displaying the same image in small an large :
<f:facet name="header">#{msgs.ATTR_PICTURE}</f:facet>
<h:panelGroup>
<p:graphicImage id="product_thumbnail" styleClass="thumbnail"
cache="false" value="#{imageBean.streamedImageById}">
<f:param name="productId" value="#{_product.id}" />
</p:graphicImage>
<p:tooltip id="imagebigger" for="product_thumbnail" position="right" showDelay="0" showEffect="blind" styleClass="tooltip_thumbnail">
<p:graphicImage value="#{imageBean.streamedImageById}" styleClass="thumbnail_large" cache="false">
<f:param name="productId" value="#{_product.id}" />
</p:graphicImage>
</p:tooltip>
</h:panelGroup>
I would like to avoid the 2 HTML requests systematically for each image to display. Is there a way to avoid that ?
You could use the stream attribute and set it to false.
This will render the image in base64 instead of rendering the URL to the image.
I don't suggest it as the markup will be very big and probably much worse performance.
I you remove the cache="false" from the first p:graphicImage (it defaults to true) the image is retrieved in the first <img ... /> (which the p:graphicImage renders to) on the client and just displayed from the client cache for the second one without retrieving it twice.
The cache="false" might have been there for a reason like you mentioned in the comments, but that is effectively a new question for which there are several Q/A in Stackoverflow but which one is the most appropriate depends on the details of your use case. Your new question might either be a duplicate again or a new valid question on its own... It might even be that returning the cache="true on the first p:graphicImage and replacing the second one with something else (even some pure client-side 'duplication' e.g.) might be a good approach.

Using of a4j:commandLink; how it works?

In my project I have the facelet, which contains the following chunk of markup:
<rich:column sortable="true" sortBy="#{item.date}">
<a4j:commandLink value="#{item.dateView}"
reRender="reportTable">
<a4j:actionparam name="shown" value="#{not item.showDetails}"
assignTo="#{item.showDetails}" />
</a4j:commandLink>
</rich:column>
After I've clicked to the commandLink I have that my table is reRendered with an additional data. But I completely don't undertand how it works. I've read a corresponding manual page about one, but it is still not clear. In the manual's example we use the following tag:
<a4j:commandLink value="Get greeting" reRender="greeting" />
And after we've clicked to it we get a message "Hello %name%". Where is the String template for the "Hello %name%" message is located. In the other word, I would like to enquire where the data that I reRendered is located?
a4j:commandLink assigns (boolean) value of !item.showDetails into item.showDetails and re-render reportTable. Your additional data (which you see after click to command link) is displayed after re-rendering of table, because there is (probably rendering) condition similar to "#{item.showDetails}".

Get the selected values from a rich:select which is inside o a4j:repeat

I am using a a4j:repeat control to iterate throughtout a Map<Object, List<MyType>> list. The XHTML page displays both h:outputText and rich:select controls.
I loop throughout a Map<String, List<Items>> instance, to show a master-detail tables. In the h:outputText I show the master description and the rich:select shows the detail.
The code is as follows:
customer.xhtml
<a4j:repeat value="#{masterManagedBean.listMasterItems.entrySet().toArray()}"
var="itemsVar">
<h:outputText value="#{itemsVar.key}" />
<rich:select enableManualInput="true">
<f:selectItems value="#{itemsVar.value}" id="itemsMenu"
var="itemsVarSelect"
itemLabel="#{itemsVarSelect.descriptionItems}"
itemValue="#{itemsVarSelect.idItems}" />
</rich:select>
</a4j:repeat>
This snippet works perfect. But, I don't have any idea how to gain the selected value from every rich:select generated by the repeater. Please guidance me to solve this issue.
Basically I would suggest not to use the datastructure Map<String,List> for the described case. Why not changing the Structure into a normal List of objects (List<SelectionObject>) with the SelectionObject-class holding:
name/label
possible values (as List or Array)
selected value
So the JSF code would look something like
<a4j:repeat value="#{masterManagedBean.listMasterItems}"
var="itemsVar">
<h:outputText value="#{itemsVar.label}" />
<rich:select enableManualInput="true" value="#{itemsVar.selectedValue}">
<f:selectItems value="#{itemsVar.possibleValues}" id="itemsMenu"
var="itemsVarSelect"
itemLabel="#{itemsVarSelect.descriptionItems}"
itemValue="#{itemsVarSelect.idItems}" />
</rich:select>
</a4j:repeat>
Any preparations/building for this object structure can be done e.g. in getListMasterItems (use some caching), the retrieval of the users data is done in the action of your submit-button, anyway. Just iterate through the list and read out the `selectedValue. If the answer is usable/helpful, please consider an upvote, confirm if it worked out for you.
Hope it helps...

Is it better to use JSF HTML or JSF Core namespaces with inputText when performing validation?

I need to perform simple form validation using JSF. Looking at different online examples I see two patterns:
<h:inputText id="name" value="#{bean.name}" label="name">
<f:validateRequired />
</h:inputText>
...or...
<f:inputText id="name" value="#{bean.name}" label="name">
<f:validateRequired />
</f:inputText>
The only difference is the namespace of the inputText tag. Both solutions seem to work, but which one is "more correct" and what makes it a better choice?
There is no such thing f:inputText, inputText is a part of the http://java.sun.com/jsf/html namespace and should use the h prefix,
like this: xmlns:h="http://java.sun.com/jsf/html"
Where have you seen the <f:inputText anyway ?
The only way that you can use <f:inputText is only if you assign the f prefix your self instead of the h , like this: xmlns:f="http://java.sun.com/jsf/html" (but its really not a good idea) better stick to the original h

Evaluating expression of Facelets components in Richfaces sort

I have some problem evaluating expression in a Facelets component defined in a .taglib.xml
SortField2.getExpression()
gives me the value "#{sortBy}" instead of evaluating the value.
My component (a simple column) is defined as:
<ui:composition>
<rich:column sortBy="#{sortBy}" width="#{width}" styleClass="#{styleClass}">
<f:facet name="header">#{header}</f:facet>
<ui:insert />
</rich:column>
</ui:composition>
I see that in TagValueExpression there is a "original" field which contains my value, but unfortunately it is private.
Any idea on how to extract it?
Also posted on Richfaces Forum
Thank you for your help.
You may be able to get "sortExpression" to work, instead of using "sortBy".
Also, instead of #{sortBy}, would ${sortBy} do any good? (A long shot)

Resources