struts2 dojo 实现动态树

1.首先写两个工具类:TreeData.java 和 TreeUtil.java

TreeData.java

package com.zjh.shopping.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class TreeData {
    public static Map<Integer, TreeData> catMap = new HashMap<Integer, TreeData>();
   
    public static TreeData getById(int id) {
        return catMap.get(id);
    }
   
    private int id;
    private String name;
    private List<TreeData> children;
    private boolean toggle;
    public TreeData(int id, String name, TreeData... children) {
        this.id = id;
        this.name = name;
        this.children = new ArrayList<TreeData>();
        for (TreeData child : children) {
            this.children.add(child);
        }
        catMap.put(id, this);
    }
    public TreeData(){}
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public List<TreeData> getChildren() {
        return children;
    }
    public void setChildren(List<TreeData> children) {
        this.children = children;
    }
    public void toggle() {
        toggle = !toggle;
    }
    public boolean isToggle() {
        return toggle;
    }
}

TreeUtil.java

 package com.zjh.shopping.util;

import java.util.ArrayList;
import java.util.List;

import com.zjh.shopping.dao.TreeBaseDao;


 
public class TreeUtil {
 List<Integer> childrenId;
    public TreeData createTreeData(int parentId,TreeBaseDao treeBaseDao){
      TreeData treeData = new TreeData();
      treeData.setId(parentId);
      treeData.setName(treeBaseDao.getObjectName(parentId)); 
      
        List<TreeData> children = new ArrayList<TreeData>();
     
     childrenId = treeBaseDao.getChildrenId(parentId);
      if(childrenId!=null){
       for(int id :childrenId){
       TreeData c1 = createTreeData(id,treeBaseDao);
       children.add(c1);
       }
       treeData.setChildren(children);
       TreeData.catMap.put(parentId, treeData);
       return treeData;
      }
       else
           TreeData.catMap.put(parentId, treeData);
           return treeData;
       }
   }

2.写dao和它的实现类:

package com.zjh.shopping.dao;

import java.util.List;

public interface TreeBaseDao {
 
 public List<Integer> getChildrenId(int parentId);
 
 public String getObjectName(int id);
}

package com.zjh.shopping.dao;

import com.zjh.shopping.bean.Category;

public interface CategoryDao extends BasicDao<Category>,TreeBaseDao{
   public int getMaxCnoByPid(int pid);
   public int getCnobyId(int id);
}

 

package com.zjh.shopping.dao.impl;

import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.zjh.shopping.bean.Category;
import com.zjh.shopping.dao.CategoryDao;

public class CategoryDaoImpl extends HibernateDaoSupport implements CategoryDao {

 


 public List<Integer> getChildrenId(int parentId) {
  List<Integer> list = null;
  String hql = "select id from Category";
  if(parentId==0){
   hql += " where pid is null";
   
  }else{
   hql += " where pid ="+parentId;
  }
  list = getHibernateTemplate().find(hql);
  return list;
 }

 public String getObjectName(int id) {
  String name = "";
  if(id==0){
   name = "所有类别";
  }else{
   name = getEntity(id).getName();
  }
  return name;
 }

}

3.写action

package com.zjh.shopping.action;


import com.opensymphony.xwork2.ActionSupport;
import com.zjh.shopping.dao.CategoryDao;
import com.zjh.shopping.util.TreeData;
import com.zjh.shopping.util.TreeUtil;

public class ShowCategoryDynamicTreeAction extends ActionSupport {
 private CategoryDao categoryDao;
 public CategoryDao getCategoryDao() {
  return categoryDao;
 }
 public void setCategoryDao(CategoryDao categoryDao) {
  this.categoryDao = categoryDao;
 }


 
 public TreeData getTreeRootNode() {
      TreeUtil treeUtil = new TreeUtil();//call a tree util
      treeUtil.createTreeData(0,categoryDao);//input a corresponding dao
      System.out.println("TreeData.getById(1)  ........."+TreeData.getById(0).getName());
         return TreeData.getById(0);
  }
}

4.显示层页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sx" uri="/struts-dojo-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <meta http-equiv="Pragma" content="no-cache">
  <meta http-equiv="expires" content="Wed, 23 Aug 2006 12:40:27 UTC" />
  <title>类别树形列表</title>
  <link href="css/Main.css" type="text/css" rel=stylesheet>
  <sx:head />
 </head>
 <body>
  <script language="javaScript" type="text/javascript">
 //dojo tree 加载页面后一次性展开树所有节点
 dojo.addOnLoad( function() {
  expandObj(dojo.widget.byId('categoryTreeId'));//为下面标签的ID
  });
 function expandObj(obj) {
  if (obj) {
   if (obj.children) {
    for ( var i = 0; i < obj.children.length; i++) {
     var childObj = obj.children[i];
     if (childObj) {
      try {
       childObj.expand();
      } catch (e) {
      }
      if (childObj.children) {
       expandObj(childObj);
      }
     }
    }
   }
  }
 }
    //点击树的节点时候触发的事件
 dojo.event.topic.subscribe("treeSelected", function treeNodeSelected(node) {
  dojo.io.bind( {
   url :"<s:url value='showCategoryDynamicTree.action'/>",
   load : function(type, data, evt) {
    //var divDisplay = dojo.byId("displayId");
 
   if (node.node.isFolder == false) {
    //alert("12");
   var cNode = document.getElementByIdx("categoryName");
   cNode.value = node.node.title;

  // cNode.value = node.node.widgetId;
   window.parent.fillStaff(rtnValue);
  }
 },
 mimeType :"text/Html"
  });
 });
</script>

  <input type="text" id="categoryName" readonly="readonly">
  <div>
   <sx:tree id="categoryTreeId" rootNode="%{treeRootNode}"
    childCollectionProperty="children" nodeIdProperty="id"
    nodeTitleProperty="name" treeSelectedTopic="treeSelected">
   </sx:tree>
  </div>
  <div id="displayId" style="display: none;">
   Please click on any of the tree nodes.
  </div>

 </body>
</html>

 

5.配置文件

<action name="showCategoryDynamicTree" class="showCategoryDynamicTreeAction">
       <result>/WEB-INF/category/categoryTreeDOJO.jsp</result>
 </action>

 

6.数据库


struts2 dojo 实现动态树_第1张图片
 

7.显示结果


struts2 dojo 实现动态树_第2张图片
 

来自:http://blog.sina.com.cn/s/blog_6145ed810100hawn.html

 

 

你可能感兴趣的:(java,DAO,css,struts,dojo)