2

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

2 回答 2

-1

据我记得,如果你想在你的支持 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);
于 2012-05-04T11:16:16.933 回答
-1
CommandButton btn = ((CommandButton) FacesContext.getCurrentInstance().getViewRoot().findComponent("full id of button"));

try{
    FacesContext context = FacesContextWrapper.getCurrentInstance();
    MethodExpressionActionListener methodExpression = new MethodExpressionActionListener(context.getApplication().getExpressionFactory()
                .createMethodExpression(context.getELContext(),"#{bean.method}", null, new Class[] {ActionEvent.class}));

    btn.addActionListener(methodExpression);
}catch(Exception exc){
    exc.printStackTrace();
}

和 createMethodExpression :

public static MethodExpression createMethodExpression(String expression, Class<?> returnType, Class<?>... parameterTypes) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    return facesContext.getApplication().getExpressionFactory().createMethodExpression(
        facesContext.getELContext(), expression, returnType, parameterTypes);
}

这对我有用;)

于 2015-08-07T08:56:18.900 回答