jquery easyui tree非同步載入的簡單用法

來源:互聯網
上載者:User

jquery easyui tree非同步載入的簡單用法

jsp頁面:

<%@ 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 {@Overrideprotected 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 {@Overrideprotected 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());}}




    頁面效果展示:













    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.