Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dynamic 以编程方式在Primefaces中创建命令按钮_Dynamic_Jsf 2_Components_Primefaces - Fatal编程技术网

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

Dynamic 以编程方式在Primefaces中创建命令按钮,dynamic,jsf-2,components,primefaces,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(

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

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

据我所知,如果您想在支持bean中调用方法,请使用MethodExpression作为AjaxBehavior的侦听器:

        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);
和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);
}
publicstaticmethodexpression createMethodExpression(字符串表达式、类returnType、类…参数类型){
FacesContext FacesContext=FacesContext.getCurrentInstance();
返回facesContext.getApplication().getExpressionFactory().createMethodExpression(
facesContext.getELContext(),表达式,返回类型,参数类型);
}
这对我有用;)

和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);
}
publicstaticmethodexpression createMethodExpression(字符串表达式、类returnType、类…参数类型){
FacesContext FacesContext=FacesContext.getCurrentInstance();
返回facesContext.getApplication().getExpressionFactory().createMethodExpression(
facesContext.getELContext(),表达式,返回类型,参数类型);
}

这对我有用;)

此面板位于表单标记内表单标记位于模板xhtml中。这与这种奇怪的行为有什么关系吗?只需使用XHTML而不是Java。这个面板在表单标记中,表单标记在模板XHTML中。这与这种奇怪的行为有什么关系吗?只需使用XHTML而不是Java。不幸的是,这也不起作用。我改变了一点策略,请看下一步。你解决了吗?嗯,我有一个类似的问题,为了解决这个问题,我还有更多的步骤要做(我在primefaces论坛上找到了解决这个问题的方法)。我改变了一点策略,请看下一步。你解决了吗?嗯,我有一个类似的问题,为了解决这个问题,我还有更多的步骤要做(我在primefaces论坛上找到了解决这个问题的方法)。看看这里:这不是以编程方式创建按钮(问题是这样的),而是向已经可用的按钮添加功能“一切正常。但是,当我单击命令按钮时,永远不会调用动作侦听器。请说明我做错了什么“…我认为将侦听器添加到新按钮是一样的这不是以编程方式创建按钮(问题是这样的),而是向已经可用的按钮添加功能”一切正常。但是,当我单击命令按钮时,永远不会调用动作侦听器。请说明我做错了什么“…我认为将侦听器添加到新按钮也是一样的