⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 permissiontree.java

📁 oa办公系统
💻 JAVA
字号:
package cn.jx.ecjtu.oa.servlets.users;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.jx.ecjtu.oa.ps.pojo.PermissionNode;
import cn.jx.ecjtu.oa.services.ServiceFactory;
import cn.jx.ecjtu.oa.services.SysUserService;

public class PermissionTree extends HttpServlet {

	/**
	 * Destruction of the servlet. <br>
	 */
	int tid=0;
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request,response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		File file = new File(getServletContext().getRealPath(""));
		int parentId = -1;
		List <PermissionNode>filelist = new ArrayList<PermissionNode>();
		getTree(file,parentId,filelist,"");
		for (PermissionNode permissionNode : filelist) {
			System.out.println("id:"+permissionNode.getId()+"  "+"pid:"+permissionNode.getParentId()+" name:"+permissionNode.getName());
		}
		if(filelist.size()!=0){
			request.setAttribute("filelist", filelist);
			//request.setAttribute("resources", resources);
			request.getRequestDispatcher("addPermission.jsp").forward(request, response);
		}else{
			request.setAttribute("message", "@没有目录@");
			getServletContext().getRequestDispatcher("/error.jsp").forward(request, response);
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occure
	 */
	private SysUserService service;//使用到的SysUserService的对象

	public void init() throws ServletException {
		service = (SysUserService) ServiceFactory.getService(SysUserService.class);
	}

	private int getTree(File file,int parentId,List<PermissionNode>filelist,String resource){
		String filename = file.getName();
		PermissionNode pnode = new PermissionNode();
		if(filename.startsWith("META-INF")||filename.startsWith("WEB-INF")||filename.startsWith("common")){
			return -1;
		}else{
			if(parentId==-1){
				resource="";
			}else{
				resource+="/"+filename;
			}
			pnode.setId(tid);
			pnode.setName(filename);
			pnode.setParentId(parentId);
			pnode.setResource(resource);
			filelist.add(pnode);
			String[] s=file.list();
			File[] f=file.listFiles();
			parentId=tid;
			for(int i=0;i<s.length;i++){
				
				if(f[i].isDirectory()){
					tid++;
					getTree(f[i],parentId,filelist,resource);
					System.out.println("我的id"+tid);
				}
				
			}
			
			return tid;
		}
		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -