纯Struts2 AJAX树实现方式及源码

      在J2EE开发中不可避免会使用的树(Tree),一般情况有二种实现方式,第一种是初始化的时候将树信息就全部加载到内存中,这种方式适用于小数据或者性能要求不高的情况,优点是加载完后对服务器就不会有压力并且打开树节点速度快;第二种是数据异步加载即ajax技术,每次都加载少量的必须的数据,这种方式适用于大量数据和性能要求较高的情况;我们做为软件设计和开发者都希望软件性能最佳、用户体验最好;

      目前网上介绍struts2 ajax tree的实例很多但就是不太全面、完整,就连struts官方网站都没有完整实例;下面我来介绍一下struts2 ajax tree的使用方法及技巧,这个实例全部引用自jdframe开发框架中的组织机构管理功能源码:


1. jsp页面

首先JSP要引用,其次在<head>中包含< sx:head/>

 

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@ taglib prefix="s" uri="/struts-tags"%>  
  3. <%@ taglib prefix="sx" uri="/struts-dojo-tags"%>  
  4. <%  
  5. String path = request.getContextPath();  
  6. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  7. %>  
  8.   
  9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  10. <html>  
  11.   <head>  
  12.     <base href="<%=basePath%>">  
  13.     <title>JDFrame System Organization Tree</title>  
  14.       
  15.     <meta http-equiv="pragma" content="no-cache">  
  16.     <meta http-equiv="cache-control" content="no-cache">  
  17.     <meta http-equiv="expires" content="0">      
  18.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  19.     <meta http-equiv="description" content="This is my page">  
  20.     <meta content="MSHTML 6.00.2600.0" name=GENERATOR>  
  21.     <link rel="stylesheet" type="text/css" href="/css/sys/main.css">  
  22.     <style type="text/css">  
  23.     body {  
  24.         margin-left: 0px;  
  25.         margin-top: 0px;  
  26.         margin-right: 0px;  
  27.         margin-bottom: 0px;  
  28.         SCROLLBAR-FACE-COLOR: #37b0dd;  
  29.         SCROLLBAR-HIGHLIGHT-COLOR:#ffffff;  
  30.         SCROLLBAR-SHADOW-COLOR: #ffffff;  
  31.         SCROLLBAR-3DLIGHT-COLOR:0099cc;  
  32.         SCROLLBAR-ARROW-COLOR:#1c6787;  
  33.         SCROLLBAR-TRACK-COLOR:#ddfdfd;  
  34.         SCROLLBAR-DARKSHADOW-COLOR:#1c6787;  
  35.     }  
  36.     </style>  
  37.     <sx:head/>  
  38.   </head>  
  39. <body>  
  40. <script language="JavaScript" type="text/javascript">  
  41.     dojo.event.topic.subscribe("treeSelected", function treeNodeSelected(node) {  
  42.         dojo.io.bind({  
  43.             url: "<s:url value='/com/jdframe/sys/core/org/treeSelected.action'/>?nodeId="+node.node.widgetId,  
  44.             load: function(type, data, evt) {  
  45.                 var divDisplay = dojo.byId("displayId");  
  46.                 divDisplay.innerHTML=data;  
  47.             },  
  48.             mimeType: "text/html"  
  49.         });  
  50.         window.parent.showOrg(node.node.widgetId);  
  51.     });  
  52. </script>  
  53. <s:url id="nodesUrl" namespace="/com/jdframe/sys/core/org" action="getNodes" />  
  54. <table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">  
  55.   <tr>  
  56.     <td valign="top" id="td_tree">  
  57.     <div style="float:left; margin-right: 50px;">  
  58.        <sx:tree id="dorg_tree" href="%{#nodesUrl}" treeSelectedTopic="treeSelected" />  
  59.     </div>  
  60.        
  61.     </td>  
  62.   </tr>  
  63. </table>  
  64.    
  65. </body>  
  66. </html>  


2. Struts2配置文件

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">  
  3. <struts>  
  4.     <package name="organiztion"  namespace="/com/jdframe/sys/core/org"   extends="struts-default" >  
  5.       
  6.         <action  name = "getNodes" class"com.jdframe.sys.biz.org.DynamicTreeAction">  
  7.             <result  type="freemarker">/sys/core/frame/treeAjaxDynamic.ftl</result>  
  8.         </action>  
  9.           
  10.         <action  name = "treeSelected" method="selectTreeNode" class"com.jdframe.sys.biz.org.DynamicTreeAction">  
  11.             <result>/sys/core/org/tree.jsp</result>  
  12.         </action>  
  13. ...  

3. java代码 

AjaxDynamicTreeAction.java

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.jdframe.sys.core.model.tree;  
  2.   
  3. import com.jdframe.sys.core.action.JdframeAction;  
  4. import com.jdframe.sys.core.model.tree.Category;  
  5.    
  6.    
  7. // TODO: Auto-generated Javadoc  
  8. /** 
  9.  * The Path : com.jdframe.sys.core.model.tree.AjaxDynamicTreeAction.java 
  10.  * The   AjaxDynamicTreeAction 
  11.  * Last-Modified-Time : 2014-2-20 10:44:11 
  12.  * 
  13.  * @author support@jdframe.com 
  14.  * @version  2.0.3.0 
  15.  *  http://www.jdframe.com 
  16.  * @see  
  17.  */  
  18. public abstract class AjaxDynamicTreeAction  extends JdframeAction {  
  19.     //default value  
  20.     /** The node id. */  
  21.     protected String nodeId = null;  
  22.     //for ftl  
  23.     /** The category. */  
  24.     public Category category;  
  25.       
  26.     /* (非 Javadoc) 
  27.     * <p>Title: perform</p> 
  28.     * <p>Description: </p> 
  29.     * @return 
  30.     * @see com.jdframe.sys.core.action.JdframeAction#perform() 
  31.     */  
  32.     /* (non-Javadoc) 
  33.      * @see com.jdframe.sys.core.action.JdframeAction#perform() 
  34.      */  
  35.     @Override  
  36.     protected abstract String perform();  
  37.       
  38.     /** 
  39.      * Select tree node. 
  40.      * 
  41.      * @return the string 
  42.      */  
  43.     public abstract String selectTreeNode() ;  
  44.   
  45.     /* (非 Javadoc) 
  46.     * <p>Title: validators</p> 
  47.     * <p>Description: </p> 
  48.     * @see com.jdframe.sys.core.action.JdframeAction#validators() 
  49.     */  
  50.     /* (non-Javadoc) 
  51.      * @see com.jdframe.sys.core.action.JdframeAction#validators() 
  52.      */  
  53.     @Override  
  54.     protected abstract void validators() ;  
  55.   
  56.     /* (非 Javadoc) 
  57.     * <p>Title: initial</p> 
  58.     * <p>Description: </p> 
  59.     * @return 
  60.     * @see com.jdframe.sys.core.action.JdframeAction#initial() 
  61.     */  
  62.     /* (non-Javadoc) 
  63.      * @see com.jdframe.sys.core.action.JdframeAction#initial() 
  64.      */  
  65.     @Override  
  66.     protected abstract String initial()  ;  
  67.       
  68.   
  69.   
  70.     /** 
  71.      * Gets the category. 
  72.      * 
  73.      * @return the category 
  74.      */  
  75.     public Category getCategory() {  
  76.         return category;  
  77.     }  
  78.   
  79.     /** 
  80.      * Sets the category. 
  81.      * 
  82.      * @param category the new category 
  83.      */  
  84.     public void setCategory(Category category) {  
  85.         this.category = category;  
  86.     }  
  87.   
  88.        
  89.       
  90.     /** 
  91.      * Gets the node id. 
  92.      * 
  93.      * @return the node id 
  94.      */  
  95.     public String getNodeId() {  
  96.         return nodeId;  
  97.     }  
  98.   
  99.     /** 
  100.      * Sets the node id. 
  101.      * 
  102.      * @param nodeId the new node id 
  103.      */  
  104.     public void setNodeId(String nodeId) {  
  105.         this.nodeId = nodeId;  
  106.     }  
  107.   
  108.     /** 
  109.      * Gets the node name. 
  110.      * 
  111.      * @return the node name 
  112.      */  
  113.     public String getNodeName() {  
  114.         return category != null ? category.getName() : "Node not found";  
  115.     }  
  116.   
  117.    
  118.   
  119. }  


业务处理类:DynamicTreeAction.java

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.jdframe.sys.biz.org;  
  2.   
  3. import com.jdframe.sys.core.model.User;  
  4. import com.jdframe.sys.core.model.tree.AjaxDynamicTreeAction;  
  5.    
  6. // TODO: Auto-generated Javadoc  
  7.    
  8. /** 
  9.  * The Path : com.jdframe.sys.biz.org.DynamicTreeAction.java 
  10.  * The   DynamicTreeAction 
  11.  * Last-Modified-Time : 2014-2-20 9:55:53 
  12.  * 
  13.  * @author support@jdframe.com 
  14.  * @version  2.0.3.0 
  15.  *  http://www.jdframe.com 
  16.  * @see  
  17.  */  
  18. public class DynamicTreeAction  extends AjaxDynamicTreeAction {  
  19.       
  20.     /* (非 Javadoc) 
  21.     * <p>Title: perform</p> 
  22.     * <p>Description: </p> 
  23.     * @return 
  24.     * @see com.jdframe.sys.core.action.JdframeAction#perform() 
  25.     */  
  26.     /* (non-Javadoc) 
  27.      * @see com.jdframe.sys.core.model.tree.AjaxDynamicTreeAction#perform() 
  28.      */  
  29.     @Override  
  30.     protected String perform() {  
  31.         // TODO Auto-generated method stub  
  32.         User user = this.getUserProfileFromSession().getUser();  
  33.         CategoryImpl cat = new CategoryImpl();  
  34.         if(nodeId == null ){  
  35.             nodeId = user.getUser_zzjg_dm();  
  36.             category = cat.getRootById(nodeId);  
  37.         }else{  
  38.             category = cat.getById(nodeId);  
  39.         }  
  40.           
  41.         return SUCCESS;  
  42.     }  
  43.       
  44.     /** 
  45.      * Select tree node. 
  46.      * 
  47.      * @return the string 
  48.      */  
  49.     public String selectTreeNode() {  
  50.         // TODO Auto-generated method stub  
  51.         //category = OrgCategory.getInstance().getById(nodeId);  
  52.         CategoryImpl cat = new CategoryImpl();  
  53.         category = cat.getById(nodeId);  
  54.         return SUCCESS;  
  55.     }  
  56.   
  57.     /* (非 Javadoc) 
  58.     * <p>Title: validators</p> 
  59.     * <p>Description: </p> 
  60.     * @see com.jdframe.sys.core.action.JdframeAction#validators() 
  61.     */  
  62.     /* (non-Javadoc) 
  63.      * @see com.jdframe.sys.core.model.tree.AjaxDynamicTreeAction#validators() 
  64.      */  
  65.     @Override  
  66.     protected void validators() {  
  67.         // TODO Auto-generated method stub  
  68.           
  69.     }  
  70.   
  71.     /* (非 Javadoc) 
  72.     * <p>Title: initial</p> 
  73.     * <p>Description: </p> 
  74.     * @return 
  75.     * @see com.jdframe.sys.core.action.JdframeAction#initial() 
  76.     */  
  77.     /* (non-Javadoc) 
  78.      * @see com.jdframe.sys.core.model.tree.AjaxDynamicTreeAction#initial() 
  79.      */  
  80.     @Override  
  81.     protected String initial() {  
  82.         // TODO Auto-generated method stub  
  83.         return null;  
  84.     }  
  85.   
  86. }  

TREE对象类: Category.java

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.jdframe.sys.core.model.tree;  
  2.   
  3. import java.util.*;  
  4.   
  5. // TODO: Auto-generated Javadoc  
  6. /** 
  7.  * The Path : com.jdframe.sys.core.model.Category.java 
  8.  * The Class Category. 
  9.  * Last-Modified-Time : 2013-12-25 21:32:41 
  10.  * 
  11.  * @author support@jdframe.com 
  12.  * @see 
  13.  * @version  2.0.3.0 www.jdframe.com 
  14.  */  
  15. public abstract class Category {  
  16.   
  17.     /** The id. */  
  18.     protected String id;  
  19.       
  20.     /** The name. */  
  21.     protected String name;  
  22.       
  23.     /** The children. */  
  24.     protected List children;  
  25.       
  26.     /** The cat map. */  
  27.     public Category(){}  
  28.      /** 
  29.       *  
  30.      * Title:  getById 
  31.      * Description: TODO(返回包含下级节点的树,初始化后展开节点时执行,一般情况下id为为当前选中的节点) 
  32.      * @param       
  33.      * @return  Category   
  34.      * 
  35.       */  
  36.     public  abstract Category getById(String id);  
  37.     /** 
  38.      *  
  39.     * Title:  getRootById 
  40.     * Description: TODO(返回包含当前节点的树,初始化时执行,一般情况下id为空) 
  41.     * @param       
  42.     * @return  Category   
  43.     * 
  44.      */  
  45.     public  abstract Category getRootById(String id);  
  46.   
  47.     public String getId() {  
  48.         return id;  
  49.     }  
  50.   
  51.     public void setId(String id) {  
  52.         this.id = id;  
  53.     }  
  54.   
  55.     /** 
  56.      * Gets the name. 
  57.      * 
  58.      * @return the name 
  59.      */  
  60.     public String getName() {  
  61.         return name;  
  62.     }  
  63.   
  64.     /** 
  65.      * Sets the name. 
  66.      * 
  67.      * @param name the new name 
  68.      */  
  69.     public void setName(String name) {  
  70.         this.name = name;  
  71.     }  
  72.   
  73.     /** 
  74.      * Gets the children. 
  75.      * 
  76.      * @return the children 
  77.      */  
  78.     public List getChildren() {  
  79.         return children;  
  80.     }  
  81.   
  82.     /** 
  83.      * Sets the children. 
  84.      * 
  85.      * @param children the new children 
  86.      */  
  87.     public void setChildren(List children) {  
  88.         this.children = children;  
  89.     }  
  90.   
  91.        
  92.   
  93. }  


TREE对象类: CategoryImpl.java

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.jdframe.sys.biz.org;  
  2.   
  3. import java.util.*;  
  4.   
  5. import org.apache.ibatis.session.SqlSession;  
  6. import org.apache.log4j.Logger;  
  7.   
  8. import com.jdframe.sys.core.model.tree.Category;  
  9. import com.jdframe.sys.core.util.DbUtils;  
  10. import com.jdframe.sys.dao.model.T_sys_organization;  
  11.   
  12. // TODO: Auto-generated Javadoc  
  13. /** 
  14.  * The Path : com.jdframe.sys.biz.org.CategoryImpl.java 
  15.  * The   CategoryImpl 
  16.  * Last-Modified-Time : 2014-2-20 21:33:19 
  17.  * @author support@jdframe.com 
  18.  * @version  2.0.3.0 
  19.  *  http://www.jdframe.com 
  20.  * @see  
  21.  */  
  22. public class CategoryImpl extends Category {  
  23.       
  24.     /** The log. */  
  25.     Logger log = Logger.getLogger(CategoryImpl.class);  
  26.       
  27.     /** 
  28.      * Instantiates a new category impl. 
  29.      */  
  30.     public CategoryImpl(){  
  31.           
  32.     }  
  33.       
  34.     /** 
  35.      * Instantiates a new category impl. 
  36.      * 
  37.      * @param id the id 
  38.      * @param name the name 
  39.      * @param children the children 
  40.      */  
  41.     public CategoryImpl(String id, String name, CategoryImpl... children) {  
  42.         //super(id, name, children);  
  43.         this.id = id;  
  44.         this.name = name;  
  45.         this.children = new ArrayList<CategoryImpl>();  
  46.         for (CategoryImpl child : children) {  
  47.             this.children.add(child);  
  48.         }  
  49.         // TODO Auto-generated constructor stub  
  50.     }  
  51.       
  52.     /* (non-Javadoc) 
  53.      * @see com.jdframe.sys.core.model.tree.Category#getById(java.lang.String) 
  54.      */  
  55.     @Override  
  56.     public  CategoryImpl getById(String sj_dm){  
  57.           
  58.         SqlSession session = DbUtils.buildSqlSession();  
  59.         CategoryImpl root_cat  = null;  
  60.         try{  
  61.                
  62.             List<T_sys_organization> all = session.selectList("getSubOrgByDm",sj_dm);  
  63.                
  64.             List allCategory = new ArrayList();  
  65.             for (int j = 0; j < all.size(); j++) {  
  66.                
  67.                 CategoryImpl  self_category  = null;  
  68.                 String self_code = all.get(j).getZzjg_dm();  
  69.                 String self_name = all.get(j).getZzjg_name();      
  70.                    
  71.                   
  72.                     List<T_sys_organization> l = session.selectList("getSubOrgByDm",self_code);  
  73.                     List subCategory = new ArrayList();  
  74.                     for (int i = 0; i < l.size(); i++) {  
  75.                         String _code = l.get(i).getZzjg_dm();  
  76.                         String _name = l.get(i).getZzjg_name();  
  77.                         subCategory.add(new CategoryImpl(_code,_name));  
  78.                     }  
  79.                     if(subCategory.size()>0){  
  80.                         CategoryImpl[] childr = new CategoryImpl[subCategory.size()];  
  81.                         for (int i = 0; i < subCategory.size(); i++) {  
  82.                             if(subCategory.get(i) instanceof CategoryImpl){  
  83.                                 childr[i] = (CategoryImpl)subCategory.get(i);  
  84.                             }  
  85.                         }  
  86.                         self_category  = new CategoryImpl(self_code,self_name,childr);  
  87.                     }else{  
  88.                         self_category  = new CategoryImpl(self_code,self_name);  
  89.                     }  
  90.                     allCategory.add(self_category);  
  91.                       
  92.              }  
  93.             CategoryImpl[] allchildr = new CategoryImpl[allCategory.size()];  
  94.             if(allCategory.size()>0){  
  95.                   
  96.                 for (int i = 0; i < allCategory.size(); i++) {  
  97.                     if(allCategory.get(i) instanceof CategoryImpl){  
  98.                         allchildr[i] = (CategoryImpl)allCategory.get(i);  
  99.                     }  
  100.                 }  
  101.                 root_cat  = new CategoryImpl("-1","Root",allchildr);  
  102.             }else{  
  103.                 root_cat  = new CategoryImpl("-1","Root");  
  104.             }  
  105.               
  106.               
  107.             //if(sj_dm.equals("76300000000")){  
  108.             //  root_cat = self_category;  
  109.             //}  
  110.         }finally{  
  111.             if(session!=null)session.close();  
  112.         }  
  113.         return root_cat;  
  114.            
  115.     }  
  116.       
  117.     /* (non-Javadoc) 
  118.      * @see com.jdframe.sys.core.model.tree.Category#getRootById(java.lang.String) 
  119.      */  
  120.     @Override  
  121.     public CategoryImpl getRootById(String id) {  
  122.         // TODO Auto-generated method stub  
  123.         SqlSession session = DbUtils.buildSqlSession();  
  124.         CategoryImpl root_cat  = null;  
  125.         try{  
  126.                
  127.             List<T_sys_organization> all = session.selectList("getOrgBySwjgDm",id);  
  128.                
  129.             List allCategory = new ArrayList();  
  130.             for (int j = 0; j < all.size(); j++) {  
  131.                
  132.                 CategoryImpl  self_category  = null;  
  133.                 String self_code = all.get(j).getZzjg_dm();  
  134.                 String self_name = all.get(j).getZzjg_name();      
  135.                    
  136.                   
  137.                     List<T_sys_organization> l = session.selectList("getSubOrgByDm",self_code);  
  138.                     List subCategory = new ArrayList();  
  139.                     for (int i = 0; i < l.size(); i++) {  
  140.                         String _code = l.get(i).getZzjg_dm();  
  141.                         String _name = l.get(i).getZzjg_name();  
  142.                         subCategory.add(new CategoryImpl(_code,_name));  
  143.                     }  
  144.                     if(subCategory.size()>0){  
  145.                         CategoryImpl[] childr = new CategoryImpl[subCategory.size()];  
  146.                         for (int i = 0; i < subCategory.size(); i++) {  
  147.                             if(subCategory.get(i) instanceof CategoryImpl){  
  148.                                 childr[i] = (CategoryImpl)subCategory.get(i);  
  149.                             }  
  150.                         }  
  151.                         self_category  = new CategoryImpl(self_code,self_name,childr);  
  152.                     }else{  
  153.                         self_category  = new CategoryImpl(self_code,self_name);  
  154.                     }  
  155.                     allCategory.add(self_category);  
  156.                       
  157.              }  
  158.             CategoryImpl[] allchildr = new CategoryImpl[allCategory.size()];  
  159.             if(allCategory.size()>0){  
  160.                   
  161.                 for (int i = 0; i < allCategory.size(); i++) {  
  162.                     if(allCategory.get(i) instanceof CategoryImpl){  
  163.                         allchildr[i] = (CategoryImpl)allCategory.get(i);  
  164.                     }  
  165.                 }  
  166.                 root_cat  = new CategoryImpl("-1","Root",allchildr);  
  167.             }else{  
  168.                 root_cat  = new CategoryImpl("-1","Root");  
  169.             }  
  170.   
  171.         }finally{  
  172.             if(session!=null)session.close();  
  173.         }  
  174.         return root_cat;  
  175.     }  
  176.    
  177.    
  178. }  


应用效果:


原网址:http://blog.csdn.net/china_melancholy/article/details/19540689


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值