云自無心水自閑

          天平山上白云泉,云自無心水自閑。何必奔沖山下去,更添波浪向人間!
          posts - 288, comments - 524, trackbacks - 0, articles - 6
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          struts2 結(jié)合 dhtmlx tree

          Posted on 2008-08-07 20:16 云自無心水自閑 閱讀(5628) 評論(6)  編輯  收藏 所屬分類: 心得體會 、Struts2
          前一陣子使用了DHtmlx的Tree,視覺效果不錯,功能也不弱。具體參見:http://dhtmlx.com
          現(xiàn)在把Struts2結(jié)合DHtmlxTree的經(jīng)驗心得整理一下,發(fā)表出來:
          一、Struts.xml
          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE struts PUBLIC
              "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
              "http://struts.apache.org/dtds/struts-2.0.dtd">
            
          <struts>

              <package name="demo" extends="struts-default">
                  <action name="menu" method="execute" class="demo.TreeMenuAction">
                          <result>/WEB-INF/menu.jsp</result>
                  </action>
              </package>
          </struts>


          二、tree.jsp

          <%@ taglib prefix="s" uri="/struts-tags"%>
          <%
          String 
          path = request.getContextPath();
          String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path;
          %
          >
           
          <html>
           
          <head>
           
          <title>Main Page</title>
           
          <!-- 注意此處的路徑需要根據(jù)各自具體情況修改 --> 
          <link rel="STYLESHEET" type="text/css" href="styles/dhtmlxtree.css">
          <script src="scripts/dhtmlxcommon.js"></script> 
          <script src="scripts/dhtmlxtree.js"></script>
           
          </head>
           
          <body onload="loadTree(); " style="padding: 0; margin: 0; overflow: hidden; height: 100%;">
           
           
          <script>
          String.prototype._dhx_trim = function(){
              return this.replace(/
          &nbsp;/g," ").replace(/(^[ \t\n\r]*)|([ \t\n\r]*$)/g,"");
          }
           
           
          /* init tree */
           var tree;
            
          function loadTree(){
              tree=new dhtmlXTreeObject("doctree_box","100%","100%",0);
              tree.setImagePath("images/");    
          <!-- 注意此處的路徑需要根據(jù)各自具體情況修改 --> 
              tree.setOnClickHandler(
                  function(id){ openPathDocs(id); }
              );
              tree.loadXML("
          <%=basePath%>/menu.do"); 
           }
           
          /* open path funtion */

          function openPathDocs(id){
              if (tree.getUserData(id, "thisurl") != null ){
                  window.frames.sampleframe.location.href = "
          <%=path%>/" + tree.getUserData(id, "thisurl") + ".do";
                  return;
              }
          }
           
          function autoselectNode(){
              tree.selectItem(node,true);
              tree.openItem(node);

           
          </script>
           
           
           
           
          <table width="100%" height="100%" cellpadding="0" cellspacing="0" border="0">
              
          <tr>
                  
          <td valign="top" width="276">
                      
          <div id="doctree_box" style="width: 274px; height: 100%;"></div>
                  
          </td>
                  
          <td width="10" background="images/grid.gif">
                      
          &nbsp;
                  
          </td>
                  
          <td align="right"> 
                      
          <iframe id="sampleframe" name="sampleframe" width="100%" height="99%" frameborder="0" src="blank.html" style="border: 0px solid #cecece;"></iframe>
                  
          </td>
              
          </tr>
          </table>
           
           
          </body>
          </html>
           

          上面的JavaScript基本上是從dhtmlx的例子中修改而來,理解起來并不復(fù)雜,只有
          String.prototype._dhx_trim = function(){
           return this.replace(/&nbsp;/g," ").replace(/(^[ \t\n\r]*)|([ \t\n\r]*$)/g,"");
          }
          這一段代碼含義不明。


          三、Action

          package demo;
           
          public class TreeMenuAction {
           
              
          private String menuString;
           
              
          public String execute() {
                  StringBuffer buf 
          = new StringBuffer();
                  buf.append(
          "<tree id=\"0\">");
                  buf.append(
          " <item text=\"Java\">");
                  buf.append(
          " <item text=\"Thinking in java\">");
                  buf.append(
          " <userdata name=\"thisurl\">java_tij.do</userdata>");
                  buf.append(
          " </item>");
                  buf.append(
          " <item text=\"Head first design pattern\">");
                  buf.append(
          " <userdata name=\"thisurl\">java_hfdp.do</userdata>");
                  buf.append(
          " </item>");
                  buf.append(
          " </item>");
                  buf.append(
          " <item text=\"Fiction\">");
                  buf.append(
          " <item text=\"Harry Porter\">");
                  buf.append(
          " <userdata name=\"thisurl\">fiction_hp.do</userdata>");
                  buf.append(
          " </item>");
                  buf.append(
          " <item text=\"Oliver Twist\">");
                  buf.append(
          " <userdata name=\"thisurl\">fiction_ot.do</userdata>");
                  buf.append(
          " </item>");
                  buf.append(
          " </item>");
                  buf.append(
          "</tree>");
                   
                  menuString 
          = buf.toString();
                   
                  
          return "success";
              }

               
               
              
          public String getMenuString() {
                  
          return menuString;
              }

               
               
              
          public void setMenuString(String menuString) {
                  
          this.menuString = menuString;
              }

          }

           

           
          四、menu.jsp
           

          <%@ page contentType="text/xml;charset=UTF-8"%>
          <%@ taglib prefix="s" uri="/struts-tags" %>

          <s:property value="menuXmlString" escape="false"/>


          過程是這樣的:首先在瀏覽器地址欄中輸入:http://......./tree.jsp
          展示tree.jsp,在load函數(shù)中調(diào)用menu.do

          menu.do對應(yīng)TreeMenuAction,返回menu.jsp,而menu.jsp只包含menuString的值,注意在menu.jsp中的escape="false"


          評論

          # re: struts2 結(jié)合 dhtmlx tree  回復(fù)  更多評論   

          2009-05-20 23:51 by 永遠的雷歐
          menuString 的內(nèi)容就是 Xml字符串吧? 謝謝博主的文章!

          # re: struts2 結(jié)合 dhtmlx tree  回復(fù)  更多評論   

          2009-05-21 18:38 by usherlight
          @永遠的雷歐
          Yes.

          # re: struts2 結(jié)合 dhtmlx tree  回復(fù)  更多評論   

          2010-05-12 10:07 by wyonge@gmail.com
          我的例子也是這樣寫的。不過在IE下面顯示沒有問題,在FF下面,仍然提示xml結(jié)構(gòu)錯誤。
          用firebug查看了一下,action返回的時候,其實質(zhì)上是HTML格式。也就是說,返回的內(nèi)容contentType是text/html,而不是text/xml,不知道是不是這個原因,導(dǎo)致了FF覺得是錯誤!??!

          # re: struts2 結(jié)合 dhtmlx tree  回復(fù)  更多評論   

          2010-08-04 11:10 by www
          String.prototype._dhx_trim = function(){
          return this.replace(/&nbsp;/g," ").replace(/(^[ \t\n\r]*)|([ \t\n\r]*$)/g,"");
          }


          正則表達式,替換

          # re: struts2 結(jié)合 dhtmlx tree  回復(fù)  更多評論   

          2010-08-09 07:54 by usherlight
          @www
          使用正則表達式進行替換,我是知道的,但是關(guān)鍵是為什么進行這樣的替換?
          為什么把&nbsp換成回車?

          # re: struts2 結(jié)合 dhtmlx tree  回復(fù)  更多評論   

          2010-08-09 07:55 by usherlight
          沒看仔細,是把&nbsp;和回車全部都刪除了。
          主站蜘蛛池模板: 孟州市| 泊头市| 奉化市| 芒康县| 阿图什市| 安康市| 东至县| 延川县| 漠河县| 甘孜县| 色达县| 旬邑县| 桃园市| 河北省| 九台市| 万年县| 东阳市| 中牟县| 藁城市| 蓝田县| 察哈| 库车县| 平山县| 搜索| 广南县| 江陵县| 梨树县| 邮箱| 惠州市| 华安县| 宜君县| 饶河县| 韶关市| 敖汉旗| 沙坪坝区| 新丰县| 息烽县| 三河市| 嵊州市| 漳州市| 三台县|