【问题标题】:Programatically create command button in Primefaces在 Primefaces 中以编程方式创建命令按钮
【发布时间】:2012-05-12 21:07:42
【问题描述】:

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

【问题讨论】:

  • 这个面板在一个表单标签里面表单标签在模板xhtml中。这与这种奇怪的行为有什么关系吗
  • 只需使用 XHTML 而不是 Java。

标签: dynamic jsf-2 components primefaces


【解决方案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);

【讨论】:

  • 不幸的是,这也不起作用。我已经稍微改变了策略,请看下一个。
  • 你解决了吗?好吧,我有一个类似的问题,我还有一些步骤可以解决它(我在 primefaces 论坛中找到了解决这个问题的方法)。看看这里:stackoverflow.com/questions/15808956/…
【解决方案2】:
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);
}

这对我有用;)

【讨论】:

  • 这不是以编程方式创建按钮(就像问题一样),而是向已经可用的按钮添加功能
  • “一切正常。但是当我点击命令按钮时,动作监听器永远不会被调用。请指出我做错了什么”......我认为将监听器添加到新按钮是一样
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-27
  • 2014-10-16
  • 2015-10-01
  • 1970-01-01
  • 2023-03-25
相关资源
最近更新 更多