TA的每日心情  | 开心 2021-3-12 23:18 | 
|---|
 
  签到天数: 2 天 [LV.1]初来乍到  
 | 
 
| 
 
 当文件层次比较深,数据量比较大时,反应会很慢。这时候应该使用Ajax与动态数据。Ajax能够只加载当前显示所需的少量数据。Dojo框架的数控件支持Ajax.struts2的Tree标签只是生成Dojo的树标签,理论上也应该支持Ajax,不过到目前为止,struts2的Tree标签并没有提供Ajax接口,尽管Dojo已经提供了。不过没有关系,可以直接越过struts2标签使用Dojo。 
     Dojo使用TreeRPCController来控制Ajax树。下面是一个使用Ajax树的代码,把Web应用的根目录作为根节点,遍历所有的子文件: 
[color=#990066,strength=3);]treeFileAjax.jsp 
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
<%@ page import="java.io.*" %> 
<%@ taglib uri="/struts-tags" prefix="struts" %> 
<HTML xmlns="http://www.w3.org/1999/xhtml"> 
  <head> 
    <title>Struts 2 Ajax - More Tree</title> 
    <struts:head theme="ajax" debug="false"/> 
  </head> 
  <body> 
  <h3>Ajax Tree Example</h3> 
<% 
   request.setAttribute("file",new File(getServletContext().getRealPath(""))); 
 %>   
  
 <div dojoType="TreeRPCController" widgetId="treeController" DNDcontroller="create" RPCUrl="treeFileAjaxData.jsp"> 
 </div> 
 <div dojoType="Tree" widgetId="appFiles" toggle="fade" controller="treeController"> 
   <div dojoType="TreeNode" title='<struts:property value="#request.file.name" />' 
               widgetId='<struts:property value="#request.file.absolutePath" />' 
               isFolder='<struts:property value="#request.file.directory" />' 
               objectId='<struts:property value="#request.file.absolutePath" />'></div> 
 </div> 
  </body> 
</html> 
treeFileAjaxData.jsp解析data参数,根据objectId属性获知展开的是哪个节点。然后找到相应的文件夹,把文件夹下的子文件与子文件夹返回给Dojo树。返回数据也是JSON格式的。 
[color=#990066,strength=3);]treeFileAjaxData.jsp 
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
<%@ page import="java.util.HashMap, 
                 java.util.List, 
                 java.util.ArrayList, 
                 java.util.Map, 
                 com.Googlecode.jsonplugin.JSONUtil, 
                 java.io.File" %> 
<%  
   out.clear();  //清空所有输出 
   request.setCharacterEncoding("UTF-8"); 
   response.setCharacterEncoding("UTF-8"); 
    
   @SuppressWarnings("all") 
   Map<String,Object> map =  
   (Map<String, Object>)JSONUtil.deserialize(request.getParameter("data")); //将提交的数据反序列化 
    
   @SuppressWarnings("all") 
   String objectId = (String)((Map<String, Object>)map.get("node")).get("objectId"); 
    
   File file = new File(objectId);  //要展开的文件 
   File[] children = file.listFiles();  //所有的子文件 
    
   List<Map<String, Object>> result = new ArrayList<Map<String, Object>>(); //查询结果 
    
   for(int i = 0; children != null && i < children.length; i++){  //先列出文件夹 
     if(children.isFile()) continue;  //如果是文件,跳过 
     Map<String,Object> entry = new HashMap<String, Object>();  
     entry.put("title",children.getName()); //存储title属性 
     entry.put("isFolder",children.isDirectory()); //存储isFolder属性 
      
     entry.put("id",children.getName());  //存储id属性 
     entry.put("objectId",children.getAbsolutePath());//存储objectId属性 
      
     result.add(entry);  //放到查询结果中 
   } 
    
   for(int i = 0; children != null && i < children.length; i++){  //后列出文件 
     if(children.isDirectory()) continue; //如果是文件夹,跳过 
      
     Map<String,Object> entry = new HashMap<String,Object>(); 
     entry.put("title",children.getName()); //存储title属性 
     entry.put("isFolder",children.isDirectory()); //存储isFolder属性 
      
     entry.put("id",children.getName());   //存数id属性 
     entry.put("objectId",children.getAbsolutePath()); //存储objectId属性 
      
     result.add(entry); //放到查询结果中 
   } 
   out.print(JSONUtil.serialize(result));  //将结果序列化,输出到客户端浏览器 
   System.out.println(JSONUtil.serialize(result));  //将结果序列化,输出到控制台 
%> 
运行效果如下: 
 
     
  |   
 
 
 
 |