在EL中动态调用方法,从String中计算

sof*_*ood 3 jsf el dynamic commandbutton methodexpression

我有一个提交按钮.此提交按钮具有"操作"属性.但是这个action属性应该总是调用另一个函数(某种泛型).所以我想动态调用一个函数.这是因为我需要重用这个组件.我只是不知道action属性需要哪种Type(Method,String等?)以及如何正确引用想要的"BeanWithMethodToCall".

@Named
@SessionScoped
public class BeanWithMethodToCall{

   @Inject
   private BeanWhichIsCalledFromEL elBean;

   public void methodToCall(){
      //do something
   }

   public void someLogic(){
      // here the wanted method is set on the bean which is later on called from el
      elBean.setMethodToCall("methodToCall");
   }
}

@Named
@SessionScoped
public class BeanWhichIsCalledFromEL{

   // i don't know the correct type of this :S
   private String method;

   public void setMethodToCall(String method){
      this.method = method;
   }

   // i don't know the correct return type of this :S
   public String getMethodToExecute(){
      //this method is called in the action attribute in the xhtml 
      // and should return a dynamic function to call
   }

}
Run Code Online (Sandbox Code Playgroud)

在EL中:

<h:commandButton value="Cancel" action="#{beanWhichIsCalledFromEL.getMethodToExecute()}">
    <f:ajax render="@form"/>
</h:commandButton>
Run Code Online (Sandbox Code Playgroud)

这看起来很棘手..我希望有人可以帮助我.我需要反思吗?或EL解析器或其他什么?

Bal*_*usC 7

使用括号表示法#{bean[foo]}来评估"动态"方法和属性名称.

您的具体案例可以解决如下:

<h:commandButton ... action="#{bean[bean.methodToExecute]}">
Run Code Online (Sandbox Code Playgroud)

也可以看看: