在Primefaces中编程创建命令按钮 [英] Programatically create command button in Primefaces

查看:114
本文介绍了在Primefaces中编程创建命令按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个带有输入文本和命令按钮的动态表单。一切都很好但是当我点击命令按钮时,动作监听器从不被调用。请建议我做错什么,或者这是PF或Mojarra的错误。代码在

I am trying to create a dynamic form with input text and command button. Everything works fine. But when I click on the command button, the action listener is never called. Please suggest what I am doing wrong or if this is a bug with PF or Mojarra. The code is below

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还在

Also the createXXXExpression are below

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

我使用
原始3.2,Mojarra 2.1.7,Tomcat 7,JDK 6,Ubuntu 11

I am using Primefaces 3.2, Mojarra 2.1.7, Tomcat 7, JDK 6 , Ubuntu 11

这是我的修改后的代码
是的,我看到你已经指出这是常见的错误。但这是我的修改后的代码。这也不行。

Here is my modified code Yes I have seen that you have pointed out this as the common mistake. But here is my modified code. This does not work either.

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


推荐答案

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:

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

这适用于我;)

这篇关于在Primefaces中编程创建命令按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆