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

后端 未结 1 1794
名媛妹妹
名媛妹妹 2020-12-11 05:20

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



        
相关标签:
1条回答
  • 2020-12-11 06:04

    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>
      
    0 讨论(0)
提交回复
热议问题