EL conditional Method Expression

前端 未结 2 1298
陌清茗
陌清茗 2021-01-14 11:42

I would like to declare a conditional method expression in EL like below:



        
2条回答
  •  独厮守ぢ
    2021-01-14 12:05

    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.

    E.g.

    
    
     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:

    
    ...
    
    
    @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 });
         }
    
    }
    

提交回复
热议问题