xtree在struts下动态加载树

来源:互联网 发布:火影忍者特效ps软件 编辑:程序博客网 时间:2024/05/17 07:49

 显示树的页面

 

<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="&quot;"+set.getString("name")+"&quot;" "+(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得到此级别的全部元素和每个此级别元素所包含的子元素数量,以便判断在显示层是否需要显示 “+“ 号

欢迎交流

 

原创粉丝点击