显示树的页面
<
script type
=
"
text/javascript
"
>

var tree
=
new
WebFXLoadTree(
"
Hello World
"
,
"treeView.do
"
);
tree.write();
</
script
>
treeView.do对应的action方法
public
ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
...
{
if(form instanceof TreeForm)...{
try ...{
TreeForm f = (TreeForm)form;
TreeBo bo = new TreeBo();
bo.getTreeList(f.getDeptNo(),response);

} catch (IOException e) ...{
e.printStackTrace();
} catch (NamingException e)...{
e.printStackTrace();
} catch (SQLException e)...{
e.printStackTrace();
}
}
return null;
}
action调用的方法
public
void
getTreeList(String deptNo,HttpServletResponse response)
throws
SQLException,IOException
...
{
Connection t = dataSource.getConnection();
PreparedStatement p = t.prepareCall("select a.deptname name,a.deptcode code,(select count(b.deptcode)-1 from t_dept b where b.deptcode like a.deptcode||'%') num from t_dept a where length(a.deptcode)=nvl(length(?),0)+4 and a.deptcode like ? || '%'");
String tmp = deptNo;
p.setString(1,deptNo);
p.setString(2,tmp);
ResultSet set = p.executeQuery();
response.setCharacterEncoding("gb2312");
response.setContentType("text/xml charset=gb2312");
PrintWriter out = response.getWriter();
out.write("<?xml version="1.0" encoding="gb2312"?>");
out.write("<tree>");
while(set.next())...{
out.write("<tree text="""+set.getString("name")+""" "+(set.getInt("num")>0?"src="treeView.do?deptNo="+set.getString("code")+""":"action="xxx.html"")+"/>");
}
out.write("</tree>");
out.flush();
set.close();
p.close();
t.close();
}
通过sql得到此级别的全部元素和每个此级别元素所包含的子元素数量,以便判断在显示层是否需要显示 “+“ 号
欢迎交流
本文介绍了一个使用Java实现的动态加载树形结构的方法。通过后台获取数据并返回XML格式供前端展示,实现了根据父节点动态加载子节点的功能。
808

被折叠的 条评论
为什么被折叠?



