简体   繁体   中英

How to use JSF navigation rules with ResourceBundle content

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 <a href="contact.xhtml">contact us</a> 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="&lt;a href='#{request.contextPath}/contact.xhtml'&gt;#{template['contactUs']}&lt;/a&gt;" />
</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="&lt;a href='#{facesContext.application.viewHandler.getBookmarkableURL(facesContext, '/contact', null, false)}'&gt;#{template['contactUs']}&lt;/a&gt;" />
</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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM