How to use JSF navigation rules with ResourceBundle content - jsf

I have a multi-lingual site with content managed by ResourceBundles. How can I use JSF navigation to insert links to website sections using paragraph content.
Example 1: Desired HTML output
<p>Simply contact us to find out more.</p>
Example 2: Simple facelt to achieve the above
<p>Simply <h:link outcome="contact" value="contact us"/> to find out more.</p>
Desired result: Same but with ResourceBundle
Facelet
<p>
<h:outputText value="${template['paragraph']}">
<f:param>
<h:link outcome="contact" value="${template['contactUs']}"/>
</f:param>
</h:outputText>
</p>
ResourceBundle
paragraph=Simply {0} to find out more.
contactUs=contact us

The problem is not exactly the JSF navigation rules, but that you cannot use <f:param> to specify JSF components as bundle parameter, let alone plain vanilla HTML children. Also, please note that you cannot use <f:param> in <h:outputText>, but only in <h:outputFormat>.
In order to achieve the functional requirement anyway, you've to write down the HTML in escaped flavor as <f:param value> yourself.
<h:outputFormat value="#{template['paragraph']}" escape="false">
<f:param value="<a href='#{request.contextPath}/contact.xhtml'>#{template['contactUs']}</a>" />
</h:outputFormat>
or, if you'd like to utilize JSF ViewHandler#getBookmarkableURL() like as <h:link> is doing for its outcome attribute,
<h:outputFormat value="#{template['paragraph']}" escape="false">
<f:param value="<a href='#{facesContext.application.viewHandler.getBookmarkableURL(facesContext, '/contact', null, false)}'>#{template['contactUs']}</a>" />
</h:outputFormat>
Since you're not the first one who stumbled upon this and the workarounds are awkward, the JSF utility library OmniFaces has recently updated its <o:param> component with support to encode children as param value when no value is specified. From OmniFaces 1.5 on, you should be able to use it as follows:
<h:outputFormat value="#{template['paragraph']}" escape="false">
<o:param><h:link outcome="contact" value="#{template['contactUs']}" /></o:param>
</h:outputFormat>

Related

Passing dynamic param through bundle.properties to alt text value of graphicImage [duplicate]

I have use case in which I have to use resource bundle to display various texts on UI. Some of these resource bundle entries take paramets (e.g. {0}), for these I use h:outputFormat but sometimes that isn't enough.
e.g.
someMessage=Display this message with param {0}
in a resource bundle.
To display it on xhtml I normally do:
<h:outputFormat value="#{msg['someMessage']}"><f:param value="#{someBean.value}"/></h:outputFormat>
That works well when it's a simple case, but for more complex use cases it isn't enough. For example if I want the 'title' attribute of a commandLink to use the above resource bundle entry:
<h:commandLink action="logout" title="#{msg['someMessage']}">
<f:param value="#{someBean.value}" />
<h:graphicImage library="images" name="image.png" />
</h:commandLink>
which doesn't work. I also tried:
<h:commandLink action="logout">
<f:attribute name="title">
<h:outputFormat value="#{msg['someMessage']}"><f:param value="#{someBean.value}"/></h:outputFormat>
</f:attribute>
<h:graphicImage library="images" name="image.png" />
</h:commandLink>
which also doesn't work since f:attibute doesn't allow children.
Even if there is a hack to bypass this (e.g. using hover component from primefaces) there are other fields that might require a parameterized message.
Does anyone know of a way to use MessageFormat that takes an argument in a non-value field of a JSF component?
You could create a custom EL function for this with which you can ultimately end up like:
<h:commandLink ... title="#{my:format(msg['someMessage'], someBean.value)}" />
You can use the MessageFormat API to perform the job, exactly as <h:outputFormat> is doing under the covers.
An alternative is to create a custom component which does the same as JSTL's good 'ol <fmt:message> which supports a var attribute to export the formatted message into the EL scope.
<my:outputFormat ... var="linkTitle">
...
</my:outputFormat>
<h:commandLink ... title="#{linkTitle}" />
Update: JSF utility library OmniFaces has #{of:formatX()} functions and a <o:outputFormat> component for the very purpose.

Does PrimeFaces has anything like o:param (solution to add complex parameter for outputFormat)?

The given problem is that passing complex parameter (see code example below) doesn't work in JSF.
<h:outputFormat value="Final result is: {0}">
<f:param>
<h:outputFormat value="{0} to {1}">
<f:param value="#{mngr.lowerBound}"/>
<f:param value="#{mngr.upperBound}"/>
</h:outputFormat>
</f:param>
</h:outputFormat>
I'm NOT looking for the solution that uses the ManagedBean, like this:
<h:outputFormat value="Final result is: {0}">
<f:param value="mngr.formattedParams />
</h:outputFormat>
However there is a proper solution that works in OmniFaces:
JSF-2. h:outputFormat. Complex f:param
I'm wondering if there is also a solution in PrimeFaces?
No PrimeFaces does not have this. There is no reason to either since OmniFaces has it. And they both target different aspects of JSF: ui component suite vs utility suite so fully complementary

EL conditional render based on composite component attribute [duplicate]

I'm using an <h:outputLink> as follows.
<c:set var="cid" value="1"/>
<c:set var="sid" value="2"/>
<h:outputLink value="Test.jsf">
<h:outputText value="Link"/>
<f:param name="cid" value="#{cid}"/>
<f:param name="sid" value="#{sid}"/>
</h:outputLink>
This is just an example. Both of the query-string parameters are dynamic. So, <c:set> used here is just for the sake of demonstration.
At any time, either one, both or none of the parameters may be present. In case, if only one or none of them is present then, parameter/s are unnecessarily appended to the URL which should not happen. Preventing unnecessary query-string parameters from being appended to the URL requires a conditional rendering of <f:param>.
JSTL <c:if> like the following
<c:if test="${not empty cid}">
<f:param name="cid" value="#{cid}"/>
</c:if>
did not work.
How can it be made possible to conditionally render <f:param> inside <h:outputLink>?
The <f:param> has a disable (not disabled!) attribute for the purpose.
<f:param name="cid" value="#{cid}" disable="#{empty cid}" />
<f:param name="sid" value="#{sid}" disable="#{empty sid}" />
Note that this has a bug in Mojarra versions older than 2.1.15, because they typo'ed the actual UIParameter property to be disble instead of disable. See also issue 2312.
As to the <c:if> approach, that would only work if the #{cid} and #{sid} is available during view build time. In other words, it would fail if they are only available during view render time, e.g. when they depend on var of a repeater component. See also JSTL in JSF2 Facelets... makes sense?
See also:
f:param tag attribute 'disable' is not work
Don't you like this kind of a solution?
<f:param name="#{cid == null ? '' : 'cid'}" value="#{cid}"/>
<f:param name="#{sid == null ? '' : 'sid'}" value="#{sid}"/>

How to add tooltip to f:selectItems

For example the f:selectItems component doesn't support the title attribute in some versions of JSF.
Would it be possible to replace JSF Components by their plain HTML counterparts using JSFC and do something like this?
<select jsfc="h:selectOneMenu" value="#{cc.data}">
<option jsfc="f:selectItems" value="${cc.listItems}" var="item" title="#{item.tooltip}"></option>
</select>
instead of
<h:selectOneMenu value="#{cc.data}">
<f:selectItems value="#{cc.listItems}" />
</h:selectOneMenu>
Doing exactly so, replacing the latter by the above, I'm getting "<f:converter> Parent not an instance of ValueHolder: javax.faces.component.html.HtmlPanelGroup" Facelet TagExceptions
Would it be possible to replace JSF Components by their plain HTML counterparts using JSFC and do something like this
Nope. Ultimately, such a HTML element with jsfc attribute will be turned into a true JSF component in the JSF component tree and only the attributes supported by the component in question would be parsed and set as component attribute. The title attribute isn't among the supported attributes of UISelectItem component. I'm not sure what exactly you mean with "some versions of JSF". The standard JSF API already doesn't support it in first place. JSF spec issue 529 describes this shortcoming and is currently still open.
If you're using JSF 2.2, make use of passthrough attributes. You only need to replace <f:selectItems> by <c:forEach><f:selectItem>, see also Using f:selectItems var in passtrough attribute
<... xmlns:a="http://xmlns.jcp.org/jsf/passthrough">
<c:forEach value="#{bean.items}" var="item">
<f:selectItem itemValue="#{item}" a:title="#{item.tooltip}" />
</c:forEach>
Based on your question history you seem to be not using JSF 2.2 yet. If you can't upgrade, you basically need a custom renderer for <h:selectOneMenu>.
While creating the custom renderer, you could make use of the unused(!) description property of the UISelectItem class. I've answered this before on a similar question targeted at <p:selectManyCheckbox>: Primefaces tooltip for p:selectManyCheckbox or other p:selectMany*/One*.
<f:selectItems ... var="item" itemDescription="#{item.tooltip}" />
Noted should be that creating the custom renderer for <h:selectOneMenu> is a pain, particularly if you intend to be JSF implementation independent. Theoretically, a custom ResponseWriter should be able to catch this, but unfortunately, the <h:selectOneMenu> only passes itself when writing <option>, instead of the UISelectItem in question.
In my case (JSF 2.2 / Mojarra 2.2.14), itemDescription worked out of the box. I.e:
<c:forEach items="#{bean.items}" var="item">
<f:selectItem itemValue="#{item}" itemLabel="#{item}" itemDescription="#{item.tooltip}" />
</c:forEach>

How to pass rich params to jsf for i18n?

I am using jsf to render my html pages and I am using the nifty resource bundle loading to add i18n to the various pages. The problem I am having is that with outputFormat you can not pass in any "rich" parameters. For instance this sentance:
This my favorite search engine, you should check it out.
It would be nice to do something like this:
<h:outputFormat value="#{bundle.favItemLineWithParam}>
<f:param>
<h:outputFormat value="#{bundle.searchEngine}>
<f:param>
<h:link value="http://google.com">
</f:param>
</h:outputFormat>
</f:param>
</h:outputFormat>
but that is not allowed, it would seem like the only option is to render the links in java with a backing bean or something. Any Ideas?
That's not possible. You need to use plain HTML in the bundle value and set escape="false".
favItem = This is my favourite {0}, you should check it out.
with
<h:outputFormat value="#{bundle.favItem}" escape="false">
<f:param value="search engine" />
<f:param value="http://google.com" />
</h:outputFormat>
Update:
Since version 1.5, it's possible with <o:param> of JSF utility library OmniFaces:
favItem = This is my favourite {0}, you should check it out.
searchEngine = search engine
with
<h:outputFormat value="#{bundle.favItem}" escape="false">
<o:param>#{bundle.searchEngine}</o:param>
</h:outputFormat>

Resources