在 Primefaces 中以编程方式创建命令按钮

2024-06-19

我正在尝试创建一个带有输入文本和命令按钮的动态表单。一切正常。但是当我单击命令按钮时,永远不会调用操作侦听器。请提出我做错了什么或者这是否是 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();
}

我的表单 bean 如下

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;
}               

据我记得,如果您想调用支持 bean 中的方法,请使用 MethodExpression 作为 AjaxBehaviour 的侦听器:

        AjaxBehavior ab1 = new AjaxBehavior();
        ExpressionFactory ef = ctx.getApplication().getExpressionFactory();
        MethodExpression me1 = ef.createMethodExpression(ctx.getELContext(),
                                     expression,//Your ELExpression #{roleCreateForm.save}
                                     expectedReturnType, //In your case null
                                     expectedParamTypes); //If you receive parameters put new Class[]{Object.class});
        ab1.setListener(me1);
        button.addClientBehavior( "submit", ab1);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Primefaces 中以编程方式创建命令按钮 的相关文章

随机推荐