struts2动态树的例子(转)

来源:互联网 发布:九阴真经女号捏脸数据 编辑:程序博客网 时间:2024/05/29 05:05

修改struts2-showcase动态树,改由从数据表中读取,并且用递归方式生成

系统采用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 }  
[java] view plaincopy
  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 }  

 

 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>  
[xml] view plaincopy
  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. }  
[java] view plaincopy
  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. }  

 

 

节点类,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. }  
[java] view plaincopy
  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. }  

 

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. }  
[java] view plaincopy
  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. }  

 

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. }  
[java] view plaincopy
  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. }  

 

在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.       
[xml] view plaincopy
  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>  
[xml] view plaincopy
  1. <action name="showDynamicTreeAction"  
  2.       class="treeAction">  
  3.         <result>/ajaxTags/treeTag/treeExampleDynamic.jsp</result>  
  4.      </action>  

 

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

原创粉丝点击