我有一个动态生成的数据表,如下所示
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";
}目的是根据iRelatorio参数生成一个url。这里的问题是,函数从未被调用过。我尝试用一个显式的10,“#{ rVO.iRelatorio (10)}”替换这个componenteC.teste,即使这样,这个操作似乎也不会被触发。正确显示报表名称。
发布于 2015-04-09 17:06:02
动态创建的UIInput、UICommand和UINamingContainer组件必须为分配固定的id。否则,它将得到一个自动生成的视图,在还原视图时不一定相同。组件ID用于提交表单数据中的请求参数名称,然后JSF将使用该名称收集提交的输入值,并在apply请求值阶段标识调用的命令。如果组件ID发生变化,那么JSF将无法像预期的那样执行apply请求值阶段。
因此,采取相应行动:
dataTable.setId("tableId");
// ...
form.setId("formId");
// ...
link.setId("linkId");还有其他可能的原因,但在问题中迄今提供的资料中看不到它们。要解决这一问题,请花时间仔细阅读以下有关“动态”创建组件/视图的答案:
也就是说,您最好使用XHTML来声明和创建组件,而不是使用Java代码的混乱。XHTML(+XML)更具有声明性和可读性,因此更易于理解和维护。JSTL may be very helpful in this all。
https://stackoverflow.com/questions/29544413
复制相似问题