struts2动态树的例子(转)

系统采用Struts2+Spring+Hibernate架构,classpath分层结构如下图:

 

表结构如下图:

 

表映射为pojo为类Tree.java代码如下:

Java代码 复制代码
  1. private Integer id;   
  2.     private String name;   
  3.     private String children;       
  4.     private Tree parent;   
  5.   
  6.     public Tree() {   
  7.            
  8.     }   
  9.        
  10.        
  11.   
  12.     public Tree(Integer id, String name, String children) {   
  13.            
  14.         this.id = id;   
  15.         this.name = name;   
  16.         this.children=children;   
  17.     }   
  18.   
  19.   
  20.   
  21.     public Tree(Integer id, String name, String children, Tree parent) {   
  22.         this.id = id;   
  23.         this.name = name;   
  24.         this.children = children;   
  25.         this.parent = parent;   
  26.     }   
  27.   
  28. //省略get,set }  
private Integer id;
	private String name;
	private String children;	
	private Tree parent;

	public Tree() {
		
	}
	
	

	public Tree(Integer id, String name, String children) {
		
		this.id = id;
		this.name = name;
	    this.children=children;
	}



	public Tree(Integer id, String name, String children, Tree parent) {
		this.id = id;
		this.name = name;
		this.children = children;
		this.parent = parent;
	}

//省略get,set }

 

 Hibernate映射文件tree.hbm.xml

Xml代码 复制代码
  1. <hibernate-mapping>  
  2.     <class name="zoboya.Tree" table="tree" >  
  3.        <id name="id" type="java.lang.Integer">  
  4.              <generator class="native" />  
  5.         </id>  
  6.         <property name="name" type="string">  
  7.             <column name="name" length="255" />  
  8.         </property>  
  9.          <property name="children" type="string">  
  10.             <column name="children" length="255" />  
  11.         </property>  
  12.            
  13.          <many-to-one name="parent" class="zoboya.Tree" fetch="select">  
  14.             <column name="parentid" not-null="true" />  
  15.         </many-to-one>  
  16.   </class>  
  17. </hibernate-mapping>  

 

TreeDao.java代码如下:

Java代码 复制代码
  1. public class TreeDAO extends HibernateDaoSupport implements ITreeDAO  {   
  2.  private static final Log log = LogFactory.getLog(TreeDAO.class);   
  3.   
  4. public Tree findById(java.lang.Integer id) {   
  5.         log.debug("getting Tree instance with id: " + id);   
  6.         try {   
  7.             Tree instance = (Tree) getSession().get("zoboya.Tree", id);   
  8.             return instance;   
  9.         } catch (RuntimeException re) {   
  10.             log.error("get failed", re);   
  11.             throw re;   
  12.         }   
  13.     }   
  14.   
  15. //其它方法省略   
  16. }  
public class TreeDAO extends HibernateDaoSupport implements ITreeDAO  {
 private static final Log log = LogFactory.getLog(TreeDAO.class);

public Tree findById(java.lang.Integer id) {
		log.debug("getting Tree instance with id: " + id);
		try {
			Tree instance = (Tree) getSession().get("zoboya.Tree", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

//其它方法省略
}

 

 

节点类,Category

Java代码 复制代码
  1. public class Category   
  2. {   
  3.   //保存了所有节点数据的Map   
  4.   private static Map<Long, Category> catMap = new HashMap<Long, Category>();   
  5.   //用作节点的id   
  6.   private long id;   
  7.   //用作节点的标题   
  8.   private String name;   
  9.   //包含子节点对象的列表   
  10.   private List<Category> children;   
  11.   
  12.      
  13.   public static Category getById(long id)   
  14.   {   
  15.     return catMap.get(id);   
  16.   }   
  17.   
  18.   public Category(long id, String name,List<Category> children){   
  19.       this.id=id;   
  20.       this.name=name;   
  21.       this.children=children;   
  22.       catMap.put(id, this);   
  23.   }     
  24.      
  25.   public Category(long id, String name, Category... children)   
  26.   {   
  27.     this.id = id;   
  28.     this.name = name;   
  29.     //初始化Category对象的children属性,并放入子对象   
  30.     this.children = new ArrayList<Category>();   
  31.     for (Category child : children)   
  32.     {   
  33.       this.children.add(child);   
  34.     }   
  35.   
  36.     catMap.put(id, this);   
  37.   }   
  38.   
  39.   public long getId()   
  40.   {   
  41.     return id;   
  42.   }   
  43.   
  44.   public void setId(long id)   
  45.   {   
  46.     this.id = id;   
  47.   }   
  48.   
  49.   public String getName()   
  50.   {   
  51.     return name;   
  52.   }   
  53.   
  54.   public void setName(String name)   
  55.   {   
  56.     this.name = name;   
  57.   }   
  58.   
  59.   public List<Category> getChildren()   
  60.   {   
  61.     return children;   
  62.   }   
  63.   
  64.   public void setChildren(List<Category> children)   
  65.   {   
  66.     this.children = children;   
  67.   }   
  68. }  
public class Category
{
  //保存了所有节点数据的Map
  private static Map<Long, Category> catMap = new HashMap<Long, Category>();
  //用作节点的id
  private long id;
  //用作节点的标题
  private String name;
  //包含子节点对象的列表
  private List<Category> children;

  
  public static Category getById(long id)
  {
    return catMap.get(id);
  }

  public Category(long id, String name,List<Category> children){
	  this.id=id;
	  this.name=name;
	  this.children=children;
	  catMap.put(id, this);
  }  
  
  public Category(long id, String name, Category... children)
  {
    this.id = id;
    this.name = name;
    //初始化Category对象的children属性,并放入子对象
    this.children = new ArrayList<Category>();
    for (Category child : children)
    {
      this.children.add(child);
    }

    catMap.put(id, this);
  }

  public long getId()
  {
    return id;
  }

  public void setId(long id)
  {
    this.id = id;
  }

  public String getName()
  {
    return name;
  }

  public void setName(String name)
  {
    this.name = name;
  }

  public List<Category> getChildren()
  {
    return children;
  }

  public void setChildren(List<Category> children)
  {
    this.children = children;
  }
}

 

Service层的类TreeService

Java代码 复制代码
  1. public class TreeService implements ITreeService {   
  2.     private ITreeDAO treeDao;   
  3.   
  4.     public ITreeDAO getTreeDao() {   
  5.         return treeDao;   
  6.     }   
  7.   
  8.     public void setTreeDao(ITreeDAO treeDao) {   
  9.         this.treeDao = treeDao;   
  10.     }   
  11.   
  12.        
  13.        
  14.      //递归方法   
  15.      public Category createTree(Tree tree){         
  16.          System.out.println("Name= "+tree.getName());   
  17.         //如果有儿子就循环调用自己去找,把找到的所有儿子生成一个list作为children加入到父中   
  18.           List<Category> children=new ArrayList<Category>();   
  19.             if (tree.getChildren()!=null) {                                
  20.                 String[] childrenId=tree.getChildren().split(",");   
  21.                 for (int i = 0; i < childrenId.length; i++) {                       
  22.                     //递归   
  23.                     Tree branch=treeDao.findById(Integer.parseInt(childrenId[i]));   
  24.                     Category child = createTree(branch);   
  25.                     children.add(child);   
  26.                 }   
  27.             }   
  28.                
  29.         Category me=new Category(tree.getId(),tree.getName(), children);     
  30.              
  31.         return me;   
  32.       }   
  33.   
  34. }  
public class TreeService implements ITreeService {
	private ITreeDAO treeDao;

	public ITreeDAO getTreeDao() {
		return treeDao;
	}

	public void setTreeDao(ITreeDAO treeDao) {
		this.treeDao = treeDao;
	}

	
	
	 //递归方法
	 public Category createTree(Tree tree){		 
		 System.out.println("Name= "+tree.getName());
		//如果有儿子就循环调用自己去找,把找到的所有儿子生成一个list作为children加入到父中
		  List<Category> children=new ArrayList<Category>();
			if (tree.getChildren()!=null) {								
				String[] childrenId=tree.getChildren().split(",");
				for (int i = 0; i < childrenId.length; i++) {					
					//递归
					Tree branch=treeDao.findById(Integer.parseInt(childrenId[i]));
					Category child = createTree(branch);
					children.add(child);
				}
			}
			
		Category me=new Category(tree.getId(),tree.getName(), children);  
		  
		return me;
	  }

}

 

Action

Java代码 复制代码
  1. public class ShowDynamicTreeAction extends ActionSupport   
  2. {   
  3.     private static final long serialVersionUID = 7437613038859411044L;   
  4.     private ITreeService treeService;   
  5.     private Category category;   
  6.        
  7.        
  8.        
  9.                public Category getCategory() {   
  10.         return category;   
  11.     }   
  12.   
  13.   
  14.   
  15.     public void setCategory(Category category) {   
  16.         this.category = category;   
  17.     }   
  18.   
  19.   
  20.   
  21. @Override  
  22.     public String execute() throws Exception {   
  23.         this.category =this.getTreeRootNode();   
  24.         return SUCCESS;   
  25.     }   
  26.   
  27.   
  28.   
  29. public void setTreeService(ITreeService treeService) {   
  30.         this.treeService = treeService;   
  31.     }   
  32.   
  33.   
  34.   
  35. public Category getTreeRootNode()    
  36.   {    Tree tree=new Tree(0,"ZH根目录","1,5");  //选择构造一个根目录,其子目录用逗号隔开!   
  37.        System.out.println("开始递归!");   
  38.                 return treeService.createTree(tree);   
  39.   }   
  40.        
  41. }  
public class ShowDynamicTreeAction extends ActionSupport
{
	private static final long serialVersionUID = 7437613038859411044L;
	private ITreeService treeService;
	private Category category;
	
	
	
               public Category getCategory() {
		return category;
	}



	public void setCategory(Category category) {
		this.category = category;
	}



@Override
	public String execute() throws Exception {
	    this.category =this.getTreeRootNode();
		return SUCCESS;
	}



public void setTreeService(ITreeService treeService) {
		this.treeService = treeService;
	}



public Category getTreeRootNode() 
  {	   Tree tree=new Tree(0,"ZH根目录","1,5");  //选择构造一个根目录,其子目录用逗号隔开!
	   System.out.println("开始递归!");
                return treeService.createTree(tree);
  }
    
}

 

在applicationContext.xml文件中加入

Xml代码 复制代码
  1. <!-- DAO -->  
  2. <bean id="treeDao" class="zoboya.struts2.dao.TreeDAO">  
  3.         <property name="sessionFactory">  
  4.             <ref bean="sessionFactory"/>  
  5.         </property>  
  6.     </bean>  
  7.   
  8. <!-- Services -->  
  9. <bean id="treeService" class="zoboya.struts2.service.TreeService">  
  10.         <property name="treeDao">  
  11.             <ref bean="treeDao"/>  
  12.         </property>  
  13.     </bean>  
  14.        
  15. <!-- Action -->  
  16. <bean id="treeAction" class="zoboya.struts2.action.ShowDynamicTreeAction" singleton="false">  
  17.       <property name="treeService">  
  18.             <ref bean="treeService"/>  
  19.         </property>  
  20.     </bean>  
  21.       

 

在struts.xml中加入

Xml代码 复制代码
  1. <action name="showDynamicTreeAction"  
  2.       class="treeAction">  
  3.         <result>/ajaxTags/treeTag/treeExampleDynamic.jsp</result>  
  4.      </action>  

 

部署后访问以下地址就可以看到动态树了

http://localhost:8080/项目名/ajaxTags/treeTag/showDynamicTreeAction.action

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值