简体   繁体   English

EL条件方法表达式

[英]EL conditional Method Expression

I would like to declare a conditional method expression in EL like below: 我想在EL中声明一个条件方法表达式,如下所示:

<p:dataTable id="#{cc.attrs.datatableId}" var="overview" 
    rowSelectListener="#{cc.attrs.detailsMode == 'single' ? cc.attrs.bean.onRowSelect : cc.attrs.bean.onRowUrlSelect}">

However, it throws an EL exception: 但是,它抛出一个EL异常:

javax.el.ELException: Not a Valid Method Expression: #{ cc.attrs.detailsMode == 'single' ? javax.el.E​​LException:不是有效的方法表达式:#{cc.attrs.detailsMode =='single'? cc.attrs.bean.onRowSelect : cc.attrs.bean.onRowUrlSelect} cc.attrs.bean.onRowSelect:cc.attrs.bean.onRowUrlSelect}

How I can declare a conditional EL method expression? 我如何声明条件EL方法表达式?

Unfortunately, method expressions does not accept value expressions. 不幸的是,方法表达式不接受值表达式。 Your best bet is to have a single method entry point which in turn delegates further to the desired action methods based on the detailsMode which you also pass/set to the bean. 最好的办法是拥有一个方法入口点,然后根据您也传递/设置给bean的detailsMode进一步委托所需的操作方法。

Eg 例如

<h:dataTable ... rowSelectListener="#{cc.attrs.bean.onRowSelect}">
 public void onRowSelect(SelectEvent event) {  
     if ("single".equals(detailsMode)) {
         onRowSingleSelect(event);
     } else {
         onRowUrlSelect(event);
     }
 }

Given that you're actually using a composite component, you can if necessary hide it away in the backing component to reduce boilerplate in backing bean: 鉴于您实际上正在使用复合组件,您可以根据需要将其隐藏在支持组件中以减少辅助bean中的样板:

<cc:interface componentType="yourComponent">
...
<h:dataTable ... rowSelectListener="#{cc.onRowSelect}">
@FacesComponent("yourComponent")
public class YourComponent extends UINamingContainer {

     public void onRowSelect(SelectEvent event) {  
        String methodName = "single".equals(detailsMode) ? "onRowSingleSelect" : "onRowUrlSelect";
        MethodExpression method = (MethodExpression) getAttributes().get(methodName);
        method.invoke(getFacesContext().getELContext(), new Object[] { event });
     }

}

You can try with 你可以试试

<p:dataTable id="#{cc.attrs.datatableId}" var="overview" 
    rowSelectListener="#{cc.attrs.bean[cc.attrs.detailsMode == 'single' ? 'onRowSelect' : 'onRowUrlSelect']}">

For further reference, you can refer to https://docs.oracle.com/javaee/6/tutorial/doc/bnahu.html#bnahz 如需进一步参考,请参阅https://docs.oracle.com/javaee/6/tutorial/doc/bnahu.html#bnahz

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM