MethodExpression未在HtmlCommandLink中触发

时间:2015-04-09 16:49:14

标签: jsf primefaces

我有一个动态生成的数据表,就像这个

    DataTable dataTable = new DataTable();
    dataTable.setValue(relatorioVOList);
    dataTable.setVar("rVO");

    Column checkBoxColumn = new Column();
    checkBoxColumn.getChildren().add(this.viewComponentBuilder.createExpressionTextWithLink("#{rVO.iRelatorio}","#{rVO.nNome}"));
    dataTable.getColumns().add(checkBoxColumn);


public HtmlForm createExpressionTextWithLink(String iRelatorioExpressionValue, String valueExpressionValue) {
    HtmlForm form = new HtmlForm();
    HtmlCommandLink link = new HtmlCommandLink();

    //config
    FacesContext context = FacesContext.getCurrentInstance(); 
    Application application = context.getApplication();
    ExpressionFactory ef = application.getExpressionFactory();
    ELContext elc = context.getELContext();

    //value that is the reports name
    ValueExpression nameValueExp = ef.createValueExpression(elc, valueExpressionValue, Object.class);
    link.setValueExpression("value", nameValueExp);

    //action that goes to method teste when link is clicked
    MethodExpression methodExpression = createMethodExpression("#{componenteC.teste(rVO.iRelatorio)}", String.class, Integer.class);
    link.setActionExpression(methodExpression);

    form.getChildren().add(link);
    return form;
}

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

在ComponenteC中,一个RequestScopedBean,即teste函数

public String teste(Integer iRelatorio) {
    System.out.println("cheguei");
    return "componente";
}

目标是teste函数将根据iRelatorio参数生成url。 这里的问题是永远不会调用该函数。我尝试用明确的10替换rVO.iRelatorio,#34;#{componenteC.teste(10)}&#34;即使这样,行动似乎也没有被解雇。 报告名称显示正确。

1 个答案:

答案 0 :(得分:7)

动态创建的UIInputUICommandUINamingContainer组件必须已分配固定id。否则它将获得一个自动生成的视图,当视图恢复时,它不一定相同。组件ID用于提交的表单数据中的请求参数名称,然后JSF将用于收集提交的输入值并在应用请求值阶段识别调用的命令。如果组件ID发生更改,则JSF无法按照预期执行应用请求值阶段。

因此,采取相应的行动:

dataTable.setId("tableId");
// ...
form.setId("formId");
// ...
link.setId("linkId");

还有其他可能的原因,但在问题中到目前为止提供的信息中看不到它们。为了解决这个问题,请花点时间仔细阅读以下相关答案,并动态地&#34;创建组件/视图:

也就是说,使用XHTML来声明和创建组件而不是所有Java代码混乱,你真的更好。 XHTML(+ XML)更具说明性和可读性,因此更易于理解和维护。 JSTL may be very helpful in this all