Simple usage of jquery easyui tree asynchronous loading, jqueryeasyui
Jsp page:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
Background code:
Tree menu-display the file list by path
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; I <fs. length; I ++) {Map map = new HashMap (); map. put ("id", fs [I]. getAbsolutePath (); map. put ("text", fs [I]. getName (); if (fs [I]. isDirectory () {map. put ("type", "folder"); if (fs [I]. listFiles () = null | fs [I]. listFiles (). length = 0) {map. put ("iconCls", "icon-folder"); map. put ("state", "open");} else {map. put ("state", "closed") ;}} else {map. put ("type", "file"); map. put ("state", "open ");} List. add (map) ;}} else {Map map = new HashMap (); map. put ("id", file. getAbsolutePath (); map. put ("text", file. getName (); if (file. listFiles ()! = Null & file. listFiles (). length> 0) {// There is a sub-file 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 ;}}
Read File Content
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 (HttpSe RvletRequest 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 = n Ull; // read a row at a time until null is the end of the file while (tempString = reader. readLine ())! = Null) {// display the row number 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 ());}}
Page effect display: