Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional rendering of f:param in JSF

Tags:

jsf

jsf-2.2

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>?

like image 411
Tiny Avatar asked Sep 15 '14 10:09

Tiny


2 Answers

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
like image 53
BalusC Avatar answered Sep 20 '22 11:09

BalusC


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}"/>
like image 40
prageeth Avatar answered Sep 22 '22 11:09

prageeth