Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 583
zura katsitadze Avatar asked Sep 04 '13 08:09

zura katsitadze


1 Answers

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>
    
like image 198
BalusC Avatar answered Sep 21 '22 01:09

BalusC