【问题标题】:How to use ui:include with parameters?如何使用 ui:include 和参数?
【发布时间】:2011-07-14 13:17:11
【问题描述】:

有 JSF 1.2 两个页面(一个.xhtml 和另一个.xhtml),
通过以下规则包含到当前页面:

...
    <c:if test="#{flowScope.Bean.param1}">
        <ui:include src="one.xhtml"/>
    </c:if> 

    <c:if test="#{!flowScope.Bean.param1}">
        <ui:include src="other.xhtml"/>
    </c:if> 
...

到目前为止,one.xhtmlother.xhtml 的区别仅在于操作参数:

one.xhtml:&lt;h:commandLink action="actionOne"&gt;
other.xhtml:&lt;h:commandLink action="actionTwo"&gt;

是否可以使用一些通用的xhtml?
而不是one.xhtml和other.xhtml,像这样:

...
    <c:if test="#{flowScope.Bean.param1}">
        <ui:include src="general.xhtml" param="actionOne"/>
    </c:if> 

    <c:if test="#{!flowScope.Bean.param1}">
        <ui:include src="general.xhtml" param="actionTwo"/>
    </c:if> 
...

谢谢你的帮助。

【问题讨论】:

    标签: jsf parameters include reusability


    【解决方案1】:

    您需要将&lt;ui:param&gt; 嵌套在&lt;ui:include&gt; 中以将参数传递给包含的文件。

    <ui:include src="general.xhtml">
        <ui:param name="action" value="actionOne" />
    </ui:include>
    

    并在包含:

    <h:commandButton action="#{action}" />
    

    请注意,这仅支持字符串,不支持操作方法。对于后者,您需要升级到 JSF 2.0 并使用 composite components

    【讨论】:

    • 谢谢,我知道cc,它们很棒,但是在当前的jsf 1.2项目中不能使用它们。我会尝试你的解决方案并写回结果。
    • 它将根据您在问题中提出的方式工作。但是,如果您使用的是#{bean.doSomething} 而不是actionOne,那么您确实需要获取 JSF 2.0 复合组件。
    • 请注意,除了字符串之外,param 还适用于整个对象。
    • @Adam:当使用&lt;ui:param&gt;时,是的。这不是什么秘密。但这不适用于&lt;f:param&gt;,它将始终转换为String。也许您最初的困惑是基于此。
    • @BalusC:我只是想详细说明一下以供将来参考。感谢f:param 的注释及其与ui:param 的比较。我从来没有使用过 f 库版本,并且会记住其中的区别。
    【解决方案2】:

    除了BalusC的回答:

    请注意,这只支持字符串, 不是行动方法。对于后者,你 需要升级到 JSF 2.0 和 使用复合组件。

    有一种方法可以用 JSF 1.2 做到这一点,虽然它有点难看:

    <ui:include src="general.xhtml">
        <ui:param name="actionBean" value="#{myBackingBean}" />
        <ui:param name="actionMethod" value="edit" />
    </ui:include>
    

    <h:commandButton action="#{actionBean[actionMethod]}" />
    

    【讨论】:

    • 我可以知道为什么对于 actionBean 值,我们需要用 #{} 将其括起来以使其工作吗?
    猜你喜欢
    • 2012-06-16
    • 1970-01-01
    • 2012-01-14
    • 2012-12-05
    • 2012-08-17
    • 1970-01-01
    • 2011-12-21
    • 2013-03-24
    • 2016-01-20
    相关资源
    最近更新 更多