使用ExpressionFactory#createMethodExpression()
.
public abstract MethodExpression createMethodExpression(
ELContext context,
java.lang.String expression,
java.lang.Class<?> expectedReturnType,
java.lang.Class<?>[] expectedParamTypes)
这是一个方便的方法:
public static MethodExpression createMethodExpression(String expression, Class<?> returnType, Class<?>... parameterTypes) {
FacesContext facesContext = FacesContext.getCurrentInstance();
return facesContext.getApplication().getExpressionFactory().createMethodExpression(
facesContext.getELContext(), expression, returnType, parameterTypes);
}
以下动作方法示例:
public void submit1()
public String submit2()
public void submit3(String argument)
public String submit4(String argument)
public void submit5(String argument1, Long argument2)
public String submit6(Long argument1, String argument2)
然后可以按如下方式创建:
createMethodExpression("#{bean.submit1}", null);
createMethodExpression("#{bean.submit2}", String.class);
createMethodExpression("#{bean.submit3('foo')}", null, String.class);
createMethodExpression("#{bean.submit4('foo')}", String.class, String.class);
createMethodExpression("#{bean.submit5('foo', 0)}", null, String.class, Long.class);
createMethodExpression("#{bean.submit6(0, 'foo')}", String.class, Long.class, String.class);
请注意,EL 表达式与您在普通视图文件中使用的完全相同。
更新这里是一个 SSCCE,它适用于我在 Tomcat 7.0.27 上的 Mojarra 2.1.12。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
>
<h:head>
<title>SO question 12098611</title>
</h:head>
<h:body>
<h:form binding="#{bean.form}">
<h:commandButton value="add" action="#{bean.add}" />
</h:form>
</h:body>
</html>
@ManagedBean
@RequestScoped
public class Bean {
private UIForm form;
public void add() {
String id = "button" + form.getChildCount();
UICommand button = new HtmlCommandButton();
button.setId(id);
button.setValue(id);
button.setActionExpression(createMethodExpression(String.format("#{bean.submit('%s')}", id), null, String.class));
form.getChildren().add(button);
}
public void submit(String arg) {
System.out.println("submit: " + arg);
}
public UIForm getForm() {
return form;
}
public void setForm(UIForm form) {
this.form = form;
}
public static MethodExpression createMethodExpression(String expression, Class<?> returnType, Class<?>... parameterTypes) {
FacesContext facesContext = FacesContext.getCurrentInstance();
return facesContext.getApplication().getExpressionFactory().createMethodExpression(
facesContext.getELContext(), expression, returnType, parameterTypes);
}
}
与具体问题无关,以上都是不好的做法。另请参阅JSF 中的“绑定”属性如何工作?何时以及如何使用它?