Conditional rendering of f: param in JSF

I use <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 query string parameters are dynamic. So, <c:set> used here only for demonstration.

At any moment, either one, or both, or none of the parameters can be present. In the event that only one or none of them is present, the / s option is not necessarily added to the URL, which should not occur. Preventing the addition of unnecessary query string parameters to the URL requires conditional rendering of <f:param> .

JSTL <c:if> as below

 <c:if test="${not empty cid}"> <f:param name="cid" value="#{cid}"/> </c:if> 

does not work.

How can we conditionally render <f:param> inside <h:outputLink> ?

+8
source share
2 answers

<f:param> has a disable (not disabled !) attribute for this purpose.

 <f:param name="cid" value="#{cid}" disable="#{empty cid}" /> <f:param name="sid" value="#{sid}" disable="#{empty sid}" /> 

Note that this has an error in versions of Mojarra older than 2.1.15 because they sealed the actual UIParameter property as disble instead of disable . See also issue 2312 .

As for the <c:if> approach, this will only work if #{cid} and #{sid} are available during build time. In other words, it will fail if they are only available during render rendering, for example. when they depend on the var component of the repeater. See Also JSTL in JSF2 Facelets ... makes sense?

See also:

  • f: param tag attribute 'disable' does not work
+15
source share

You do not like this decision?

 <f:param name="#{cid == null ? '' : 'cid'}" value="#{cid}"/> <f:param name="#{sid == null ? '' : 'sid'}" value="#{sid}"/> 
+2
source share

All Articles