sx:tree 标签生成树状的文件列表

来源:互联网 发布:搜索引擎数据库设计 编辑:程序博客网 时间:2024/04/30 13:29

</pre>参照 Java Web 开发实战1200例写的一个将HDFS文件以树状形式展示的文件列表</p><p>因为是为了复制和移动的功能写的,故只显示文件夹</p><p>点击确定会在文本框中显示选择文件的路径</p><pre name="code" class="java">package com.suiyue;import java.io.IOException;import java.util.ArrayList;import java.util.List;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.FileStatus;import org.apache.hadoop.fs.FileSystem;import org.apache.hadoop.fs.Path;public class HdfsList {private FileSystem fileSystem;private FileStatus fileStatus;private String hdfsFileName;public String getHdfsFileName() {return hdfsFileName;}public void setHdfsFileName(String hdfsFileName) {this.hdfsFileName = hdfsFileName;}private List list = new ArrayList();private String hdfsPath;public String getHdfsPath() {return hdfsPath;}public void setHdfsPath(String hdfsPath) {this.hdfsPath = hdfsPath;}public FileSystem getFileSystem() {return fileSystem;}public void setFileSystem(FileSystem fileSystem) {this.fileSystem = fileSystem;}public FileStatus getFileStatus() {return fileStatus;}public void setFileStatus(FileStatus fileStatus) {this.fileStatus = fileStatus;}public List getList() {return list;}public void setList(List list) {this.list = list;}public  static String HdfsPath() {return "hdfs://localhost:9001/user";}public HdfsList(String hdfsPath){this.hdfsPath = hdfsPath;Path path = new Path(hdfsPath);this.hdfsFileName = path.getName();Configuration conf = new Configuration();FileSystem fs;try {fs = FileSystem.get(conf);FileStatus[] files = fs.listStatus(path);for(int i=0; i<files.length;i++) {if(files[i].isDir()) {HdfsList hdfslist = new HdfsList(files[i].getPath().toString() + "/");list.add(hdfslist);} }} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"import = "com.suiyue.HdfsList"import = "org.apache.hadoop.fs.*"%><%@ taglib  uri="/struts-tags"  prefix="s"%><%@ taglib prefix="sx" uri="/struts-dojo-tags"%>  <%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--><s:head /><sx:head parseContent="true"/><script type="text/javascript">        function treeNodeSelected(arg) {              var input = document.getElementById("filepath");        var s = dojo.widget.byId('root').selector;        //alert(s.selectedNode.widgetId);        input.value = s.selectedNode.widgetId;            //alert(arg.source.widgetId + ' selected');            //return arg.source.widgetId;          }        dojo.addOnLoad(function() {                            var s = dojo.widget.byId('root').selector;                            dojo.event.connect(s, 'select', 'treeNodeSelected');        });        </script>  </head>   <body>     文件列表:   <%    request.setAttribute("file", new HdfsList(HdfsList.HdfsPath()));     %>        <s:set name="k" value="0"></s:set>     <sx:tree id="root" rootNode="%{#request.file}" nodeTitleProperty="hdfsFileName"  nodeIdProperty="hdfsPath" childCollectionProperty="list"  >     </sx:tree>     <button name="submit" onclick="treeNodeSelected()">确定</button>     <input type="text"  id="filepath" value="">  </body></html>

0 0
原创粉丝点击