Using conditional operator in h:inputText value and h:commandButton actionListener
Asked Answered
P

1

6

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.

Prowess answered 4/9, 2013 at 8:58 Comment(7)
Which code is not working? Show us all the relevant code please. That includes the xhtml and the relevand backing beans. Also, describe what is not working. Stacktrace? Or some unexpected behaviour?Tidewaiter
<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}" />Prowess
this is xhtml code, formatting not working soryProwess
Post some sample code it will help us to solve the problemContradance
Are you trying to load the list in dataTable or any other componentContradance
The code was there, but invisible because of wrong formatting. I changed that.Dx
thank, i'm not trying to load dataTable, i want to load only 3 element.Prowess
W
11

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>
    
Wadlinger answered 4/9, 2013 at 13:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.