其间,遇到了不少问题:
先来说一下
1.对文件夹的遍历,将信息写入xml文件中
路径的问题(相对路径与绝对路径);
编码的问题(出现乱码,注意记事本(xml文件)默认的编码,将编码设成GBK可解决)
结合dtree实现树状目录,其实xml中只有一层。
2.前台出现的问题,调试了很长时间。
因为servlet的跳转问题,会导致jsp页面中导入的资源的路径出现问题,找不到。
所以以后前台的编写最好使用绝对路径。
1.servlet
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("utf-8");
String pid=request.getParameter("pid");
ProjectAction projectaction=new ProjectAction();
Ch_projects project=(Ch_projects)projectaction.getProject(pid);
String pname=project.getPname();
String weburl="http://localhost:8080/";//工程的ip地址
String webpro="codehome3.0/upload";
String temps=request.getSession().getServletContext().getRealPath("/");//工程的物理路径
String outFile=temps+"projectxml\\a"+pid+".xml";//项目的xml文件的输出路径
//System.out.println("former:"+outFile+"\n");
String url=temps+project.getPurl();//项目的物理路径
//System.out.println("former:url:"+url);
Create_Xml creatxml=new Create_Xml(url,pname,outFile,weburl,webpro);
try {
creatxml.writeXMLFile();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String xmlurl="/codehome3.0/projectxml/a"+pid+".xml";
//System.out.print(xmlurl);
request.setAttribute("xmlurl", xmlurl);
request.getRequestDispatcher("/projects/insight.jsp").forward(request, response);
}
2.写xml文件。
[code="java"]
package com.star.codehome.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.crimson.tree.XmlDocument;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class Create_Xml {
private Document doc = null;
private int id = 0;
private String sourceurl="";
private String projectName="";
private String outFile="";
private String weburl="";
private String webpro="";
/*
*构造方法
*/
public Create_Xml(String sourceurl,String projectName,
String outFile,String weburl,String webpro) {
super();
this.sourceurl = sourceurl;
this.projectName = projectName;
this.outFile=outFile;
this.weburl=weburl;
System.out.println("weburl:"+this.weburl);
System.out.println("webpro:"+webpro);
this.webpro=webpro.replace('/','\\' );
}
public void writeXMLFile() throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
try {
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException pce) {
System.err.println(pce);
System.exit(1);
}
doc = db.newDocument();
Element root=doc.createElement("a");
id=0;
root.setAttribute("id", "0");
root.setAttribute("pid", "-1");
root.setAttribute("na", projectName);
doc.appendChild(root);
seacherFile(sourceurl,root,0);
shengcheng(outFile, doc);
}
private void shengcheng(String outFile,Document doc) throws IOException
{
FileOutputStream outStream = new FileOutputStream(outFile);
OutputStreamWriter outWriter = new OutputStreamWriter(outStream);
((XmlDocument) doc).write(outWriter, "GBK");
outWriter.close();
outStream.close();
}
private void seacherFile(String s,Element father,int p_id) {
// TODO Auto-generated method stub
File dir = new File(s);
if (dir.exists())
{
if (dir.isFile())
{
String temp=dir.toString();
int a=temp.indexOf(webpro);
String b=temp.substring(a);
b=weburl+b;
System.out.println("formal:"+b);
String ss="a";
Element files=doc.createElement(ss);
id++;
files.setAttribute("id",Integer.toString(id));
files.setAttribute("pid", Integer.toString(p_id));
files.setAttribute("na", dir.getName());
files.setAttribute("url", dir.toString());
System.out.println("文件名:"+dir.getName());
System.out.println("formal:"+b);
//System.out.println( dir.toString());
father.appendChild(files);
//System.out.println(s + "is an file!");
}
else
{
File[] dirList = dir.listFiles();
for (int i = 0; i < dirList.length; i++) {
if (dirList[i].isFile()) {
String temp=dirList[i].toString();
int a=temp.indexOf(webpro);
String b=temp.substring(a);
b=weburl+b;
String ss="a";
id++;
Element files=doc.createElement(ss);
files.setAttribute("id",Integer.toString(id));
files.setAttribute("pid", Integer.toString(p_id));
files.setAttribute("na", dirList[i].getName());
files.setAttribute("url", b);
System.out.println("latter:"+b);
father.appendChild(files);
System.out.println(dirList[i] + "is an file!");
}
else
{
id++;
String ss="a";
Element files=doc.createElement(ss);
files.setAttribute("id",Integer.toString(id));
files.setAttribute("pid", Integer.toString(p_id));
files.setAttribute("na", dirList[i].getName());
father.appendChild(files);
seacherFile(dirList[i].toString(),father,id);
}
}
}
} else
{
//System.out.println(s + "is not existed!");
}
}
}
[/code]
2.先来介绍一下. dtree 的用法.(我引用了以前我收集的一篇文章.还比较详细,出处不记得啦).文章下面会附带dtree用法的例子.
Dtree目录树的总结
一:函数
1:页面中
tree.add(id,pid,name,url,title,target,icon,iconOpen,open);
参数说明:
id :节点自身的id
pid :节点的父节点的id
name :节点显示在页面上的名称
url :节点的链接地址
title :鼠标放在节点上所出现的提示信息
target :节点链接所打开的目标frame(如框架目标mainFrame,_blank,_self 类)
icon :节点关闭时的显示图片的路径
iconOpen:节点打开时的显示图片的路径
open :布尔型,节点是否打开(默认为false)
注:open项:顶级节点一般采用true,即pid是-1的节点
2:dtree.js文件中
约87-113行是一些默认图片的路径,注意要指对。
二:页面中的书写
1:默认值的书写规则(从左至右,依次省略)
即 tree.add(id,pid,name,url);后面5个参数可以省略
2:有间隔时的默认值(如存在第6个参数,但第5个参数想用默认值)
即 tree.add(id,pid,name,url,"",target);必须这样写
3:样式表
(1):可以将dtree.css中的样式附加到你的应用中的主css中,如a.css
(2):也可以同时引用dtree.css与a.css两个文件,但前提条件是两个css文件中不能有重复的样式
4:页面代码书写的位置是:一般写在表格的td之中
说明:这是静态的代码,动态的可用循环加入。其他 tree.add(id,pid,name,url,"","","","",true);
不罗嗦啦..上面的只是让你大概了解一下.dtree怎么用.
dtree+JQuery动态生成树.思路其实很简单... 首先把树的节点信息存储到数据库,然后在servlet或jsp中获取数据库表中的数据,把这些信息写成在xml文件中.然后界面jsp页面通过JQuery实现对改 servlet的请求.并且回调方法中接受xml数据对象.并且遍历xml文件,取得xml文件中的节点的属性或文本数据.再循环的对dtree添加节点.。
5. 将dtree.js 和dtree.css,jquery.js, img文件夹.放在WebRoot下面.(工程的根目录)
6. 编写我们的tree.jsp页面.
Java代码
<%@ page language="java" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>dtree ajax</title>
<script type="text/javascript" src="dtree.js"></script>
<script type="text/javascript" src="jquery.js"></script>//注意这里的路径,如果使用servlet则跳转的
//路径会有问题
<link rel="stylesheet" href="dtree.css" type="text/css"></link>
<script type="text/javascript">
tree = new dTree('tree');//创建一个对象.
$.ajax({
<A href="'NodesPrint'">url:'NodesPrint'</A>,
type:'post', //数据发送方式
dataType:'xml', //接受数据格式
error:function(json){
alert( "not lived!");
},
async: false ,//同步方式
success: function(xml){
$(xml).find("node").each(function(){
var nodeName=$(this).attr("na");
var parentId=$(this).attr("pid");
var nodeUrl=$(this).attr("url");
var nodeId=$(this).attr("id");
//var nodeTarget="right";
tree.add(nodeId,parentId,nodeName,nodeUrl,"","","","",false);
});
}
});
document.write(tree);
</script>
</head>
<body>
</body>
</html>
jquery xml dtree
最新推荐文章于 2017-10-21 10:54:05 发布