我正在尝试使用输入文本和命令按钮创建一个动态表单。一切正常。但是当我单击命令按钮时,永远不会调用动作侦听器。请建议我做错了什么,或者这是否是 PF 或 Mojarra 的错误。代码如下
panel = new Panel();
panel.setHeader("Test");
InputText text = new InputText();
final String binding = "#{roleCreateForm.role.name}";
text.setValueExpression("value",
createValueExpression(binding, String.class));
panel.getChildren().add(text);
CommandButton button = new CommandButton();
button.setValue("Save");
MethodExpression me = createMethodExpression("#{roleCreateForm.save}");
button.addActionListener(new MethodExpressionActionListener(me));
panel.getChildren().add(button);
createXXXExpression 也在下面
private MethodExpression createMethodExpression(String action) {
final Class<?>[] paramTypes = new Class<?>[0];
MethodExpression methodExpression = getExpressionFactory()
.createMethodExpression(getELContext(),action, null, paramTypes);
return methodExpression;
}
private ValueExpression createValueExpression(String binding,
Class<String> clazz) {
final ValueExpression ve = getExpressionFactory()
.createValueExpression(getELContext(), binding, String.class);
return ve;
}
public static ELContext getELContext() {
return FacesContext.getCurrentInstance().getELContext();
}
public static ExpressionFactory getExpressionFactory() {
return getApplication().getExpressionFactory();
}
public static Application getApplication() {
return FacesContext.getCurrentInstance().getApplication();
}
我的表单豆在下面
public void save() {
logger.info("Saving role - {}" , role);
}
我正在使用 Primefaces 3.2、Mojarra 2.1.7、Tomcat 7、JDK 6、Ubuntu 11
这是我修改后的代码 是的,我已经看到您指出这是常见错误。但这是我修改后的代码。这也不起作用。
public Panel getPanel() {
if (panel == null) {
panel = new Panel();
panel.setHeader("Test");
panel.setId("dynapanel");
InputText text = new InputText();
text.setId("dynatext");
final String binding = "#{roleCreateForm.role.name}";
text.setValueExpression("value", createValueExpression(binding, String.class));
panel.getChildren().add(text);
CommandButton button = new CommandButton();
button.setValue("Save");
MethodExpression me = getExpressionFactory().createMethodExpression(getELContext(), "#{roleCreateForm.save}", void.class, new Class[0]);
AjaxBehavior ajaxBehavior = new AjaxBehavior();
//ajaxBehavior.setListener( me );
ajaxBehavior.addAjaxBehaviorListener( new AjaxBehaviorListenerImpl( me ) );
button.addClientBehavior( "submit", ajaxBehavior);
panel.getChildren().add(button);
}
return panel;
}