JSF中f:param的条件渲染 [英] Conditional rendering of f:param in JSF

查看:113
本文介绍了JSF中f:param的条件渲染的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按如下方式使用<h:outputLink>.

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>

这只是一个例子.这两个查询字符串参数都是动态的.因此,此处使用的<c:set>只是出于演示目的.

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.

在任何时候,可能会出现一个参数,两个参数或都不存在任何参数.如果仅存在一个或一个都不存在,则不必要地将参数附加到URL,这是不应该发生的.要防止将不必要的查询字符串参数附加到URL,需要有条件地呈现<f:param>.

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>如下所示

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

没有用.

如何在<h:outputLink>内部有条件地渲染<f:param>?

How can it be made possible to conditionally render <f:param> inside <h:outputLink>?

推荐答案

<f:param>具有

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}" />

请注意,这在Mojarra 2.1.15之前的版本中存在一个错误,因为它们将实际的UIParameter属性打错为disble而不是disable.另请参见问题2312 .

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.

关于<c:if>方法,只有在视图构建期间#{cid}#{sid}可用的情况下,该方法才有效.换句话说,如果它们仅在视图渲染期间可用,则失败.当它们取决于中继器组件的var时.另请参见 JSF2 Facelets中的JSTL ...有意义吗?

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?

这篇关于JSF中f:param的条件渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆