java JSF 中的递归(c:forEach 与 ui:repeat)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3493395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Recursion in JSF (c:forEach vs. ui:repeat)
提问by Tuukka Mustonen
I am trying to build a navigation tree via recursion in JSF. I have defined a navigationNodecomponent as:
我正在尝试通过 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>
My tree is declared as:
我的树被声明为:
rootNode = new DefaultMutableTreeNode(new NodeData("Dashboard", "dashboard.xhtml"), true);
DefaultMutableTreeNode configurationsNode = new DefaultMutableTreeNode(new NodeData("Configurations", "configurations.xhtml"), true);
rootNode.add(configurationsNode);
I call component by:
我通过以下方式调用组件:
<nav:navigationNode node="#{rootNode}" />
The problem is, this results in StackOverflowError.
问题是,这导致StackOverflowError.
There are a few references to building recursion in JSF (for example, c:forEach vs ui:repeat in Facelets). The common problem seems to be mixing the build-time and render-time components/tags. In my case:
有一些关于在 JSF 中构建递归的参考资料(例如,c:forEach 与 ui:repeat in Facelets)。常见的问题似乎是混合构建时和渲染时组件/标签。就我而言:
- My composite component is actually a tag, which is executed when the tree is built
- ui:repeat is an actual JSF component, which is evaluated when the tree is rendered
- 我的复合组件其实就是一个标签,在树构建的时候执行
- ui:repeat 是一个实际的 JSF 组件,它在树被渲染时被评估
Is the child component navigation:navigationNodeactually processed before the ui:repeatcomponent? If so, what object is it using for #{child}? Is it null (doesn't seem so)? Is the problem here that the child component is actually created without even caring about the ui:repeat and so each time a new child component is created even though it is not necessarily wanted?
子组件navigation:navigationNode真的是在组件之前处理的ui:repeat吗?如果是这样,它用于什么对象#{child}?它是否为空(似乎并非如此)?这里的问题是实际上创建了子组件而不关心 ui:repeat 等每次创建一个新的子组件时,即使它不一定需要?
The c:forEach vs ui:repeat in Faceletsarticle has a separate section for this (recursion). The suggestion is to to use c:forEachinstead. I tried this, however it is still giving me the same StackOverflowError, with different trace that I cannot make sense of.
Facelets文章中的c:forEach 与 ui:repeat对此(递归)有一个单独的部分。建议是c:forEach改用。我试过这个,但是它仍然给我同样的StackOverflowError,我无法理解的不同痕迹。
I know that we can also build components by extending UIComponent, but that approach (writing html in Java code) seems ugly. I would rather use MVC style / templates. However, if there are no other ways, do I have to implement this sort of recursion as UIComponent instead?
我知道我们也可以通过扩展来构建组件UIComponent,但是这种方法(在 Java 代码中编写 html)看起来很丑陋。我宁愿使用 MVC 样式/模板。但是,如果没有其他方法,我是否必须将这种递归实现为 UIComponent ?
采纳答案by McDowell
JSF's built-in declarative tags are ill-suited for handling this sort of recursion. JSF builds a stateful component tree that is persisted between requests. If the view is restored in a subsequent request, the view state may not reflect changes in the model.
JSF 的内置声明性标签不适合处理这种递归。JSF 构建了一个在请求之间持久化的有状态组件树。如果视图在后续请求中恢复,则视图状态可能不会反映模型中的更改。
I would favour an imperative approach. You have two options as I see it:
我赞成一种命令式的方法。在我看来,您有两个选择:
- Use the
bindingattribute to bind a control (e.g. some form of panel) to a backing bean that provides theUIComponentinstance and its children - you write code to instantiate theUIComponentand add whatever children you want. See the spec for thebindingattribute contract. - Write a custom control, implementing some of: a
UIComponent; aRenderer; a tag handler; meta-data files (delete as appropriate - you do some or all of these depending on what you are doing and how and in which version of JSF).
- 使用该
binding属性将控件(例如某种形式的面板)绑定到提供UIComponent实例及其子项的支持 bean - 您编写代码来实例化UIComponent并添加您想要的任何子项。请参阅binding属性契约的规范。 - 编写自定义控制,实现一些:一
UIComponent; 一个Renderer; 标签处理程序;元数据文件(根据需要删除 - 您可以执行其中的部分或全部操作,具体取决于您正在做什么以及如何以及在哪个版本的 JSF 中)。
Perhaps another option is to pick up a 3rd party control that already does this.
也许另一种选择是选择已经执行此操作的第 3 方控件。
UPDATE:If one is using the very useful OmniFaces library(you should if you don't already), there is the <o:tree>which has no html generation whatsoever but was specifically designed to support usecases like this.
更新:如果有人正在使用非常有用的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>
EDIT:
编辑:
Here's a model-driven approach that doesn't involve writing custom components or backing-bean-generated component trees. It's kind of ugly.
这是一种模型驱动的方法,它不涉及编写自定义组件或支持 bean 生成的组件树。有点丑
The Facelets view:
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>
The managed bean:
托管 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;
}
}
A tree node:
一个树节点:
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;
}
}
Output:
输出:
* JSF Stuff
o Chapter One
o Chapter Two
+ Section A
+ Section B
o Chapter Three
I would still bite the bullet and write a custom tree control.
我仍然会咬紧牙关写一个自定义的树控件。
回答by Anonymous
I had a similar issue(StackOverflowException) while migrating our app from jsf 1.x to 2.x. If you're using the c:forEach approach to jsf recursion, make sure you're using the new namespace for jstl core. Use
在将我们的应用程序从 jsf 1.x 迁移到 2.x 时,我遇到了类似的问题(StackOverflowException)。如果您使用 c:forEach 方法进行 jsf 递归,请确保您使用的是新的 jstl 核心命名空间。利用
xmlns:c="http://java.sun.com/jsp/jstl/core"
instead of
代替
xmlns:c="http://java.sun.com/jstl/core"
Here's the pattern we we're using, adapted to your scenario.
这是我们正在使用的模式,适用于您的场景。
client.xhtml
客户端.xhtml
<ui:include src="recursive.xhtml">
<ui:param name="node" value="#{child}" />
</ui:include>
recursive.xhtml
递归.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>

