我有一个项目使用JSF primefaces和EJB管理Beans与hibernate存储。 http://www.primefaces.org/showcase/ui/treeAjax.jsf
问题:我一直看到内存不足或堆栈溢出错误,你看到我的递归有问题吗?
@ManagedBean(name = "categoryController")
@RequestScoped
public class CategoryController implements Serializable {
...
@EJB
private CategoryFacade ejbFacade;
private TreeNode root;
public TreeNode getRoot() {
return root;
}
@PostConstruct
public void init() {
root = new DefaultTreeNode("Root", null);
System.out.print("Facade value=" + getFacade());
Category categoryRoot = getFacade().find(new Integer(1));
getSubcategories(categoryRoot, root);
}
private void getSubcategories(Category categoryRoot, TreeNode root) {
List<Category> list = getFacade().findByNamedQuery("Category.findByPCatid", "pcatid", categoryRoot);
Iterator<Category> it = list.iterator();
while (it.hasNext()) {
Category subcategory = it.next();
TreeNode nextNode = new DefaultTreeNode(subcategory.getUcatid(), root);
getSubcategories(subcategory, nextNode);
}
}
...
}
答案 0 :(得分:0)
问题是你的递归在你的根元素上是一个无限循环。 尝试添加:
if (!subcategory.getCatid().equals(new Integer(1))) {
getSubcategories(subcategory, nextNode);
}
您还可以查找Primefaces事件侦听器以按需生成树。 http://www.primefaces.org/showcase/ui/treeEvents.jsf