将EL方法表达式作为自定义Facelets标记文件的属性传递

Fed*_*ico 2 jsf facelets tagfile jsf-2 methodexpression

我创建了一个自定义的JSF标记:

<ui:composition>
    <h:panelGroup>
        <rich:dataScroller id="#{id}" for="#{table}" execute="#{table}"
            page="#{scrollerPage}" render="#{table}-sc1" maxPages="5"
            fastControls="hide" oncomplete="#{onCompl}" scrollListener="#{scrollListenerBean[scrollListenerMethod]}" />
        <h:inputText value="#{scrollerPage}" id="#{table}-sc1" size="2">
            <f:convertNumber integerOnly="true" />
        </h:inputText>
        <h:outputText styleClass="outputText"
            value=" of #{scrollPagesCount}  " />
        <h:commandButton value="GO! " />
    </h:panelGroup>
</ui:composition>
Run Code Online (Sandbox Code Playgroud)

为了传递监听器方法,我使用了一个相当古老的博客中建议的解决方案:

<my:dataScroller id="idDS1" table="table1"
                    scrollerPage="#{bean.navigationHelper.scrollerPage}"
                    scrollPagesCount="#{bean.navigationHelper.scrollPagesCount}"
                    onCompl="initForm();" 
                    scrollListenerBean="#{bean}"
                    scrollListenerMethod="aMethod" />
Run Code Online (Sandbox Code Playgroud)

我的问题是:这是最好的方法吗?如何使方法可选?

非常感谢任何帮助!再见!

Bal*_*usC 5

我的问题是:这是最好的方法吗?

这是唯一的方法,前提是您只能使用标准的JSF/EL工具,并且无法创建自定义标签处理程序.

但是,您可以创建自定义标记处理程序以将值表达式转换为方法表达式.该OmniFaces JSF工具库有<o:methodParam>出于这样的目的.又见<o:methodParam>演示页.

然后你最终会像:

<my:dataScroller ... scrollListener="#{bean.aMethod}" />
Run Code Online (Sandbox Code Playgroud)

<o:methodParam name="scrollListenerMethod" value="#{scrollListener}" />
<rich:dataScroller ... scrollListener="#{scrollListenerMethod}" />
Run Code Online (Sandbox Code Playgroud)

如何使方法可选?

从理论上讲,您可以使用JSTL标记有条件地构建视图.就像是:

<h:someComponent>
    <c:if test="#{not empty fooAttribute}">
        <f:attribute name="foo" value="#{fooAttriubte}" />
    </c:if>
</h:someComponent>
Run Code Online (Sandbox Code Playgroud)

但遗憾的是,在特殊方法表达式侦听器属性的特定情况下,这是不可能的.没有任何<rich:scrollListener>东西可以让你将RichFaces特定scrollListener的单独标签绑定到<rich:dataScroller>.在没有创建自定义标签处理程序的情况下,您可以做的最好的事情是将整个内容复制为<rich:dataScroller>两个<c:if>(或一个<c:choose>); 一个与另一个没有scrollListener.这太笨拙了.你最好<my:richScrollListener>为此创建一个自定义标签处理程序,然后放入一个<c:if>.