[英]Recursion in JSF (c:forEach vs. ui:repeat)
我正在尝试通过 JSF 中的递归构建导航树。 我已经定义了一个navigationNode
组件:
<composite:interface>
<composite:attribute name="node" />
</composite:interface>
<composite:implementation>
<ul>
<ui:repeat value="#{navigationTreeBean.getChildrenForNode(cc.attrs.node)}" var="child">
<li><navigation:navigationNode node="#{child}" /></li>
</ui:repeat>
</ul>
</composite:implementation>
我的树被声明为:
rootNode = new DefaultMutableTreeNode(new NodeData("Dashboard", "dashboard.xhtml"), true);
DefaultMutableTreeNode configurationsNode = new DefaultMutableTreeNode(new NodeData("Configurations", "configurations.xhtml"), true);
rootNode.add(configurationsNode);
我通过以下方式调用组件:
<nav:navigationNode node="#{rootNode}" />
问题是,这会导致StackOverflowError
。
有一些关于在 JSF 中构建递归的参考资料(例如, c:forEach 与 ui:repeat in Facelets )。 常见的问题似乎是混合构建时和渲染时组件/标签。 就我而言:
子组件navigation:navigationNode
真的在ui:repeat
组件之前处理? 如果是这样,它用于#{child}
对象是什么? 它是否为空(似乎并非如此)? 这里的问题是实际上创建了子组件而不关心 ui:repeat 等每次创建一个新的子组件时,即使它不一定需要?
Facelets文章中的c:forEach 与 ui:repeat对此(递归)有一个单独的部分。 建议使用c:forEach
代替。 我试过这个,但是它仍然给我相同的StackOverflowError
,具有我无法理解的不同跟踪。
我知道我们也可以通过扩展UIComponent
来构建组件,但是这种方法(用 Java 代码编写 html)看起来很丑陋。 我宁愿使用 MVC 样式/模板。 但是,如果没有其他方法,我是否必须将这种递归实现为 UIComponent ?
JSF 的内置声明性标签不适合处理这种递归。 JSF 构建了一个在请求之间持久化的有状态组件树。 如果视图在后续请求中恢复,则视图状态可能不会反映模型中的更改。
我赞成一种命令式的方法。 在我看来,您有两个选择:
binding
属性将控件(例如某种形式的面板)绑定到提供UIComponent
实例及其子项的支持 bean - 您编写代码来实例化UIComponent
并添加您想要的UIComponent
项。 请参阅binding
属性协定的规范。UIComponent
; Renderer
; 标签处理程序; 元数据文件(根据需要删除 - 您可以根据您正在做什么以及如何以及在哪个版本的 JSF 中执行其中的部分或全部操作)。也许另一种选择是选择已经执行此操作的第 3 方控件。
更新:如果有人正在使用非常有用的OmniFaces 库(如果你还没有,你应该使用),有<o:tree>
它没有任何 html 生成,但专门设计用于支持这样的用例。
<o:tree value="#{bean.treeModel}" var="item" varNode="node">
<o:treeNode>
<ul>
<o:treeNodeItem>
<li>
#{node.index} #{item.someProperty}
<o:treeInsertChildren />
</li>
</o:treeNodeItem>
</ul>
</o:treeNode>
</o:tree>
编辑:
这是一种模型驱动的方法,它不涉及编写自定义组件或支持 bean 生成的组件树。 有点丑。
Facelets 视图:
<?xml version='1.0' encoding='UTF-8' ?>
<!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:ui="http://java.sun.com/jsf/facelets">
<h:head><title>Facelet Tree</title></h:head>
<h:body>
<ul>
<ui:repeat value="#{tree.treeNodes}" var="node">
<h:outputText rendered="#{node.firstChild}"
value="<ul>" escape="false" />
<li>
<h:outputText value="#{node.value}" />
</li>
<ui:repeat rendered="#{node.lastChild and empty node.kids}"
value="#{node.lastChildLineage}" var="ignore">
<h:outputText
value="</ul>" escape="false" />
</ui:repeat>
</ui:repeat>
</ul>
</h:body>
</html>
托管 bean:
@javax.faces.bean.ManagedBean(name = "tree")
@javax.faces.bean.RequestScoped
public class Tree {
private Node<String> root = new Node(null, "JSF Stuff");
@PostConstruct
public void initData() {
root.getKids().add(new Node(root, "Chapter One"));
root.getKids().add(new Node(root, "Chapter Two"));
root.getKids().add(new Node(root, "Chapter Three"));
Node<String> chapter2 = root.getKids().get(1);
chapter2.getKids().add(new Node(chapter2, "Section A"));
chapter2.getKids().add(new Node(chapter2, "Section B"));
}
public List<Node<String>> getTreeNodes() {
return walk(new ArrayList<Node<String>>(), root);
}
private List<Node<String>> walk(List<Node<String>> list, Node<String> node) {
list.add(node);
for(Node<String> kid : node.getKids()) {
walk(list, kid);
}
return list;
}
}
一个树节点:
public class Node<T> {
private T value;
private Node<T> parent;
private LinkedList<Node<T>> kids = new LinkedList<>();
public Node(Node<T> parent, T value) {
this.parent = parent;
this.value = value;
}
public List<Node<T>> getKids() {return kids;}
public T getValue() { return value; }
public boolean getHasParent() { return parent != null; }
public boolean isFirstChild() {
return parent != null && parent.kids.peekFirst() == this;
}
public boolean isLastChild() {
return parent != null && parent.kids.peekLast() == this;
}
public List<Node> getLastChildLineage() {
Node node = this;
List<Node> lineage = new ArrayList<>();
while(node.isLastChild()) {
lineage.add(node);
node = node.parent;
}
return lineage;
}
}
输出:
* JSF Stuff
o Chapter One
o Chapter Two
+ Section A
+ Section B
o Chapter Three
我仍然会咬紧牙关写一个自定义的树控件。
在将我们的应用程序从 jsf 1.x 迁移到 2.x 时,我遇到了类似的问题(StackOverflowException)。 如果您使用 c:forEach 方法进行 jsf 递归,请确保您使用的是新的 jstl 核心命名空间。 用
xmlns:c="http://java.sun.com/jsp/jstl/core"
代替
xmlns:c="http://java.sun.com/jstl/core"
这是我们正在使用的模式,适用于您的场景。
客户端.xhtml
<ui:include src="recursive.xhtml">
<ui:param name="node" value="#{child}" />
</ui:include>
递归.xhtml
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jsp/jstl/core" >
<ul>
<c:forEach items="#{node.children}" var="child">
<li>
#{child.label}
<ui:include src="recursive.xhtml">
<ui:param name="node" value="#{child}" />
</ui:include>
</li>
</c:forEach>
</ul>
</ui:composition>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.