MethodExpression 未在 HtmlCommandLink 中触发
MethodExpression not firing in HtmlCommandLink
我有一个动态生成的数据表,像这样
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。
这里的问题是该函数从未被调用。我尝试将 rVO.iRelatorio 替换为明确的 10,“#{componenteC.teste(10)}”,即使如此,该操作似乎也没有被触发。
报告名称显示正确。
动态创建的 UIInput
, UICommand
and UINamingContainer
组件 必须 分配固定的 id
。否则它会得到一个自动生成的视图,在恢复视图时不一定相同。组件 ID 用于提交的表单数据中的请求参数名称,然后 JSF 将使用它来收集提交的输入值并在应用请求值阶段识别调用的命令。如果组件 ID 更改,则 JSF 将无法按预期执行应用请求值阶段。
因此,采取相应的行动:
dataTable.setId("tableId");
// ...
form.setId("formId");
// ...
link.setId("linkId");
还有其他潜在原因,但在问题中到目前为止提供的信息中看不到它们。为了解决这个问题,请花点时间仔细阅读以下有关 "dynamically" 创建 components/views:
的相关答案
- Create inputtext dynamically
- How does the 'binding' attribute work in JSF? When and how should it be used?
- How to create dynamic JSF form fields
也就是说,您最好使用 XHTML 来声明和创建组件,而不是所有 Java 代码混乱。 XHTML(+XML) 更具声明性和可读性,因此更易于理解和维护。 JSTL may be very helpful in this all.
我有一个动态生成的数据表,像这样
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。 这里的问题是该函数从未被调用。我尝试将 rVO.iRelatorio 替换为明确的 10,“#{componenteC.teste(10)}”,即使如此,该操作似乎也没有被触发。 报告名称显示正确。
动态创建的 UIInput
, UICommand
and UINamingContainer
组件 必须 分配固定的 id
。否则它会得到一个自动生成的视图,在恢复视图时不一定相同。组件 ID 用于提交的表单数据中的请求参数名称,然后 JSF 将使用它来收集提交的输入值并在应用请求值阶段识别调用的命令。如果组件 ID 更改,则 JSF 将无法按预期执行应用请求值阶段。
因此,采取相应的行动:
dataTable.setId("tableId");
// ...
form.setId("formId");
// ...
link.setId("linkId");
还有其他潜在原因,但在问题中到目前为止提供的信息中看不到它们。为了解决这个问题,请花点时间仔细阅读以下有关 "dynamically" 创建 components/views:
的相关答案- Create inputtext dynamically
- How does the 'binding' attribute work in JSF? When and how should it be used?
- How to create dynamic JSF form fields
也就是说,您最好使用 XHTML 来声明和创建组件,而不是所有 Java 代码混乱。 XHTML(+XML) 更具声明性和可读性,因此更易于理解和维护。 JSTL may be very helpful in this all.