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

时间:2012-05-03 10:52:26

标签: dynamic jsf-2 components primefaces

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

2 个答案:

答案 0 :(得分:-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);

答案 1 :(得分:-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);
}

这对我有用;)