JSP自定义标签(一) 树形下拉选择菜单

http://blog.csdn.net/core_star/article/details/7421228


一、效果图:


二、自定义标签JAVA代码:

[java]  view plain  copy
  1. package com.moonNigh.tagSupport;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import javax.servlet.http.HttpServletRequest;  
  6. import javax.servlet.jsp.JspException;  
  7. import javax.servlet.jsp.JspWriter;  
  8. import javax.servlet.jsp.tagext.TagSupport;  
  9.   
  10. /** 
  11.  * 
  12.  * @author HHB 
  13.  * 
  14.  * 树形下拉选择控件 
  15.  * 
  16.  */  
  17.   
  18. public class SelectorTag extends TagSupport {  
  19.   
  20.     private static final long serialVersionUID = 9878861374414215L;  
  21.       
  22.     //标签name属性  
  23.     private String name;  
  24.       
  25.     //所需图片的路径  
  26.     private String imgPath;  
  27.       
  28.     //所需javascript文件的路径  
  29.     private String scriptPaht;  
  30.       
  31.     //所需css文件的路径  
  32.     private String cssPath;  
  33.       
  34.     //项目的根路径  
  35.     private String rootPath;  
  36.       
  37.     //标签的value属性  
  38.     private String value;  
  39.     private String text;  
  40.     private String path;  
  41.       
  42.     /* 
  43.      * 标签的actionUrl属性 
  44.      * 联想查询结果数据通过向actionUrl属性指定的url请求得到 
  45.      */  
  46.     private String actionUrl;  
  47.       
  48.     private HttpServletRequest request=null;  
  49.       
  50.       
  51.     public String getActionUrl() {  
  52.         return actionUrl;  
  53.     }  
  54.     public void setActionUrl(String actionUrl) {  
  55.         this.actionUrl = actionUrl;  
  56.     }  
  57.     public String getValue() {  
  58.         return value;  
  59.     }  
  60.     public void setValue(String value) {  
  61.         this.value = value;  
  62.     }  
  63.     public String getImgPath() {  
  64.         return imgPath;  
  65.     }  
  66.     public void setImgPath(String imgPath) {  
  67.         this.imgPath = imgPath;  
  68.     }  
  69.     public String getScriptPaht() {  
  70.         return scriptPaht;  
  71.     }  
  72.       
  73.     public void setScriptPaht(String scriptPaht) {  
  74.         this.scriptPaht = scriptPaht;  
  75.     }  
  76.     public String getCssPath() {  
  77.         return cssPath;  
  78.     }  
  79.       
  80.     public void setCssPath(String cssPath) {  
  81.         this.cssPath = cssPath;  
  82.     }  
  83.       
  84.       
  85.     public String getText() {  
  86.         return text;  
  87.     }  
  88.     public void setText(String text) {  
  89.         this.text = text;  
  90.     }  
  91.     public String getName() {  
  92.         return name;  
  93.     }  
  94.     public void setName(String name) {  
  95.         this.name = name;  
  96.     }  
  97.     public SelectorTag()  
  98.     {  
  99.           
  100.     }  
  101.       
  102.       
  103.     /** 
  104.      * 初始化变量 
  105.      */  
  106.     private void initAbttributes()  
  107.     {  
  108.         request=(HttpServletRequest)this.pageContext.getRequest();  
  109.         rootPath=request.getContextPath();  
  110.         this.imgPath="/images/";  
  111.         this.scriptPaht="/js/";  
  112.         this.cssPath="/css/";  
  113.           
  114.     }  
  115.     @Override  
  116.     public int doStartTag() throws JspException {  
  117.         initAbttributes();  
  118.         path=rootPath+"/jsp/tags/treeSelectorPage.jsp?id="+id+"&actionUrl="+actionUrl;  
  119.         JspWriter out=pageContext.getOut();  
  120.         try {  
  121.             String tName=name;  
  122.             //引入javascript文件  
  123.             out.println("<script type='text/javascript' charset='GB2312' src='"+rootPath+scriptPaht+"selector.js'></script>");  
  124.               
  125.             //引入css文件  
  126.             out.println("<link rel='stylesheet' href='"+rootPath+cssPath+"selector.css' type='text/css' />");  
  127.               
  128.             StringBuilder tag=new StringBuilder("<input type='text' ");  
  129.             tag.append("id='").append(id).append("'");  
  130.             tag.append(" value='").append(text==null?"":text).append("'");  
  131.             tag.append(" οnclick='return showSelector(\"");  
  132.             tag.append(id).append("\",\"").append(path).append("\")' readonly>");  
  133.             tag.append("<input type='hidden' name='")  
  134.             .append(tName).append("' id='").append(id).append("_value")  
  135.             .append("' value='").append(value==null?"":value).append("'>");  
  136.             out.println(tag.toString());  
  137.         } catch (IOException e) {  
  138.             e.printStackTrace();  
  139.         }  
  140.         return SKIP_BODY;  
  141.     }  
  142.       
  143.       
  144. }  


三、树形结构所属jsp页面:

[css]  view plain  copy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <%  
  4. String path = request.getContextPath();  
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  6. %>  
  7. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  8. <html xmlns="http://www.w3.org/1999/xhtml" >  
  9. <head runat="server">  
  10.     <style type="text/css">  
  11.     .btn-unfold {  
  12.     BACKGROUND: url(../images/bg10.png) no-repeat; FLOAT: left; WIDTH: 13px; MARGIN-RIGHT: 5px; HEIGHT: 13px  
  13. }  
  14. .btn-fold {  
  15.     BACKGROUND: url(../images/bg09.png) no-repeat; FLOAT: left; WIDTH: 13px; MARGIN-RIGHT: 5px; HEIGHT: 13px  
  16. }  
  17.     </style>  
  18.    <script type='text/javascript' src='../../js/treeCreator.js'></script>  
  19.    <script type='text/javascript' src='../../js/jquery-1.3.2.js'></script>  
  20. </head>  
  21. <body style="margin-left: 0px;margin-right: 0px;margin-top: 0px">  
  22.     <form id="form1" runat="server">  
  23.     <div>  
  24.     </div>  
  25.     </form>  
  26.     <%  
  27.     String para="";  
  28.     String actionUrl="";  
  29.     para=request.getParameter("id");  
  30.     actionUrl=request.getParameter("actionUrl");  
  31. %>  
  32.     <script language="javascript">  
  33.     $.get("<%=(basePath+actionUrl) %>", function(data,status){  
  34.         xmlParser(data,'<%=para %>');  
  35.     });  
  36.     </script>  
  37. </body>  
  38. </html>  



三、js代码:selector.j

[javascript]  view plain  copy
  1. function closeHandler(cal) {  
  2.   cal.hide();                        // hide the selector  
  3. }  
  4. /* 
  5. function selected(cal, date) { 
  6.   
  7. } 
  8. */  
  9.   
  10. /* 
  11.  * 显示下拉框 
  12.  */  
  13. function showSelector(id,path,ref) {  
  14.   var selector=window["selector_"+id];  
  15.   var el = document.getElementById(id);  
  16.   if (selector) {  
  17.     // we already have some selector created  
  18.     if(ref&&ref===true)  
  19.     {  
  20.         selector.frame.src=path;  
  21.     }  
  22.     //selector.hide();                 // so we hide it first.  
  23.   } else {  
  24.     // first-time call, create the selector.  
  25.     var selec = new Selector();  
  26.     selec.textId=id;  
  27.     selec.id="selector_"+id;  
  28.     selec.valueId=id+"_value";  
  29.     selec.path=path;  
  30.     // uncomment the following line to hide the week numbers  
  31.     // cal.weekNumbers = false;  
  32.     selector = selec;                  // remember it in the global var  
  33.     selec.create();  
  34.   }  
  35.   selector.sel = el;                 // inform it what input field we use  
  36.   
  37.   // the reference element that we pass to showAtElement is the button that  
  38.   // triggers the selector.  In this example we align the selector bottom-right  
  39.   // to the button.  
  40.   selector.showAtElement(el, "Bl");        // show the selector  
  41.   return false;  
  42. }  
  43.   
  44. Selector = function () {  
  45.     // member variables  
  46.     this.activeDiv = null;  
  47.     this.timeout = null;  
  48.     this.dragging = false;  
  49.     this.hidden = false;  
  50.     this.isPopup = true;  
  51.     // HTML elements  
  52.     this.element = null;  
  53.     this.containFrame=null;  
  54.     this.textId=null;  
  55.     this.id=null;  
  56.     this.valueId=null;  
  57.     this.path=null;  
  58.     this.frame=null;  
  59.   
  60. }  
  61. Selector.is_ie = ( /msie/i.test(navigator.userAgent) &&!/opera/i.test(navigator.userAgent) );  
  62. Selector.getAbsolutePos = function(el) {  
  63.     var r = { x: el.offsetLeft, y: el.offsetTop };  
  64.     if (el.offsetParent) {  
  65.         var tmp = Selector.getAbsolutePos(el.offsetParent);  
  66.         r.x += tmp.x;  
  67.         r.y += tmp.y;  
  68.     }  
  69.     //alert(r.x+":"+r.y);  
  70.       
  71.     return r;  
  72. };  
  73. Selector.isRelated = function (el, evt) {  
  74.     var related = evt.relatedTarget;  
  75.     if (!related) {  
  76.         var type = evt.type;  
  77.         if (type == "mouseover") {  
  78.             related = evt.fromElement;  
  79.         } else if (type == "mouseout") {  
  80.             related = evt.toElement;  
  81.         }  
  82.     }  
  83.     while (related) {  
  84.         if (related == el) {  
  85.             return true;  
  86.         }  
  87.         related = related.parentNode;  
  88.     }  
  89.     return false;  
  90. };  
  91. Selector.removeClass = function(el, className) {  
  92.     if (!(el && el.className)) {  
  93.         return;  
  94.     }  
  95.     var cls = el.className.split(" ");  
  96.     var ar = new Array();  
  97.     for (var i = cls.length; i > 0;) {  
  98.         if (cls[--i] != className) {  
  99.             ar[ar.length] = cls[i];  
  100.         }  
  101.     }  
  102.     el.className = ar.join(" ");  
  103. };  
  104. Selector.addClass = function(el, className) {  
  105.     Selector.removeClass(el, className);  
  106.     el.className += " " + className;  
  107. };  
  108. Selector.createElement = function(type, parent) {  
  109.     var el = null;  
  110.     if (document.createElementNS) {  
  111.         // use the XHTML namespace; IE won't normally get here unless  
  112.         // _they_ "fix" the DOM2 implementation.  
  113.         el = document.createElementNS("http://www.w3.org/1999/xhtml", type);  
  114.     } else {  
  115.         el = document.createElement(type);  
  116.     }  
  117.     if (typeof parent != "undefined") {  
  118.         parent.appendChild(el);  
  119.     }  
  120.     return el;  
  121. };  
  122. /** Calls the second user handler (closeHandler). 
  123.  * 添加事件 
  124.  *  */  
  125.   
  126. Selector.addEvent = function(el, evname, func) {  
  127.     if (el.attachEvent) { // IE  
  128.         el.attachEvent("on" + evname, func);  
  129.     } else if (el.addEventListener) { // Gecko / W3C  
  130.         el.addEventListener(evname, func, true);  
  131.     } else { // Opera (or old browsers)  
  132.         el["on" + evname] = func;  
  133.     }  
  134. };  
  135.   
  136. /* 
  137.  * 删除事件 
  138.  */  
  139. Selector.removeEvent = function(el, evname, func) {  
  140.     //alert(el.detachEvent);  
  141.     if (el.detachEvent) { // IE  
  142.         el.detachEvent("on" + evname, func);  
  143.     } else if (el.removeEventListener) { // Gecko / W3C  
  144.         el.removeEventListener(evname, func, true);  
  145.     } else { // Opera (or old browsers)  
  146.         el["on" + evname] = null;  
  147.     }  
  148. };  
  149. Selector.prototype.create = function (_par) {  
  150.     var parent = null;  
  151.     if (! _par) {  
  152.         // default parent is the document body, in which case we create  
  153.         // a popup selector.  
  154.         parent = document.getElementsByTagName("body")[0];  
  155.         this.isPopup = true;  
  156.     } else {  
  157.         parent = _par;  
  158.         this.isPopup = false;  
  159.     }  
  160.     var div = document.createElement("div");  
  161.     var selectIframe = Selector.createElement("iframe");  
  162.     selectIframe.style.width="100%";  
  163.     selectIframe.style.height="100%";  
  164.     selectIframe.style.display="block";  
  165.     selectIframe.scrolling="auto";  
  166.     selectIframe.style.border="0px";  
  167.     selectIframe.id=this.textId+"_frm";  
  168.     this.containFrame=selectIframe;  
  169.     //selectIframe.style.border="1px";  
  170.     selectIframe.src=this.path;  
  171.     var span=document.createElement("span");  
  172.     span.innerHTML="test";  
  173.     //div.appendChild(span);  
  174.     this.frame=selectIframe;  
  175.     div.appendChild(selectIframe);  
  176.     this.element = div;  
  177.     div.className = "selector";  
  178.     if (this.isPopup) {  
  179.         div.style.position = "absolute";  
  180.         div.style.display = "none";  
  181.     }  
  182.     parent.appendChild(this.element);  
  183. };  
  184. Selector.prototype.destroy = function () {  
  185.     var el = this.element.parentNode;  
  186.     el.removeChild(this.element);  
  187.     Selector._C = null;  
  188. };  
  189.   
  190. /** 
  191.  *  Hides the Selector.  Also removes any "hilite" from the class of any TD 
  192.  *  element. 
  193.  *  隐藏下拉框 
  194.  */  
  195. Selector.prototype.hide = function () {  
  196.     if (this.isPopup) {  
  197.         //Selector.removeEvent(document, "keydown", Selector._keyEvent);  
  198.         //Selector.removeEvent(document, "keypress", Selector._keyEvent);  
  199.         Selector.removeEvent(document, "mousedown"function(){window[this.id].hide();});  
  200.     }  
  201.     this.element.style.display = "none";  
  202.     this.hidden = true;  
  203.     this.hideShowCovered();  
  204. };  
  205.   
  206. Selector.prototype.showAt = function (x, y) {  
  207.     var s = this.element.style;  
  208.     s.left = x + "px";  
  209.     s.top = y + "px";  
  210.     this.show();  
  211. };  
  212. /** 下拉框相对指定控件的显示位置. */  
  213. Selector.prototype.showAtElement = function (el, opts) {  
  214.     var p = Selector.getAbsolutePos(el);  
  215.     //alert(p.x+'-'+p.y);  
  216.     if (!opts || typeof opts != "string") {  
  217.         this.showAt(p.x, p.y + el.offsetHeight);  
  218.         return true;  
  219.     }  
  220.     this.show();  
  221.     var w = this.element.offsetWidth;  
  222.     var h = this.element.offsetHeight;  
  223.     this.hide();  
  224.     var valign = opts.substr(0, 1);  
  225.     var halign = "l";  
  226.     if (opts.length > 1) {  
  227.         halign = opts.substr(1, 1);  
  228.     }  
  229.     // 垂直对齐方式  
  230.         switch (valign) {  
  231.         case "T": p.y -= h; break;  
  232.         case "B": p.y += el.offsetHeight; break;  
  233.         case "C": p.y += (el.offsetHeight - h) / 2; break;  
  234.         case "t": p.y += el.offsetHeight - h; break;  
  235.         case "b"break// already there  
  236.         }  
  237.     // 水平对齐方式  
  238.     switch (halign) {  
  239.         case "L": p.x -= w; break;  
  240.         case "R": p.x += el.offsetWidth; break;  
  241.         case "C": p.x += (el.offsetWidth - w) / 2; break;  
  242.         case "r": p.x += el.offsetWidth - w; break;  
  243.         case "l"break// already there  
  244.     }  
  245.     this.showAt(p.x, p.y);  
  246. };  
  247.   
  248. Selector.prototype.show = function () {  
  249.     this.element.style.display = "block";  
  250.     this.hidden = false;  
  251.       
  252.     if (this.isPopup) {  
  253.         window[this.id] = this;  
  254.         var tId=this.id;  
  255.         //Calendar.addEvent(document, "keydown", Calendar._keyEvent);  
  256.         //Calendar.addEvent(document, "keypress", Calendar._keyEvent);  
  257.         Selector.addEvent(document, "mousedown"function(){window[tId].hide();});  
  258.     }  
  259.       
  260.     this.hideShowCovered();  
  261. };  
  262.   
  263. Selector.prototype.hideShowCovered = function () {  
  264.     function getStyleProp(obj, style){  
  265.         var value = obj.style[style];  
  266.         if (!value) {  
  267.             if (document.defaultView && typeof (document.defaultView.getComputedStyle) == "function") { // Gecko, W3C  
  268.                 value = document.defaultView.  
  269.                     getComputedStyle(obj, "").getPropertyValue(style);  
  270.             } else if (obj.currentStyle) { // IE  
  271.                 value = obj.currentStyle[style];  
  272.             } else {  
  273.                 value = obj.style[style];  
  274.             }  
  275.         }  
  276.         return value;  
  277.     };  
  278.   
  279.     var tags = new Array("applet""select");  
  280.     var el = this.element;  
  281.   
  282.     var p = Selector.getAbsolutePos(el);  
  283.     var EX1 = p.x;  
  284.     var EX2 = el.offsetWidth + EX1;  
  285.     var EY1 = p.y;  
  286.     var EY2 = el.offsetHeight + EY1;  
  287.   
  288.     for (var k = tags.length; k > 0; ) {  
  289.         var ar = document.getElementsByTagName(tags[--k]);  
  290.         var cc = null;  
  291.   
  292.         for (var i = ar.length; i > 0;) {  
  293.             cc = ar[--i];  
  294.   
  295.             p = Selector.getAbsolutePos(cc);  
  296.             var CX1 = p.x;  
  297.             var CX2 = cc.offsetWidth + CX1;  
  298.             var CY1 = p.y;  
  299.             var CY2 = cc.offsetHeight + CY1;  
  300.   
  301.             if (this.hidden || (CX1 > EX2) || (CX2 < EX1) || (CY1 > EY2) || (CY2 < EY1)) {  
  302.                 if (!cc.__msh_save_visibility) {  
  303.                     cc.__msh_save_visibility = getStyleProp(cc, "visibility");  
  304.                 }  
  305.                 cc.style.visibility = cc.__msh_save_visibility;  
  306.             } else {  
  307.                 if (!cc.__msh_save_visibility) {  
  308.                     cc.__msh_save_visibility = getStyleProp(cc, "visibility");  
  309.                 }  
  310.                 cc.style.visibility = "hidden";  
  311.             }  
  312.         }  
  313.     }  
  314. };  



四、js代码:treeCreator.js

      

[javascript]  view plain  copy
  1. var blank=".   ";  
  2.         var blankTimes=1;  
  3.         var blankSize=30;  
  4.           
  5.         var idIndex=0;  
  6.         var totalWidth=0;  
  7.         var imgpath="/MoonNighTags/images/";  
  8.         /* 
  9.          *  加载xml字符串返回XMLDocument对象 
  10.          */  
  11.         function loadXML(xmlStr)  
  12.         {  
  13.             if(!window.DOMParser && window.ActiveXObject){   //window.DOMParser Ã…жÏÊÇ·ñÊÇ·Çieä¯ÀÀÆ÷  
  14.                 var xmlDomVersions = ['MSXML.2.DOMDocument.6.0','MSXML.2.DOMDocument.3.0','Microsoft.XMLDOM'];  
  15.                 for(var i=0;i<xmlDomVersions.length;i++){  
  16.                     try{  
  17.                         xmlDoc = new ActiveXObject(xmlDomVersions[i]);  
  18.                         xmlDoc.async = false;  
  19.                         xmlDoc.loadXML(xmlStr);  
  20.                         break;  
  21.                     }catch(e){  
  22.                     }  
  23.                 }  
  24.             }  
  25.             else if(window.DOMParser && document.implementation && document.implementation.createDocument){  
  26.                 try{  
  27.                     domParser = new DOMParser();  
  28.                     xmlDoc = domParser.parseFromString(xmlStr,"text/xml");  
  29.                 }catch(e){  
  30.                 }  
  31.             }  
  32.             else{  
  33.                 return null;  
  34.             }  
  35.             return xmlDoc;  
  36.         }  
  37.           
  38.         /* 
  39.          * 解析xml字符串生成属性结构 
  40.          */  
  41.         function xmlParser(xmlStr,textId)  
  42.         {  
  43.             var childs=null;  
  44.             var root=null;  
  45.             var xmlDoc=loadXML(xmlStr);  
  46.             root=xmlDoc.documentElement;  
  47.             childs=root.childNodes;  
  48.             var topDiv=document.createElement("div");  
  49.             topDiv.id='topdiv';  
  50.             document.body.appendChild(topDiv);  
  51.             dealWithNode(root,0,topDiv,textId);  
  52.             topDiv.style.width='100%';  
  53.               
  54.         }  
  55.           
  56.         /* 
  57.          * 根据节点及其子节点生成树 
  58.          */  
  59.         function dealWithNode(node,blankTimes,container,textId)  
  60.         {  
  61.             var children=null;  
  62.             var childCount=0;  
  63.             var isLeaf=false;  
  64.               
  65.             var htmlnode=null;  
  66.             var textnode=null;  
  67.               
  68.             var bl_times=blankTimes;  
  69.             var bl_str="";  
  70.               
  71.             var node_value="";  
  72.             var node_id="";  
  73.               
  74.             if(node)  
  75.             {  
  76.                 isLeaf=isLeafNode(node);  
  77.                 var str="";  
  78.                 var spaceLength=0;  
  79.                 if(idIndex>0)  
  80.                 {  
  81.                    str=getSpaceString(1);  
  82.                 }  
  83.                 if(bl_times>0)  
  84.                 {  
  85.                        spaceLength=bl_times*blankSize;  
  86.                 }  
  87.                 node_id=node_value=getAttributValue(node,"id");  
  88.                 if(!node_id||node_id=="")  
  89.                 {  
  90.                     node_id=idIndex+1;  
  91.                 }  
  92.                 if(isLeaf)  
  93.                 {  
  94.                     if(node.nodeType==3)  
  95.                     {  
  96.                         node_value=node.nodeValue;  
  97.                     }  
  98.                     else  
  99.                     {  
  100.                         node_value=node.firstChild.nodeValue;  
  101.                     }  
  102.                     idIndex++;  
  103.                     node_value=str+node_value;  
  104.                     htmlnode=createItem(node_value,node_id,container,isLeaf,textId);  
  105.                       
  106.                     htmlnode.style.paddingLeft=spaceLength+'px';  
  107.                 }  
  108.                 else  
  109.                 {  
  110.                     node_value=getAttributValue(node,"value");  
  111.                     idIndex++;  
  112.                     node_value=str+node_value;  
  113.                     htmlnode=createItem(node_value,node_id,container,isLeaf,textId);  
  114.                     htmlnode.style.paddingLeft=spaceLength+'px';  
  115.                     var cContainer=createChildContainer("div"+node_id,container);  
  116.                     cContainer.style.paddingLeft=bl_times*blankSize;  
  117.                     cContainer.style.width='100%';  
  118.                     children=node.childNodes;  
  119.                     if(getBrowser()=="MSIE")  
  120.                     {  
  121.                         bl_times=1;  
  122.                     }  
  123.                     else  
  124.                     {  
  125.                         bl_times+=1;  
  126.                     }  
  127.                       
  128.                     if(children&&children.length>0)  
  129.                     {  
  130.                         for(var i=0;i<children.length;i++)  
  131.                         {  
  132.                             dealWithNode(children[i],bl_times,cContainer,textId);  
  133.                         }  
  134.                     }  
  135.                 }  
  136.                   
  137.             }  
  138.         }  
  139.           
  140.         /* 
  141.          * 判断节点是否是叶子节点 
  142.          */  
  143.         function isLeafNode(node)  
  144.         {  
  145.             var children=null;  
  146.             if(node)  
  147.             {  
  148.                 if(node.nodeType==3)  
  149.                 {  
  150.                     return true;  
  151.                 }  
  152.                 else  
  153.                 {  
  154.                     children=node.childNodes;  
  155.                     if(children.length==1&&children[0].nodeType==3)  
  156.                     {  
  157.                         return true;  
  158.                     }  
  159.                     else  
  160.                     {  
  161.                         return false;  
  162.                     }  
  163.                 }  
  164.             }  
  165.             return true;  
  166.         }  
  167.           
  168.           
  169.         /* 
  170.          * 获取自定节点的指定属性值 
  171.          */  
  172.         function getAttributValue(node,attrName)  
  173.         {  
  174.             var attributes=null;  
  175.             var attr=null;  
  176.             if(node)  
  177.             {  
  178.                 attributes=node.attributes;  
  179.                 if(attributes&&attributes.length>0)  
  180.                 {  
  181.                     for(var attrI=0;attrI<attributes.length;attrI++)  
  182.                     {  
  183.                         attr=attributes[attrI];  
  184.                         if(attr&&attr.nodeName==attrName)  
  185.                         {  
  186.                             return attr.nodeValue;  
  187.                         }  
  188.                     }  
  189.                 }  
  190.             }  
  191.             return null;  
  192.         }  
  193.           
  194.         /* 
  195.          * 创建树形结构中每个节点的项 
  196.          */  
  197.         function createItem(itemText,idStr,parentContainer,isLeaf,textId)  
  198.         {  
  199.             var htmlnode=null;  
  200.             var textnode=null;  
  201.             var imgnode=null;  
  202.             var titlenode=null;  
  203.             var imgsrc=imgpath+"bg09.png";  
  204.             if(isLeaf)  
  205.             {  
  206.                 imgsrc=imgpath+"bg10.png";  
  207.             }  
  208.             htmlnode=document.createElement("div");  
  209.             htmlnode.id="span"+idStr;  
  210.             //htmlnode.href="#";  
  211.             //htmlnode.style.paddingLeft="200";  
  212.             //htmlnode.expanded="fales";  
  213.             //图片  
  214.             imgnode=document.createElement("img");  
  215.             imgnode.setAttribute("src",imgsrc);  
  216.             imgnode.setAttribute("flag",isLeaf);  
  217.             imgnode.setAttribute("isLeaf",isLeaf);  
  218.             myAttachEvent(imgnode,'click',open_closeClick);  
  219.             //文字–  
  220.             titlenode=document.createElement("div");  
  221.             titlenode.style.display="inline";  
  222.             titlenode.style.cursor="pointer";  
  223.             titlenode.id=idStr;  
  224.             textnode=document.createTextNode(itemText);  
  225.             titlenode.appendChild(textnode);  
  226.             myAttachEvent(titlenode,'click',function(){  
  227.                 var srcObj;  
  228.                 if(typeof(event)!="undefined")  
  229.                 {  
  230.                     srcObj=event.srcElement;  
  231.                 }  
  232.                 else  
  233.                 {  
  234.                     srcObj=this;  
  235.                 }  
  236.                 itemClick(srcObj,textId);});  
  237.               
  238.             htmlnode.appendChild(imgnode);  
  239.             htmlnode.appendChild(titlenode);  
  240.               
  241.             parentContainer.appendChild(htmlnode);  
  242.             return htmlnode;  
  243.         }  
  244.           
  245.         /* 
  246.          * 创建包含节点下子树的容器 
  247.          */  
  248.         function createChildContainer(idStr,parentContainer)  
  249.         {  
  250.             var cContainer=null;  
  251.             cContainer=document.createElement("div");  
  252.             cContainer.id=idStr;  
  253.             cContainer.style.display="none";  
  254.             cContainer.style.whiteSpace='nowrap';  
  255.             parentContainer.appendChild(cContainer);  
  256.             return cContainer;  
  257.         }  
  258.           
  259.         /* 
  260.          * 单击图片时触发的事件 
  261.          * 展开或收起该节点的子树 
  262.          */  
  263.         function open_closeClick(arg)  
  264.         {  
  265.             var srcObj;  
  266.             if(typeof(event)!="undefined")  
  267.             {  
  268.                 srcObj=event.srcElement;  
  269.             }  
  270.             else  
  271.             {  
  272.                 srcObj=this;  
  273.             }  
  274.             var flag=srcObj.getAttribute("flag");  
  275.             if(srcObj.getAttribute("isLeaf")=="true")  
  276.             {  
  277.                 return;  
  278.             }  
  279.             if(flag=="true"){  
  280.                 srcObj.src=imgpath+"bg09.png";  
  281.                 srcObj.setAttribute("flag","false")  
  282.             }else{  
  283.                 srcObj.src=imgpath+"bg10.png";  
  284.                 srcObj.setAttribute("flag","true");  
  285.             }  
  286.               
  287.             var conId=srcObj.parentNode.id.replace("span","div");  
  288.             //alert(conId);  
  289.             var container=document.getElementById(conId);  
  290.             if(container)  
  291.             {  
  292.                 if(container.style.display=="block")  
  293.                     container.style.display="none";  
  294.                 else  
  295.                     container.style.display="block";  
  296.             }  
  297.         }  
  298.           
  299.         /* 
  300.          * 点击树中节点上选项时触发事件时调用该方法 
  301.          * 关闭下拉框 
  302.          */  
  303.         function itemClick(srcObj,pid)  
  304.         {  
  305.               
  306.             if(window.parent)  
  307.             {  
  308.                 window.parent.document.getElementById(pid).value=srcObj.innerHTML;  
  309.                 window.parent.document.getElementById(pid+'_value').value=srcObj.id;  
  310.                 if(window.parent["selector_"+pid])  
  311.                     window.parent["selector_"+pid].hide();  
  312.             }  
  313.         }  
  314.           
  315.         /* 
  316.          * 绑定事件 
  317.          */  
  318.         var myAttachEvent = function(obj, evt, fn){  
  319.                             if (obj.addEventListener)  
  320.                                 obj.addEventListener(evt, fn, false);  
  321.                             else if (obj.attachEvent)  
  322.                                 obj.attachEvent("on" + evt, fn);  
  323.                 }      
  324.           
  325.         function Integration_InterceptCellEvent(id, evt, func)  
  326.         {  
  327.             if (func.constructor == Function)  
  328.             {  
  329.                 document.write('<scr' + 'ipt for="' + id + '" event="' + evt + '">'  
  330.                         + 'Array.prototype.push.call(arguments, "' + evt + '");'  
  331.                         + func.toString().match(/\s*Function\s+(\S*)\s*\(/i)[1]  
  332.                         + '.apply(this, arguments);'  
  333.                         + '</sc' + 'ript>');  
  334.             }  
  335.         }  
  336.           
  337.         function getSpaceString(strlength)  
  338.         {  
  339.                 var str="";  
  340.                 for(var strIndex=0;strIndex<strlength;strIndex++)  
  341.                 {  
  342.                         str+=" ";  
  343.                 }  
  344.                 return str;  
  345.         }  
  346.         function getShortFileName(fileName)  
  347.         {  
  348.             var name="";  
  349.             var index=-1;  
  350.               
  351.         }  
  352.           
  353.         /* 
  354.          * 获取浏览器类型 
  355.          */  
  356.         function getBrowser()  
  357.         {  
  358.            var OsObject = "";    
  359.            if(navigator.userAgent.indexOf("MSIE")>0) {    
  360.                 return "MSIE";    
  361.            }    
  362.            if(isFirefox=navigator.userAgent.indexOf("Firefox")>0){    
  363.                 return "Firefox";    
  364.            }    
  365.            if(isSafari=navigator.userAgent.indexOf("Safari")>0) {    
  366.                 return "Safari";    
  367.            }     
  368.            if(isCamino=navigator.userAgent.indexOf("Camino")>0){    
  369.                 return "Camino";    
  370.            }    
  371.            if(isMozilla=navigator.userAgent.indexOf("Gecko/")>0){    
  372.                 return "Gecko";    
  373.            }    
  374.            return "others";  
  375.         }  


五、tld文件标签定义:

<tag>
      <name>selector</name>
      <tag-class>com.moonNigh.tagSupport.SelectorTag</tag-class>
      <body-content>empty</body-content>
      <small-icon></small-icon>
      <large-icon></large-icon>
      <description></description>
      <attribute>
          <name>id</name>
          <required>true</required>
      </attribute>
      <attribute>
          <name>name</name>
          <required>true</required>
      </attribute>
      <attribute>
          <name>value</name>
          <required>false</required>
          <rtexprvalue>true</rtexprvalue>
      </attribute>
      <attribute>
          <name>text</name>
          <required>false</required>
          <rtexprvalue>true</rtexprvalue>
      </attribute>
      <attribute>
          <name>actionUrl</name>
          <required>false</required>
          <rtexprvalue>true</rtexprvalue>
      </attribute>
      <example></example>
  </tag>

六、CSS代码:

[css]  view plain  copy
  1. .selector {  
  2.   positionrelative;  
  3.   displaynone;  
  4.   border:2px solid #C9D7F1;  
  5.   font-size11px;  
  6.   color#666;  
  7.   cursordefault;  
  8.  background:white;  
  9. /*  font-family:tahoma,verdana,sans-serif;*/  
  10.   font-family:arial;  
  11.   margin-top:2px;  
  12.   width150px;  
  13.   height200px;  
  14. }  


七、自定义的用于输出生成树形结构的XML的Servlet或Action:

[java]  view plain  copy
  1. package com.moonNigh.servlet;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.PrintWriter;  
  5.   
  6. import javax.servlet.ServletException;  
  7. import javax.servlet.http.HttpServlet;  
  8. import javax.servlet.http.HttpServletRequest;  
  9. import javax.servlet.http.HttpServletResponse;  
  10.   
  11. public class GetTreeXML extends HttpServlet {  
  12.   
  13.     /** 
  14.      * 
  15.      */  
  16.     private static final long serialVersionUID = 1L;  
  17.   
  18.     @Override  
  19.     protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
  20.             throws ServletException, IOException {  
  21.         this.doPost(req, resp);  
  22.     }  
  23.   
  24.     @Override  
  25.     protected void doPost(HttpServletRequest req, HttpServletResponse resp)  
  26.             throws ServletException, IOException {  
  27.         // TODO Auto-generated method stub  
  28.         resp.setCharacterEncoding("UTF-8");  
  29.         resp.setContentType("UTF-8");  
  30.         PrintWriter out=resp.getWriter();  
  31.         StringBuilder sb=new StringBuilder();  
  32.         sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");  
  33.         sb.append("<xmlRoot value=\"中国\" id=\"china\">");  
  34.         sb.append("<child value=\"四川\" id=\"sc\">");  
  35.         sb.append("<child id=\"ctu\">成都</child>");  
  36.         sb.append("</child><child id=\"gz\">贵州</child></xmlRoot>");  
  37.         out.write(sb.toString());  
  38.         out.flush();  
  39.     }  
  40. }  


八、测试JSP标签使用

<c:selector id="ctest" name="test" actionUrl="getTreeXml"/>


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值