在h:inputText值和h:commandButton actionListener中使用条件运算符 [英] Using conditional operator in h:inputText value and h:commandButton actionListener

查看:95
本文介绍了在h:inputText值和h:commandButton actionListener中使用条件运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在.xhtml文件中加载两个差异面板.

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}" />

当http get请求参数等于"TERMINAL"时,我要加载"terminalsList"托管bean,否则要加载"merchantsList"托管bean.

when http get request params equals 'TERMINAL' i want to load 'terminalsList' managed bean, else 'merchantsList' managed bean.

此代码不起作用.

推荐答案

您不能在值和动作表达式中使用条件运算符?:.值表达式将在表单提交上抛出PropertyNotWritableException,因为EL语法不代表可写操作,而是只读操作.由于EL语法不代表方法表达式,而代表值表达式,因此动作表达式在页面加载时就已经抛出了ELException: not a valid method expression.

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.

您需要以不同的方式解决它,然后以可以完全摆脱value和action表达式中的条件运算符?:的方式进行解决.这可以通过几种方式实现:

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. 使用抽象基类和标记文件.当前,不幸的是,您的后备bean方法名称未以两种类完全相同的方式对齐.您只对齐了globalFilter属性,但没有对齐操作侦听器方法.我建议将它们重命名为filter()resetFilter().然后,您可以从这些bean类中提取一个抽象基类,并在

  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'}" />

其实现方式如下(假设这些bean在请求范围内):

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}" />

(如果您的bean在其他范围内,只需相应地更改#{requestScope},例如#{viewScope})

(if your bean is in a different scope, just change #{requestScope} accordingly, e.g. #{viewScope})

使用JSTL有条件地构建视图.这确实很笨拙(不是 DRY ),但对于初学者来说可能更容易,实际上如果由于某些不清楚的原因而无法更改方法签名,这是唯一的方法.

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>

这篇关于在h:inputText值和h:commandButton actionListener中使用条件运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆