Passing EL method expression as attribute of custom Facelets tagfile

…衆ロ難τιáo~ 提交于 2019-11-27 07:19:01

问题


I created a custom JSF tag:

<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>

To pass the listener method, I used the solution suggested in a quite old blog:

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

My questions are: is this the best way to do this? How can I make the method optional?

Thanks a lot for any Help! bye!


回答1:


My questions are: is this the best way to do this?

That's the only way anyway, provided that you can only use standard JSF/EL facilities and you cannot create a custom taghandler.

You could however create a custom taghandler to convert the value expression to a method expression. The OmniFaces JSF utility library has a <o:methodParam> for exactly this purpose. See also the <o:methodParam> demo page.

You could then end up like:

<my:dataScroller ... scrollListener="#{bean.aMethod}" />

and

<o:methodParam name="scrollListenerMethod" value="#{scrollListener}" />
<rich:dataScroller ... scrollListener="#{scrollListenerMethod}" />

See also:

  • Dynamic ui include and commandButton

How can I make the method optional?

Theoretically, you could use JSTL tags to build the view conditionally. Something like:

<h:someComponent>
    <c:if test="#{not empty fooAttribute}">
        <f:attribute name="foo" value="#{fooAttriubte}" />
    </c:if>
</h:someComponent>

But that's in the particular case of a special method expression listener attribute unfortunately not possible. There's no such thing as <rich:scrollListener> or something which allows you binding a RichFaces specific scrollListener as a separate tag to the <rich:dataScroller>. Best what you could do without creating custom taghandlers is duplicating the whole <rich:dataScroller> in two <c:if>s (or a <c:choose>); one with and other without scrollListener. This is too clumsy. You'd really better create a custom <my:richScrollListener> taghandler for this which you could then place in a <c:if>.



来源:https://stackoverflow.com/questions/12953560/passing-el-method-expression-as-attribute-of-custom-facelets-tagfile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!