首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MethodExpression没有在HtmlCommandLink开火

MethodExpression没有在HtmlCommandLink开火
EN

Stack Overflow用户
提问于 2015-04-09 16:49:13
回答 1查看 165关注 0票数 1

我有一个动态生成的数据表,如下所示

代码语言:javascript
复制
    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函数

代码语言:javascript
复制
public String teste(Integer iRelatorio) {
    System.out.println("cheguei");
    return "componente";
}

目的是根据iRelatorio参数生成一个url。这里的问题是,函数从未被调用过。我尝试用一个显式的10,“#{ rVO.iRelatorio (10)}”替换这个componenteC.teste,即使这样,这个操作似乎也不会被触发。正确显示报表名称。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-09 17:06:02

动态创建的UIInputUICommandUINamingContainer组件必须为分配固定的id。否则,它将得到一个自动生成的视图,在还原视图时不一定相同。组件ID用于提交表单数据中的请求参数名称,然后JSF将使用该名称收集提交的输入值,并在apply请求值阶段标识调用的命令。如果组件ID发生变化,那么JSF将无法像预期的那样执行apply请求值阶段。

因此,采取相应行动:

代码语言:javascript
复制
dataTable.setId("tableId");
// ...
form.setId("formId");
// ...
link.setId("linkId");

还有其他可能的原因,但在问题中迄今提供的资料中看不到它们。要解决这一问题,请花时间仔细阅读以下有关“动态”创建组件/视图的答案:

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

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29544413

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档