这可能吗?经过几个小时的战斗,我放弃了。
JSF 表是通过编程方式创建的,并且内部还有命令链接。
下面的代码段在表外部和内部都有 commandLink。两者的创建方式相似。 XHTML完整代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core">
<f:view contentType="text/html" locale="pl">
<h:body>
<h:form id="form">
<h:dataTable binding="#{dynamicDataTable.table}" value="#{dynamicDataTable.tableContent}" />
<!-- this one below works fine -->
<h:commandLink binding="#{dynamicDataTable.link}" />
</h:form>
</h:body>
</f:view>
</html>
动态数据表 bean:
@Named
@ViewScoped
public class DynamicDataTable implements Serializable {
private static final long serialVersionUID = 1L;
private HtmlDataTable table;
private HtmlCommandLink link;
private List<String> tableContent;
public void action() {
System.out.println("Action performed");
}
public HtmlDataTable getTable() {
table = new HtmlDataTable();
HtmlCommandLink inlink = new HtmlCommandLink();
inlink.setValue("Inside link");
inlink.setActionExpression(createMethodExpression("#{dynamicDataTable.action}", String.class));
UIColumn column = new UIColumn();
column.getChildren().add(inlink);
table.getChildren().add(column);
return table;
}
public List<String> getTableContent() {
tableContent = new ArrayList<String>();
tableContent.add("a");
tableContent.add("b");
return tableContent;
}
public void setTableContent(List<String> tableContent) {
this.tableContent = tableContent;
}
public void setTable(HtmlDataTable table) {
this.table = table;
}
public HtmlCommandLink getLink() {
link = new HtmlCommandLink();
link.setValue("Outside link");
link.setActionExpression(createMethodExpression("#{dynamicDataTable.action}", String.class));
return link;
}
public void setLink(HtmlCommandLink link) {
this.link = link;
}
public static MethodExpression createMethodExpression(String expression, Class<?> returnType) {
FacesContext context = FacesContext.getCurrentInstance();
return context.getApplication().getExpressionFactory().createMethodExpression(
context.getELContext(), expression, returnType, new Class[0]);
}
}
表内的链接已创建,但不起作用。 在表外创建的,也是以编程方式创建的,效果很好。
有什么想法吗?
最佳答案
简要说明
您遇到的上述问题是由于 View 处理程序无法处理您的操作链接而引起的,因为它无法为链接组件生成ID - 这意味着对激活的组件的引用实际上失踪了。
你的两个例子实际上都不太正确。然而,通过在表外部生成链接, View 处理程序能够以某种方式找出组件的 id,并在执行操作时最终到达正确的位置。
查看代码
如果我们查看调用上述代码时 JSF Mojarra 生成的内容,我们可以看到以下内容:
表内的链接获取到 form:j_idt3:0:j_id3
的链接,但没有归因于该组件的 ID,因此该链接不起作用。
表格外部的链接获取到 form:j_idt5
的链接,并生成 id j_id1:javax.faces.ViewState:0
。因此,虽然它确实调用了该操作 - 链接/id 相关性并不完全正确。
很明显,渲染器能够计算出链接的生成 id,但它永远不会将该生成的 id 设置到实际的组件/标签上。这有点问题。
解决问题
这里的解决方案是帮助 JSF 和 View 处理程序找出带有操作的组件的路径。您可以通过在以编程方式生成命令链接时强制设置 id 来实现此目的 - 在 getTable()
方法内添加以下代码;
inlink.setId("link");
这应该允许渲染器渲染带有工作操作链接的有效页面。
关于java - 在表内以编程方式创建的 JSF commandLink 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54274084/