Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
859 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...