隨筆-34  評(píng)論-1965  文章-0  trackbacks-0

          在上一篇文章《Struts 2與AJAX(第一部分)》,我已經(jīng)簡(jiǎn)單地介紹了<s:tree />的一些用法,接下來(lái)我將繼續(xù)深入講解<s:tree />的使用和通過(guò)DWR實(shí)現(xiàn)AJAX校驗(yàn)。

          更多<s:tree />

          在Struts 2的showcase中有兩個(gè)<s:tree />的例子,分別是靜態(tài)樹(shù)與動(dòng)態(tài)樹(shù)。所謂的靜態(tài)樹(shù)即是在編寫(xiě)JSP代碼時(shí)通過(guò)<s:treenode />生成樹(shù)節(jié)點(diǎn)。我的上一篇文章的例子就是一個(gè)典型的靜態(tài)樹(shù)。而動(dòng)態(tài)樹(shù)則是在程序運(yùn)行期間,Struts 2 運(yùn)行時(shí)(Runtime)根據(jù)程序中的數(shù)據(jù)動(dòng)態(tài)創(chuàng)建樹(shù)節(jié)點(diǎn)。雖然在兩個(gè)例子中<s:tree />的theme屬性都為“ajax”,但是從嚴(yán)格意義上來(lái)說(shuō),這兩種樹(shù)都不屬于AJAX樹(shù),因?yàn)樗鼈兌际窃谳敵鲰?yè)面時(shí)將全部節(jié)點(diǎn)加載到其中,而不是在父節(jié)點(diǎn)展開(kāi)時(shí)通過(guò)XHR(XMLHttpRequest)獲取節(jié)點(diǎn)數(shù)據(jù)。

          動(dòng)態(tài)樹(shù)

          下面我們先看一下動(dòng)態(tài)樹(shù)的例子,接著再一步步地將其改造為名副其實(shí)的AJAX 樹(shù)。下例將會(huì)把WEB應(yīng)用程序的目錄樹(shù)展現(xiàn)在JSP頁(yè)面中。因此,我需要先包裝一下java.io.File 類(lèi),代碼如下:

          package tutorial;

          import java.io.File;

          public class FileWrapper {
             
          private File file;

             
          public FileWrapper(String path) {
                 file
          = new File(path);
             }

             
             
          public FileWrapper(File file) {
                 
          this.file = file;
             }

             
             
          public String getId() {
                 
          return "file_" + file.hashCode();
             }

             
             
          public String getName() {
                 
          return file.getName();
             }

             
             
          public String getAbsolutePath() {
                 
          return file.getAbsolutePath();
             }

             
             
          public FileWrapper[] getChildren() {
                 File[] files
          = file.listFiles();
                 
          if(files != null && files.length > 0) {
                     
          int length = files.length;
                     FileWrapper[] wrappers
          = new FileWrapper[length];
                     
          for(int i = 0; i < length; ++i) {
                         wrappers[i]
          = new FileWrapper(files[i]);
                     }

                     
          return wrappers;
                 }

                 
          return new FileWrapper[0];
             }

          }
          清單1 src/tutorial/FileWrapper.java

          之所以需要對(duì)File類(lèi)進(jìn)行如此包裝,是因?yàn)?lt;s:tree />用于動(dòng)態(tài)樹(shù)時(shí),rootNode、nodeIdProperty、nodeTitleProperty 和 childCollectionProperty等屬性都必填的。

          然后是Action類(lèi)的代碼如下:

          package tutorial;

          import javax.servlet.http.HttpServletRequest;

          import org.apache.struts2.interceptor.ServletRequestAware;

          import com.opensymphony.xwork2.ActionSupport;

          public class DynamicTreeAction extends ActionSupport implements ServletRequestAware {
             
          private static final long serialVersionUID = 1128593047269036737L;
             
             
          private HttpServletRequest request;
             
          private FileWrapper root;

             
          public void setServletRequest(HttpServletRequest request) {    
                 
          this.request = request;
             }

             
             
          public FileWrapper getRoot() {
                 
          return root;
             }

             
             @Override
             
          public String execute() {
                 root
          = new FileWrapper(request.getSession().getServletContext().getRealPath("/"));        
                 
          return SUCCESS;
             }

          }
          清單2 src/tutorial/DynamicTreeAction.java

          上述代碼取得WEB應(yīng)用程序的根目錄的絕對(duì)路徑后,初始化FileWrapper對(duì)象root。該對(duì)象將為JSP頁(yè)面的<s:tree />的根節(jié)點(diǎn)。如下代碼所示:

          <%@ page language="java" contentType="text/html; charset=utf-8"
              pageEncoding
          ="utf-8"%>
          <%@ taglib prefix="s" uri="/struts-tags"%>

          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
          <html xmlns="http://www.w3.org/1999/xhtml">
             
          <head>
                 
          <title>Struts 2 AJAX - More Tree</title>
                 
          <s:head theme="ajax" debug="true" />
                 
          <script type="text/javascript">
                 
          /* <![CDATA[ */
                     
          function treeNodeSelected(arg) {
                          alert(arg.source.title
          + ' selected');
                      }
                      
                     
          function treeNodeExpanded(arg) {
                          alert(arg.source.title
          + ' expanded');
                      }
                      
                     
          function treeNodeCollapsed(arg) {
                          alert(arg.source.title
          + ' collapsed');
                      }
                      
                      dojo.addOnLoad(
          function() {                
                         
          var t = dojo.widget.byId('appFiles');
                          dojo.event.topic.subscribe(t.eventNames.expand, treeNodeExpanded);                
                          dojo.event.topic.subscribe(t.eventNames.collapse, treeNodeCollapsed);
                          
                         
          var s = t.selector;                
                          dojo.event.connect(s, 'select', 'treeNodeSelected');
                      });
                 
          /* ]]> */    
                 
          </script>
             
          </head>
             
          <body>
                 
          <h2>
                      Dynamic Tree Example
                 
          </h2>
                 
          <div style="float:left; margin-right: 50px;">
                     
          <s:tree id="appFiles" theme="ajax" rootNode="root"
                          nodeTitleProperty
          ="name" nodeIdProperty="id"
                          childCollectionProperty
          ="children" />
                 
          </div>
             
          </body>
          </html>
          清單3 WebContent/Tree.jsp

          因?yàn)?lt;s:tree />的treeCollapsedTopic和treeExpandedTopic屬性都沒(méi)有起作用,所以如果我們想要監(jiān)聽(tīng)這兩個(gè)事件,就必須使用上述代碼的方法。

          最后是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="Struts2_AJAX_DEMO" extends="struts-default">
                 
          <action name="DynamicTree" class="tutorial.DynamicTreeAction">
                     
          <result>Tree.jsp</result>
                 
          </action>
             
          </package>
          </struts>
          清單4 src/struts.xml

          發(fā)布運(yùn)行應(yīng)用程序,在瀏覽器地址欄中鍵入http://localhost:8080/Struts2_Ajax2/DynamicTree.action,有如下圖所示頁(yè)面:


          圖1 動(dòng)態(tài)樹(shù)示例

          AJAX 樹(shù)

          正如我在文章開(kāi)頭所說(shuō),Struts 2所提供的靜態(tài)樹(shù)和動(dòng)態(tài)樹(shù)都不是嚴(yán)格意義上的AJAX樹(shù)。下面就讓我們來(lái)實(shí)現(xiàn)一個(gè)如假包換的AJAX樹(shù)。首先要說(shuō)明的是,Struts 2的<s:tree />默認(rèn)是不支持這種按需加載數(shù)據(jù)的AJAX樹(shù)。不過(guò)因?yàn)樗腔贒ojo的樹(shù)控件(Widget)所以要擴(kuò)展也很方便。

          Dojo 通過(guò)名為“TreeRPCController”的控件實(shí)現(xiàn) AJAX 樹(shù),它會(huì)監(jiān)聽(tīng)被控制樹(shù)的事件。當(dāng)發(fā)生展開(kāi)節(jié)點(diǎn)的事件時(shí),TreeRPCController就會(huì)向URL發(fā)送XHR請(qǐng)求,該URL由TreeRPCController的RPCUrl 屬性定義。XHR請(qǐng)求格式類(lèi)似如下格式:

          http://localhost:8080/Struts2_Ajax2/AjaxTree.action?action=getChildren&data={"node":{"widgetId":"file_226092423","objectId":"C:\\Program Files\\Tomcat 5.5\\webapps\\Struts2_Ajax2","index":0,"isFolder":true},"tree":{"widgetId":"appFiles","objectId":""}}&dojo.preventCache=1182913465392
          清單5 XHR樣本

          顯而易見(jiàn),請(qǐng)求中包含三個(gè)參數(shù),分別是action為“getChildren”(固定值),data一個(gè)包含當(dāng)前節(jié)點(diǎn)與樹(shù)信息的JSON串和dojo.preventCache隨機(jī)串,用于緩存不同節(jié)點(diǎn)的請(qǐng)求響應(yīng)(父節(jié)點(diǎn)只會(huì)在第一次被展開(kāi)時(shí)到服務(wù)器端加載數(shù)據(jù),之后都是從瀏覽器的緩存中讀取數(shù)據(jù),可以提高應(yīng)用程序性能)。

          首先我要先寫(xiě)一個(gè)加載樹(shù)節(jié)點(diǎn)數(shù)據(jù)的Action類(lèi),代碼如下:

          package tutorial;

          import java.util.Map;

          import com.googlecode.jsonplugin.JSONExeption;
          import com.googlecode.jsonplugin.JSONUtil;

          public class AjaxTreeAction extends DynamicTreeAction {
             
          private static final long serialVersionUID = 3970019751740942311L;

             
          private String action;
             
          private String data;
             
          private FileWrapper[] wrappers;

             
          public void setAction(String action) {
                 
          this.action = action;
             }

             
             
          public void setData(String data) {
                 
          this.data = data;
             }


             
          public FileWrapper[] getWrappers() {
                 
          return wrappers;
             }


             @Override
             
          public String execute() {
                 
          if("getChildren".equals(action)) {
                     
          try {
                         Object o
          = JSONUtil.deserialize(data);
                         String path
          = ((Map) ((Map) o).get("node")).get("objectId").toString();
                         wrappers
          = new FileWrapper(path).getChildren();
                     }
          catch (JSONExeption e) {
                         e.printStackTrace();
                     }

                     
          return "ajax";
                 }
             
                 
          return super.execute();
             }

          }
          清單6 src/tutorial/AjaxTreeAction.java

          上述代碼可能需要解釋一下:

          1. action屬性對(duì)應(yīng)于XHR中的action,如果它為“getChildren”時(shí),則需要進(jìn)行加載子節(jié)點(diǎn)操作。否則,會(huì)讀取樹(shù)的根節(jié)點(diǎn),并返回JSP頁(yè)面;
          2. 通過(guò)上面XHR的分析,大家可以知道data是代表樹(shù)和當(dāng)前節(jié)點(diǎn)的JSON串,故應(yīng)將其反串行化為Map對(duì)象,并將其 objectId屬性取出。通常情況下,Dojo樹(shù)的objectId屬性代表服務(wù)器端的對(duì)象的標(biāo)識(shí),在本例中為文件夾的絕對(duì)路徑;
          3. wrappers屬性表示當(dāng)前文件夾下的文件數(shù)組,它被傳送到Freemarker頁(yè)面,翻譯為Dojo樹(shù)節(jié)點(diǎn)數(shù)組的JSON串。

          下面是Freemarker頁(yè)面的代碼:

          [
          <#list wrappers as r>
              { "title": "${r.name}", "isFolder":
          <#if r.children?size gt 0>true<#else>false</#if>, "id": "${r.id}", "objectId": "${r.absolutePath?js_string}" }<#if r_has_next>,</#if>
          </#list>
          ]
          清單7 WebContent/AjaxTree.ftl

          以上代碼中<#list></#lsit>的寫(xiě)法是Freemarker中遍歷集合的寫(xiě)法;而<#if r.children?size gt 0>判斷“r”對(duì)象的children屬性是否為空;r.absolutePath?js_string 就是將“r”的absolutePath屬性的值輸出為Javascript 的字串符形式;<#if r_has_next></#if>判斷集合是否有下一項(xiàng)數(shù)據(jù)。如果希望更詳細(xì)地了解Freemarker的使用,請(qǐng)參考該手冊(cè)

          接下來(lái),讓我們看看Action的配置代碼片段:

                  <action name="AjaxTree" class="tutorial.AjaxTreeAction">
                     
          <result>AjaxTree.jsp</result>
                     
          <result name="ajax" type="freemarker">AjaxTree.ftl</result>
                 
          </action>
          清單8 src/struts.xml配置片段

          最后是JSP頁(yè)面代碼:

          <%@ page language="java" contentType="text/html; charset=utf-8"
              pageEncoding
          ="utf-8"%>
          <%@ taglib prefix="s" uri="/struts-tags"%>

          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
          <html xmlns="http://www.w3.org/1999/xhtml">
             
          <head>
                 
          <title>Struts 2 AJAX - More Tree</title>
                 
          <s:head theme="ajax" debug="true" />
                 
          <script type="text/javascript">
                 
          /* <![CDATA[ */
                     
          function treeNodeSelected(arg) {
                          alert(arg.source.title
          + ' selected');
                      }
                          
                      dojo.addOnLoad(
          function() {                
                         
          var t = dojo.widget.byId('appFiles');                
                         
          var s = t.selector;                
                          dojo.event.connect(s, 'select', 'treeNodeSelected');
                      });    
                 
          /* ]]> */    
                 
          </script>
             
          </head>
             
          <body>
                 
          <h2>
                      AJAX Tree Example
                 
          </h2>
                 
          <div style="float:left; margin-right: 50px;">
                     
          <script type="text/javascript">
                     
          /* <![CDATA[ */
                          dojo.require(
          "dojo.lang.*");
                          dojo.require(
          "dojo.widget.*");
                          dojo.require(
          "dojo.widget.Tree");
                          dojo.require(
          "dojo.widget.TreeRPCController");                
                       
          /* ]]> */
                       
          </script>             
                     
          <div dojoType="TreeRPCController" widgetId="treeController"
                          DNDcontroller
          ="create" RPCUrl="<s:url />"></div>
                     
          <div dojoType="Tree" widgetId="appFiles" toggle="fade" controller="treeController">
                         
          <div dojoType="TreeNode" title='<s:property value="root.name" />'
                              widgetId='
          <s:property value="root.id" />'
                              isFolder='
          <s:property value="root.children.length > 0" />'
                              objectId='
          <s:property value="root.absolutePath" />'>
                         
          </div>
                     
          </div>
                 
          </div>
             
          </body>
          </html>
          清單9 WebContent/AjaxTree.jsp

          由于上面所提及的原因,我在上述的代碼中并沒(méi)有使用<s:tree />標(biāo)志,而是使用了Dojo的寫(xiě)法——?jiǎng)?chuàng)建 widgetId 為“treeController”的 TreeRPCController 并將設(shè)為樹(shù)的控制器。

          發(fā)布運(yùn)行應(yīng)用程序,在瀏覽器地址欄中鍵入http://localhost:8080/Struts2_Ajax2/AjaxTree.action,點(diǎn)開(kāi)某個(gè)節(jié)點(diǎn),在節(jié)點(diǎn)加載的過(guò)程中,加號(hào)圖標(biāo)變成時(shí)鐘狀圖標(biāo),如下圖所示頁(yè)面:


          圖2 AJAX樹(shù)示例

          自定義<s:tree />的AJAX的主題(theme)

          Struts 2的標(biāo)志過(guò)人之外在于它允許開(kāi)發(fā)人員自定義標(biāo)志的頁(yè)面輸出。要做到這一點(diǎn),你所需要做的只是創(chuàng)建一個(gè)自定義的theme并將其應(yīng)用到相應(yīng)標(biāo)志。下面就讓我自定義一個(gè)真正的AJAX的<s:tree/>的theme。

          首先,你的源文件的根目錄下新建包“template.realajax”。

          然后,在上一步所建的包中新建“tree.ftl”文件,內(nèi)容如下:

          <script type="text/javascript">
          /* <![CDATA[ */
              dojo.require(
          "dojo.lang.*");
              dojo.require(
          "dojo.widget.*");
              dojo.require(
          "dojo.widget.Tree");
              dojo.require(
          "dojo.widget.TreeRPCController");    <#-- Added by Max -->            
          /* ]]> */
          </script>    
          <#-- Added by Max -->             
          <div dojoType="TreeRPCController" 
               widgetId
          ="${parameters.id?html}_controller"
               DNDcontroller
          ="create" 
               RPCUrl
          ="<@s.url />">
          </div>
          <#-- End -->    
          <div dojoType="Tree"   
              <#if parameters.blankIconSrc?exists
          >
              gridIconSrcT="
          <@s.url value='${parameters.blankIconSrc}' encode="false" includeParams='none'/>"
             
          </#if>
             
          <#if parameters.gridIconSrcL?exists>
              gridIconSrcL="
          <@s.url value='${parameters.gridIconSrcL}' encode="false" includeParams='none'/>"
             
          </#if>
             
          <#if parameters.gridIconSrcV?exists>
              gridIconSrcV="
          <@s.url value='${parameters.gridIconSrcV}' encode="false" includeParams='none'/>"
             
          </#if>
             
          <#if parameters.gridIconSrcP?exists>
              gridIconSrcP="
          <@s.url value='${parameters.gridIconSrcP}' encode="false" includeParams='none'/>"
             
          </#if>
             
          <#if parameters.gridIconSrcC?exists>
              gridIconSrcC="
          <@s.url value='${parameters.gridIconSrcC}' encode="false" includeParams='none'/>"
             
          </#if>
             
          <#if parameters.gridIconSrcX?exists>
              gridIconSrcX="
          <@s.url value='${parameters.gridIconSrcX}' encode="false" includeParams='none'/>"
             
          </#if>
             
          <#if parameters.gridIconSrcY?exists>
              gridIconSrcY="
          <@s.url value='${parameters.gridIconSrcY}' encode="false" includeParams='none'/>"
             
          </#if>
             
          <#if parameters.gridIconSrcZ?exists>
              gridIconSrcZ="
          <@s.url value='${parameters.gridIconSrcZ}' encode="false" includeParams='none'/>"
             
          </#if>
             
          <#if parameters.expandIconSrcPlus?exists>
              expandIconSrcPlus="
          <@s.url value='${parameters.expandIconSrcPlus}' includeParams='none'/>"
             
          </#if>
             
          <#if parameters.expandIconSrcMinus?exists>
              expandIconSrcMinus="
          <@s.url value='${parameters.expandIconSrcMinus?html}' includeParams='none'/>"
             
          </#if>
             
          <#if parameters.iconWidth?exists>
              iconWidth="
          <@s.url value='${parameters.iconWidth?html}' encode="false" includeParams='none'/>"
             
          </#if>
             
          <#if parameters.iconHeight?exists>
              iconHeight="
          <@s.url value='${parameters.iconHeight?html}' encode="false" includeParams='none'/>"
             
          </#if>
             
          <#if parameters.toggleDuration?exists>
              toggleDuration=${parameters.toggleDuration?c}
             
          </#if>
             
          <#if parameters.templateCssPath?exists>
              templateCssPath="
          <@s.url value='${parameters.templateCssPath}' encode="false" includeParams='none'/>"
             
          </#if>
             
          <#if parameters.showGrid?exists>
              showGrid="${parameters.showGrid?default(true)?string}"
             
          </#if>
             
          <#if parameters.showRootGrid?exists>
              showRootGrid="${parameters.showRootGrid?default(true)?string}"
             
          </#if>
             
          <#if parameters.id?exists>
              id="${parameters.id?html}"
             
          </#if>
             
          <#if parameters.treeSelectedTopic?exists>
              publishSelectionTopic="${parameters.treeSelectedTopic?html}"
             
          </#if>
             
          <#if parameters.treeExpandedTopic?exists>
              publishExpandedTopic="${parameters.treeExpandedTopic?html}"
             
          </#if>
             
          <#if parameters.treeCollapsedTopic?exists>
              publishCollapsedTopic="${parameters.treeCollapsedTopic?html}"
             
          </#if>
             
          <#if parameters.toggle?exists>
              toggle="${parameters.toggle?html}"
             
          </#if>
              controller="${parameters.id?html}_controller"
          <#-- Added by Max -->    
              >
             
          <#if parameters.label?exists>
             
          <div dojoType="TreeNode" title="${parameters.label?html}"
              <#if parameters.nodeIdProperty?exists
          >
              id="${stack.findValue(parameters.nodeIdProperty)}"
             
          <#else>
              id="${parameters.id}_root"
             
          </#if>
              >
             
          <#elseif parameters.rootNode?exists>
              ${stack.push(parameters.rootNode)}
             
          <#-- Edited by Max -->    
             
          <div dojoType="TreeNode" 
                       title
          ="${stack.findValue(parameters.nodeTitleProperty)}"
                       widgetId
          ="${stack.findValue(parameters.nodeIdProperty)}"
                       isFolder
          ="<#if stack.findValue(parameters.childCollectionProperty)?size gt 0>true<#else>false</#if>"
                       objectId
          ="${stack.findValue(parameters.nameValue)}">
                 
          </div>
             
          <#-- End -->        
             
          <#assign oldNode = stack.pop()/> <#-- pop the node off of the stack, but don't show it -->
             
          </#if>    
          清單10 src/template/realajax/tree.ftl

          對(duì)上述稍作解釋?zhuān)鲜龃a主要在原版的src/template/ajax/tree.ftl的基礎(chǔ)上添加了TreeRPCController的控件,并只輸出根節(jié)點(diǎn)。由于<s:tree />沒(méi)有類(lèi)似nodeObjectIdProperty的屬性,所以我用了value屬性表示objectId對(duì)應(yīng)的屬性名稱(chēng)。

          接著新建tree-close.ftl文件,內(nèi)容和原版的一樣,如下所示:

          <#if parameters.label?exists></div></#if></div>
          清單11 src/template/realajax/tree-close.ftl

          再下來(lái)就應(yīng)該是將theme應(yīng)用到<s:tree />,如下代碼所示:

          <%@ page language="java" contentType="text/html; charset=utf-8"
              pageEncoding
          ="utf-8"%>
          <%@ taglib prefix="s" uri="/struts-tags"%>

          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
          <html xmlns="http://www.w3.org/1999/xhtml">
             
          <head>
                 
          <title>Struts 2 AJAX - More Tree</title>
                 
          <s:head theme="ajax" debug="true" />
                 
          <script type="text/javascript">
                 
          /* <![CDATA[ */
                     
          function treeNodeSelected(arg) {
                          alert(arg.source.title
          + ' selected');
                      }
                          
                      dojo.addOnLoad(
          function() {                
                         
          var t = dojo.widget.byId('appFiles');                
                         
          var s = t.selector;                
                          dojo.event.connect(s, 'select', 'treeNodeSelected');
                      });    
                 
          /* ]]> */    
                 
          </script>
             
          </head>
             
          <body>
                 
          <h2>
                      AJAX Tree Example
                 
          </h2>
                 
          <div style="float:left; margin-right: 50px;">
                     
          <s:tree id="appFiles" theme="realajax" rootNode="root"
                          nodeTitleProperty
          ="name" nodeIdProperty="id"
                          childCollectionProperty
          ="children" value="absolutePath" />
                 
          </div>
             
          </body>
          </html>
          清單12 WebContent/AjaxTreeTheme.jsp

          上述代碼中<s:tree />的用法,除了theme改為“realajax”和多了value="absolutePath"外,幾乎和靜態(tài)樹(shù)中的一樣。

          為了不影響前一個(gè)例子,我們?yōu)樵揓SP文件配置類(lèi)型相同的Action,如下代碼所示:

                  <action name="AjaxTreeTheme" class="tutorial.AjaxTreeAction">
                     
          <result>AjaxTreeTheme.jsp</result>
                     
          <result name="ajax" type="freemarker">AjaxTree.ftl</result>
                 
          </action>
          清單13 src/struts.xml配置片段

          發(fā)布運(yùn)行應(yīng)用程序,在瀏覽器地址欄中鍵入http://localhost:8080/Struts2_Ajax2/AjaxTreeTheme.action,結(jié)果如圖2所示。

          總結(jié)

          通過(guò)上述例子,大家知道Struts 2 的AJAX 標(biāo)志是基于Dojo控件開(kāi)發(fā)的,所以如果大家希望熟練地使用這些標(biāo)志,最好去了解一下Dojo。

          本來(lái)還打算介紹一下Struts 2與DWR,不過(guò)看看文章的篇幅似乎足夠自成一篇了,因此DWR相關(guān)的內(nèi)容要留待下文繼續(xù)了。

          posted on 2007-06-27 18:33 Max 閱讀(31393) 評(píng)論(41)  編輯  收藏 所屬分類(lèi): Struts 2.0系列

          評(píng)論:
          # re: Struts 2與AJAX(第二部分) 2007-06-27 23:34 | 大于
          MAX!謝謝你的文章,這些天本人一直在學(xué)習(xí)你的技術(shù)文章。真的很感謝,也很崇拜你!!!  回復(fù)  更多評(píng)論
            
          # re: Struts 2與AJAX(第二部分)[未登錄](méi) 2007-06-28 09:01 | javaman
          謝謝,期待很久了!~  回復(fù)  更多評(píng)論
            
          # re: Struts 2與AJAX(第二部分)[未登錄](méi) 2007-06-28 11:31 | javaman
          文中舉的例子是通過(guò) 本工程下文件夾的包含關(guān)系來(lái)確定樹(shù)中父與子的關(guān)系,

          但是如何通過(guò)數(shù)據(jù)庫(kù)的記錄來(lái)確定樹(shù)中父與子的關(guān)系呢,比如一條記錄中有父親的id 還有自己的id。這樣通過(guò)<S:tree/>標(biāo)簽動(dòng)態(tài)生成樹(shù)呢?  回復(fù)  更多評(píng)論
            
          # re: Struts 2與AJAX(第二部分) 2007-06-28 23:00 | Max
          @javaman
          你可以在你的實(shí)體類(lèi)加入children屬性,如
          public class MyEntity {
          private int id;
          private List<MyEntity> children;
          /* getters and setters */
          }
          有Action中你可以通過(guò)對(duì)象id獲得對(duì)象,再通過(guò)對(duì)象的getChildren()方法獲得子對(duì)象列表。具體做法和本文相似。  回復(fù)  更多評(píng)論
            
          # re: Struts 2與AJAX(第二部分) 2007-06-29 17:44 | ehe
          # re: Struts 2與AJAX(第二部分) 2007-07-03 10:40 | lxb
          樓主,為什么我打包時(shí)jar都不加進(jìn)去的啊,那里需要設(shè)置一下啊,謝謝  回復(fù)  更多評(píng)論
            
          # re: Struts 2與AJAX(第二部分) 2007-07-03 11:16 | lxb
          另外樓主可以把關(guān)于struts2.0的一些其他資料發(fā)給我一些嗎?還有你的示例源碼,謝謝,luxb@teamsun.com.cn  回復(fù)  更多評(píng)論
            
          # re: Struts 2與AJAX(第二部分) 2007-07-14 11:25 | txm
          摟主,如果是動(dòng)態(tài)加載的數(shù),請(qǐng)問(wèn)在節(jié)點(diǎn)的select事件中如何獲得id?您的程序中是用souece.title獲得節(jié)點(diǎn)name,但是我用source.objectId獲得的卻是空值?請(qǐng)問(wèn)有別的辦法嗎?  回復(fù)  更多評(píng)論
            
          # re: Struts 2與AJAX(第二部分) 2007-07-15 17:43 | 蛋白質(zhì)
          這里的評(píng)論少點(diǎn),說(shuō)不定作者還能看到我的說(shuō)話,有太多問(wèn)題想問(wèn)了,不知道怎么聯(lián)系下你呢?請(qǐng)指導(dǎo)下吧,我的郵箱danbaizhi0733@msn.com
          今天發(fā)個(gè)了消息你了,希望能給點(diǎn)指導(dǎo),謝謝  回復(fù)  更多評(píng)論
            
          # re: Struts 2與AJAX(第二部分)[未登錄](méi) 2007-07-19 21:25 | jacky
          JSP代碼:

          <div dojoType="TreeRpcControllerV3" widgetId="controller" editor="editor"
          RpcUrl='<s:url action="list" namespace="/pages/product" />' ></div>

          STRUTS-CONFIG:


          <action name="list" class="productCategoryAction"
          method="list">
          <result>/pages/product/index.jsp</result>
          </action>

          我想從JSP頁(yè)面直接調(diào)用action,但是老是走不進(jìn)去,請(qǐng)問(wèn)樓主能不能告訴我怎么改下阿?急用,謝謝啦!!
          kiddshen@hotmail.com我的郵箱,樓主能不能給各聯(lián)系方式阿?  回復(fù)  更多評(píng)論
            
          # re: Struts 2與AJAX(第二部分) 2007-07-24 00:05 | Max
          @jacky
          你可以通過(guò)Gmail:max.m.yuan@gmail.com找到我。  回復(fù)  更多評(píng)論
            
          # re: Struts 2與AJAX(第二部分) 2007-08-10 14:04 | ryofeng
          很多的地方都用到點(diǎn)樹(shù)的某一個(gè)節(jié)點(diǎn)然后轉(zhuǎn)到相應(yīng)的頁(yè)面,想知道在struts2 的Tree 里怎么實(shí)現(xiàn)?  回復(fù)  更多評(píng)論
            
          # re: Struts 2與AJAX(第二部分) 2007-08-14 11:27 | cljhyjs
          好文章  回復(fù)  更多評(píng)論
            
          # re: Struts 2與AJAX(第二部分) 2007-08-14 11:27 | cljhyjs
          # re: Struts 2與AJAX(第二部分)[未登錄](méi) 2007-08-17 13:57 | 過(guò)客
          受益頗多,謝謝!  回復(fù)  更多評(píng)論
            
          # re: Struts 2與AJAX(第二部分) 2007-09-06 16:58 | ch
          Max大哥,怎么才能實(shí)現(xiàn)動(dòng)態(tài)加一個(gè)樹(shù)的節(jié)點(diǎn)啊,比如選擇一個(gè)樹(shù)的節(jié)點(diǎn),點(diǎn)一個(gè)按鈕后動(dòng)態(tài)生成子節(jié)點(diǎn)。  回復(fù)  更多評(píng)論
            
          # re: Struts 2與AJAX(第二部分) 2007-09-15 13:47 | @s
          需要好好學(xué)學(xué)DOJO....如果想做更令人滿意的樹(shù)的話  回復(fù)  更多評(píng)論
            
          # re: Struts 2與AJAX(第二部分) 2007-10-10 11:23 | sgfgh
          max兄,我讀你的struts2系列教程獲益良多,但在閱讀Struts 2與AJAX(第二部分)時(shí),發(fā)現(xiàn)那個(gè)tree出現(xiàn)了中文問(wèn)題,data部分{"node":{"widgetId":"file_98236965","objectId":"F:\\???é??","index":0,"isFolder":true},"tree":{"widgetId":"appFiles","objectId":""}}
          不知道如何解決,希望max兄幫我一下,謝謝  回復(fù)  更多評(píng)論
            
          # re: Struts 2與AJAX(第二部分) 2007-10-10 11:51 | sgfgh
          我在后臺(tái)使用new String(data.getBytes("ISO-8859-1"),"UTF-8")是可以得到正確的中文啦,但是我還是希望在前臺(tái)處理這個(gè)字符串@sgfgh
            回復(fù)  更多評(píng)論
            
          # re: Struts 2與AJAX(第二部分) 2007-10-12 18:02 | silence
          你是java界的大俠!  回復(fù)  更多評(píng)論
            
          # re: Struts 2與AJAX(第二部分) 2007-11-20 14:35 | 獨(dú)自私奔
          兄弟不知道你能不能看到,我不知道這個(gè)樹(shù)能不參加復(fù)選框,進(jìn)行像權(quán)限設(shè)置這類(lèi)的工作.如果有空請(qǐng)您想一下,如有結(jié)果請(qǐng)與我jufuduo@163.com聯(lián)系.希望你的幫助!  回復(fù)  更多評(píng)論
            
          # re: Struts 2與AJAX(第二部分)[未登錄](méi) 2008-01-11 16:42 | JET
          root = new FileWrapper(request.getSession().getServletContext().getRealPath("/"));
          為何我提示request沒(méi)有初始化。
          --------------------------------------------------------------------------------

          type Exception report

          message

          description The server encountered an internal error () that prevented it from fulfilling this request.

          exception

          javax.servlet.ServletException: java.lang.NullPointerException
          org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:515)
          org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)


          root cause

          java.lang.NullPointerException
          tutorial.DynamicTreeAction.execute(DynamicTreeAction.java:22)

          第22行 root = new FileWrapper (request.getSession().getServletContext().getRealPath("/"));
            回復(fù)  更多評(píng)論
            
          # re: Struts 2與AJAX(第二部分) 2008-01-17 10:12 | 心情流淌
          寫(xiě)得太好啦。受益匪淺  回復(fù)  更多評(píng)論
            
          # re: Struts 2與AJAX(第二部分) 2008-01-17 17:50 | 迷惑者
          如果將樹(shù)形結(jié)果作為菜單使用,請(qǐng)問(wèn):如果給樹(shù)節(jié)點(diǎn)添加一個(gè)鏈接?  回復(fù)  更多評(píng)論
            
          # re: Struts 2與AJAX(第二部分) 2008-02-01 15:33 | ZUES
          Max,感謝你的文章,寫(xiě)的很好,請(qǐng)問(wèn)個(gè)問(wèn)題:如果想要TREE默認(rèn)展開(kāi)怎么實(shí)現(xiàn)呢???  回復(fù)  更多評(píng)論
            
          # re: Struts 2與AJAX(第二部分) 2008-05-20 22:18 | winner
          依上步聚出現(xiàn):
          DEBUG: [SyntaxError: syntax error, file: http://localhost:8080/struts2/struts/dojo/dojo.js, line: 109]
          ....
          FATAL exception raised: loadProcessResponse: Not array loaded: false
          FATAL exception raised: unlock: not locked
          DEBUG: XMLHttpTransport error callback failed: Error: unlock: not locked
          這個(gè)異常是怎么回事?  回復(fù)  更多評(píng)論
            
          # re: Struts 2與AJAX(第二部分) 2008-05-31 12:09 | liusp
          動(dòng)態(tài)樹(shù)例子之中 File接點(diǎn)之間的上下級(jí)關(guān)系 《S:TREE >是通過(guò)什么屬性來(lái)獲取的,如果吧File對(duì)象換成普通bean,這數(shù)據(jù)該怎么組織?  回復(fù)  更多評(píng)論
            
          # re: Struts 2與AJAX(第二部分) 2008-06-02 10:41 | ayao
          多謝!非常好.  回復(fù)  更多評(píng)論
            
          # re: Struts 2與AJAX(第二部分) 2008-07-11 15:45 | sdf
          # re: Struts 2與AJAX(第二部分) 2008-07-14 11:41 | zenghongjun
          very good
            回復(fù)  更多評(píng)論
            
          # re: Struts 2與AJAX(第二部分) 2008-08-20 11:11 | 明天
          請(qǐng)樓主明示,如何獲得選定節(jié)點(diǎn)的ID?我使用arg.source.id報(bào)告“undefined"。  回復(fù)  更多評(píng)論
            
          # re: Struts 2與AJAX(第二部分) 2008-08-29 16:16 | 444
          不是很明白  回復(fù)  更多評(píng)論
            
          # re: Struts 2與AJAX(第二部分) 2009-01-04 20:14 | imake
          謝謝!  回復(fù)  更多評(píng)論
            
          # re: Struts 2與AJAX(第二部分) 2009-03-06 16:55 | pany
          @Max:
          先謝謝你提供的這些例子哈,讓我受益匪淺!
          另外,我在調(diào)試AjaxTree.action 和 AjaxTreeTheme.action兩個(gè)例子中均在頁(yè)面上出現(xiàn)如下內(nèi)容:
          FATAL exception raised: loadProcessResponse: Not array loaded: [object Object]
          FATAL exception raised: unlock: not locked
          DEBUG: XMLHttpTransport error callback failed: [object Error]
          讓我非常頭疼,在網(wǎng)上關(guān)于這個(gè)問(wèn)題的內(nèi)容也只有本頁(yè)內(nèi)"2008-05-20 22:18 | winner
          "的提問(wèn), 能請(qǐng)問(wèn)一下是為什么么?不勝感激!
          baiban@hotmail.com  回復(fù)  更多評(píng)論
            
          # re: Struts 2與AJAX(第二部分) 2009-03-09 11:11 | pany
          上個(gè)問(wèn)題找到原因了,是因?yàn)锳jaxTree.jsp里面的div不支持dojoType,widgetId,DNDcontroller,RPCUrl等等這些屬性,可是不曉得為什么....頁(yè)面上已有<s:head theme="ajax" debug="true" />


          還有個(gè)問(wèn)題,樹(shù)打開(kāi)很慢  回復(fù)  更多評(píng)論
            
          # re: Struts 2與AJAX(第二部分) 2009-03-25 21:11 | Shyhao
          謝謝,不過(guò)我用的是JavaScript框架,做出這樣的動(dòng)態(tài)書(shū)是很方便的,還可以修改外觀  回復(fù)  更多評(píng)論
            
          # re: Struts 2與AJAX(第二部分) 2009-04-08 18:32 | ccue
          function loadAjaxCommodity(commodityId) {
          dojo.io.bind({
          url:"../ajax/loadCommodity.action?id="+commodityId,
          handle:drawTable,
          method:"GET",
          sync:false,
          mimetype:"text/json"

          });
          }
          為什么在IE中只發(fā)送一次請(qǐng)求呢
            回復(fù)  更多評(píng)論
            
          # re: Struts 2與AJAX(第二部分) 2009-05-20 18:10 | allendai@nsetelecom.com
          看了你寫(xiě)的關(guān)于struts2.0,也不我現(xiàn)在用的struts2.1.6有許多東西都不一樣,而且在網(wǎng)上可查的文檔不多,你可不可以寫(xiě)點(diǎn)關(guān)于struts2.1.6的東西呢  回復(fù)  更多評(píng)論
            
          # re: Struts 2與AJAX(第二部分) 2009-11-12 18:34 | AL
          @ZUES
          我也很想知道啊。哪位能實(shí)現(xiàn)根節(jié)點(diǎn)默認(rèn)自動(dòng)展開(kāi)的功能?  回復(fù)  更多評(píng)論
            
          # re: Struts 2與AJAX(第二部分) 2009-11-12 18:35 | AL
          # re: Struts 2與AJAX(第二部分) 2008-02-01 15:33 | ZUES
          Max,感謝你的文章,寫(xiě)的很好,請(qǐng)問(wèn)個(gè)問(wèn)題:如果想要TREE默認(rèn)展開(kāi)怎么實(shí)現(xiàn)呢???
          ---------
          我也很想知道啊。哪位能實(shí)現(xiàn)根節(jié)點(diǎn)默認(rèn)自動(dòng)展開(kāi)的功能?   回復(fù)  更多評(píng)論
            
          # re: Struts 2與AJAX(第二部分) 2010-01-19 18:09 | AL
          @ZUES
          <div expandLevel="2" dojoType="Tree"......  回復(fù)  更多評(píng)論
            
          主站蜘蛛池模板: 太谷县| 富蕴县| 嘉兴市| 锦州市| 新蔡县| 定陶县| 江达县| 忻州市| 出国| 青州市| 柘荣县| 中牟县| 赤壁市| 衡南县| 临洮县| 和田市| 丘北县| 湄潭县| 尼玛县| 文山县| 陵水| 乌拉特中旗| 商南县| 乌海市| 石嘴山市| 基隆市| 安丘市| 陕西省| 柳河县| 镶黄旗| 三穗县| 贵南县| 会泽县| 天水市| 胶南市| 铜梁县| 福海县| 天气| 图们市| 漠河县| 延寿县|