我要生成一个栏目的树结构,用递归实现,但生成出来三级或三级以上的都有问题,望高手指点下.下面是我的代码和运行结果
public ActionForward treeModule(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
response.setCharacterEncoding("UTF-8");
response.setContentType("text/xml");
Document document = new Document();
Element tree = new Element("tree");
List pm = moduleService.findFModuleList();
treeModule(tree, pm);
document.addContent(tree);
XMLOutputter out = new XMLOutputter(Format.getPrettyFormat()
.setOmitDeclaration(true));
out.output(document, System.out);
PrintWriter writer = response.getWriter();
writer = response.getWriter();
out.output(document, writer);
return null;
} public Element treeModule(Element parent, List pm) throws Exception {
Element itemRoot = null;
if (pm != null) {
for (int i = 0; i < pm.size(); i++) {
BaSystemModule module = (BaSystemModule) pm.get(i);
String mid = module.getModuleId();
List cm = moduleService.findSubModByFId(module.getModuleId());
itemRoot = new Element("item");
itemRoot.setAttribute("text", module.getModuleName());
parent.addContent(itemRoot);
System.out.println(module.getModuleName());
if (cm != null && cm.size() > 0) {
treeModule(itemRoot, cm);
} }
}
return parent;
}
- <tree>
- <item text="代码表管理">
  <item text="代码表管理" /> 
  </item>
- <item text="数据库表管理">
  <item text="新增数据库表" /> 
  <item text="修改数据库表" /> 
  </item>
- <item text="自定义视图">
  <item text="自定义视图" /> 
  </item>
- <item text="系统管理">
- <item text="用户管理">
- <item text="qqqq">
  <item text="4444" /> 
  </item>
  </item>
  <item text="角色管理" /> 
  </item>
- <item text="用户管理">
- <item text="qqqq">
  <item text="4444" /> 
  </item>
  </item>
- <item text="系统模块管理">
  <item text="系统模块管理" /> 
  </item>
- <item text="qqqq">
  <item text="4444" /> 
  </item>
  </tree>