JSF 1.2:ui:包含参数

ser*_*nni 30 parameters jsf include reusability

拥有JSF 1.2两个页面(one.xhtml和other.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> 
...
Run Code Online (Sandbox Code Playgroud)

one.xhtml不同于other.xhtml只能通过动作参数:

one.xhtml:<h:commandLink action="actionOne">
other.xhtml:<h:commandLink action="actionTwo">

是否可以使用一些通用的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> 
...
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助.

Bal*_*usC 61

您需要嵌套<ui:param>内部<ui:include>以将参数传递给包含的文件.

<ui:include src="general.xhtml">
    <ui:param name="action" value="actionOne" />
</ui:include>
Run Code Online (Sandbox Code Playgroud)

并在包括:

<h:commandButton action="#{action}" />
Run Code Online (Sandbox Code Playgroud)

请注意,这仅支持字符串,而不支持操作方法.对于后者,您需要升级到JSF 2.0并使用复合组件.

  • 作为一个注释,除了字符串之外,param还可以处理整个对象. (2认同)
  • @Adam:当使用`&lt;ui:param&gt;`时,是的。这不是什么秘密。但这不适用于“&lt;f:param&gt;”,它始终会转换为“String”。也许你最初的困惑就是基于此。 (2认同)
  • @BalusC:我只是想详细说明以供将来参考。感谢您对“f:param”的注释及其与“ui:param”的比较。我从未使用过“f”库版本,并且会记住其中的差异。 (2认同)

mer*_*ike 22

除了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>
Run Code Online (Sandbox Code Playgroud)

<h:commandButton action="#{actionBean[actionMethod]}" />
Run Code Online (Sandbox Code Playgroud)