Using a conditional statement in h: inputText value and h: commandButton actionListener

I want to load two difference panels into a .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 is "TERMINAL", I want to load a "terminalList" controlled by a bean, otherwise the "merchantsList" is controlled by a bean.

this code does not work.

+4
source share
1 answer

You cannot use the conditional operator ?: In expressions of values ​​and actions. Expressing a value will raise a PropertyNotWritableException in the form of submit because the EL syntax is not a write operation, instead it is a read-only operation. The action expression would already load ELException: not a valid method expression on the page load, because the EL syntax is not a method expression, but a value expression.

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

  • Using an abstract base class and tag file. Currently, your bean subcategory names are unfortunately not aligned in such a way that they are the same for both classes. You only aligned the globalFilter property, but the action listener methods did not execute. I suggest renaming them to filter() and resetFilter() . You can then extract the abstract base class from these bean classes and use it in the custom tag file , as shown below:

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

    which is implemented as follows (assuming those beans have a request scope):

     <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 area, just change #{requestScope} accordingly, for example #{viewScope} )


  • Using JSTL to conditionally build a view. This is really awkward (not DRY ), but perhaps easier for the starter and actually the only way if you cannot change the method signatures for some strange 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> 
+8
source

Source: https://habr.com/ru/post/1500472/


All Articles