jquery easyui tree异步加载的简单用法

页面导航:首页 > 网络编程 > JavaScript > jquery easyui tree异步加载的简单用法

jquery easyui tree异步加载的简单用法

来源: 作者: 时间:2016-02-03 09:20 【

jsp页面: My JSP 39;index jsp 39; starting page function ok(){$( file_tree ) tree

页面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<>
  
    
    
    My JSP 'index.jsp' starting page
	
	
	    
	
	
	
	
	
    
    
    <script type="text/javascript" src="jquery/easyui/jquery.min.js"></script>
    <script type="text/javascript" src="jquery/easyui/jquery.easyui.min.js"></script>
  
  <script type="text/javascript">
  		function ok(){
			$("#file_tree").tree({
				url:'TestTreeServlet',
				lines:true,
				onClick:function(node){
					if(node.type=="file"){
 					var ps={"id":node.id};
 					$.post("ReadFileServlet",ps,function(data){
 						alert(data);
 						$("#info").empty().append(data);
 					});
					}
					
				}
			});
  		}
  	
  </script>
  
  
    This is my JSP page. 

    后台代码:

    树形菜单---根据路径显示文件列表

    package servlet;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import net.sf.json.JSONArray;
    
    //import com.google.gson.JsonArray;
    
    import util.TreeJson;
    
    public class TestTreeServlet extends HttpServlet {
    	@Override
    	protected void service(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    		request.setCharacterEncoding("UTF-8");
    		
    		response.setCharacterEncoding("utf-8");
    		response.setContentType("text/html");
    		
    		String id=request.getParameter("id");
    		if(id==null || "".equals(id)){
    			id="E:/test/";
    		}
    		List list=readPath(id);
    		
    		JSONArray json=JSONArray.fromObject(list);
    		response.getWriter().write(json.toString());
    	}
    	
    	public static List readPath(String path){
    		List list=new ArrayList();
    		File file = new File(path);
    		if(file.exists()){
    			File[] fs=file.listFiles();
    			if(fs!=null && fs.length>0){
    				for(int i=0;i0){//还有子文件
    					map.put("state", "closed");
    				}else{
    					map.put("state", "open");
    				}
    				map.put("iconCls", file.isFile()==true?"icon-file":"icon-folder");
    				map.put("type", file.isFile()==true?"file":"folder");
    				
    				list.add(map);
    			}
    		}
    		System.out.println(list.toString());
    		return list;
    	} 
    }
    

    读取文件内容

    package servlet;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.InputStream;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class ReadFileServlet extends HttpServlet {
    	@Override
    	protected void service(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    		response.setCharacterEncoding("utf-8");
    		response.setContentType("text/html");
    		
    		String fileName=request.getParameter("id");
    		File file = new File(fileName);
            BufferedReader reader = null;
            StringBuffer text=new StringBuffer("");
            try {
                reader = new BufferedReader(new FileReader(file));
                int line = 1;
                String tempString=null;
                // 一次读入一行,直到读入null为文件结束
                while ((tempString = reader.readLine()) != null) {
                    // 显示行号
                	System.out.println(line + ": " + tempString);
                	text.append(tempString+"\n");
                    line++;
                }
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e1) {
                    }
                }
            }
            response.getWriter().write(text.toString());
    	}
    }
    




    页面效果展示:













    Tags:

    文章评论

    最 近 更 新
    热 点 排 行
    Js与CSS工具
    代码转换工具
    
    <