Using conditional operator in h:inputText value and h:commandButton actionListener

99封情书 提交于 2019-12-17 20:11:16

问题


I want to load two difference panel in .xhtml file.

<h:inputText value="#{param['from']=='TERMINAL' ? terminalsList.globalFilter : merchantsList.globalFilter}" size="50" />
<h:commandButton value="Filter" actionListener="#{param['from']=='TERMINAL' ? terminalsList.filterTerminals : merchantsList.filterMerchants}" />
<h:commandButton value="Reset" actionListener="#{param['from']=='TERMINAL' ? terminalsList.resetTerminalsFilter : merchantsList.resetMerchantsFilter}" />

when http get request params equals 'TERMINAL' i want to load 'terminalsList' managed bean, else 'merchantsList' managed bean.

this code not working.


回答1:


You cannot use the conditional operator ?: in value and action expressions. The value expression would throw a PropertyNotWritableException on form submit because the EL syntax does not represent a writable operation, instead it is a read-only operation. The action expression would already throw an ELException: not a valid method expression on page load because the EL syntax does not represent a method expression but a value expression.

You need to solve it differently and then in such way that you can get rid of the conditional operator ?: in the value and action expressions altogether. This can be achieved in several ways:

  1. Using an abstract base class and a tagfile. Currently, your backing bean method names are unfortunately not aligned out in such way that they are exactly the same on both classes. You've only globalFilter property aligned, but the action listener methods not. I suggest to rename them to filter() and resetFilter(). Then you can extract an abstract base class from those bean classes and use it on a custom tag file like follows:

    <my:filter beanName="#{param.from eq 'TERMINAL' ? 'terminalsList' : 'merchantsList'}" />
    

    which is implemented like follows (assuming that those beans are request scoped):

    <h:inputText value="#{requestScope[beanName].globalFilter}" size="50" />
    <h:commandButton value="Filter" actionListener="#{requestScope[beanName].filter}" />
    <h:commandButton value="Reset" actionListener="#{requestScope[beanName].resetFilter}" />
    

    (if your bean is in a different scope, just change #{requestScope} accordingly, e.g. #{viewScope})


  2. Using JSTL to conditionally build the view. This is really clumsy (not DRY), but maybe easier for a starter and actually the only way if you can't change the method signatures for some unclear reason.

    <c:choose>
        <c:when test="#{param.from eq 'TERMINAL'}">
            <h:inputText value="#{terminalsList.globalFilter}" size="50" />
            <h:commandButton value="Filter" actionListener="#{terminalsList.filterTerminals}" />
            <h:commandButton value="Reset" actionListener="#{terminalsList.resetTerminalsFilter}" />
        </c:when>
        <c:otherwise>
            <h:inputText value="#{merchantsList.globalFilter}" size="50" />
            <h:commandButton value="Filter" actionListener="#{merchantsList.filterMerchants}" />
            <h:commandButton value="Reset" actionListener="#{merchantsList.resetMerchantsFilter}" />
        </c:otherwise>
    </c:choose>
    


来源:https://stackoverflow.com/questions/18609493/using-conditional-operator-in-hinputtext-value-and-hcommandbutton-actionlisten

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