在h:inputText值和h:commandButton actionListener中使用条件运算符

6
我希望你能够在.xhtml文件中加载两个不同的面板。
<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}" />

当HTTP GET请求参数等于“TERMINAL”时,我希望加载“terminalsList”托管bean,否则加载“merchantsList”托管bean。这段代码不起作用。

哪段代码出了问题?请展示与此相关的所有代码,包括xhtml和相关的后台bean。此外,请描述哪些部分没有正常工作。堆栈跟踪?还是一些意外行为? - Magnilex
<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}" /> - zura katsitadze
这是 XHTML 代码,格式不起作用,抱歉。 - zura katsitadze
请发布一些示例代码,这将有助于我们解决问题。 - newuser
代码本来就在那里,只是因为格式错误而看不见。我进行了更改。 - noone
显示剩余2条评论
1个回答

11
您不能在值和动作表达式中使用条件运算符?:。因为EL语法不代表可写操作,而是只读操作,所以该值表达式将在表单提交时抛出PropertyNotWritableException。该动作表达式将在页面加载时抛出ELException: not a valid method expression,因为EL语法不表示方法表达式,而是值表达式。
您需要通过其他方式解决此问题,从而彻底摆脱值和动作表达式中的条件运算符?:。可以通过以下几种方式来实现:
  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>
    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接