www.pudn.com > struts2.rar > AjaxTreeAction.java, change:2009-08-21,size:1607b


package com.ajax.action; 
 
import java.util.Map; 
 
import com.ajax.business.FileWrapper; 
import com.googlecode.jsonplugin.JSONException; 
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; 
   } 
    
   /** 
    * 解释一下: 
   1. action属性对应于XHR中的action,如果它为“getChildren”时,则需要进行加载子节点操作。否则,会读取树的根节点,并返回JSP页面; 
   2. 通过上面XHR的分析,大家可以知道data是代表树和当前节点的JSON串,故应将其反串行化为Map对象,并将其 objectId属性取出。通常情况下,Dojo树的objectId属性代表服务器端的对象的标识,在本例中为文件夹的绝对路径; 
   3. wrappers属性表示当前文件夹下的文件数组,它被传送到Freemarker页面,翻译为Dojo树节点数组的JSON串。 
    */ 
   @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 (JSONException e) { 
               e.printStackTrace(); 
           } 
           return "ajax"; 
       }     
       return super.execute(); 
   } 
    
}