原文地址:http://www.blogjava.net/rickhunter/articles/59742.html
1、jsp页面
<%
@ page language
=
"
java
"
contentType
=
"
text/html; charset=GB2312
"
import
=
"
java.util.*,com.wehave.oa.cecontract.model.TbJyhtflb
"
%>
<
html
>
<
head
>
<
title
>
Insert title here
</
title
>
<
link rel
=
"
stylesheet
"
href
=
"
../css/tree.css
"
>
<
script type
=
"
text/javascript
"
src
=
"
../js/tree_htfl.js
"
></
script
>
<
script languge
=
"
javascript
"
>
![](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
function ShowDetail(ID,NAME,FLAG)
{
window.parent.right.location ="getContract.go?method=doGetContract&folderID=" +ID + "&&folderName="+NAME+"&&flag="+FLAG;
}
</
script
>
</
head
>
<
body bgcolor
=
"
#F6F9FF
"
>
<
tbody
>
<
table cellpadding
=
"
0
"
cellspacing
=
"
0
"
width
=
"
300
"
>
<
div id
=
"
load
"
style
=
"
display:none
"
>
<
img src
=
"
../images/tree_loading.gif
"
>
Loading data..
</
div
>
<
ul
class
=
"
tree
"
>
<%
List treeList
=
(List)request.getAttribute(
"
treefolder
"
);
Iterator it
=
treeList.iterator();
![](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
while
(it.hasNext())
{
out.println(it.next().toString());
}
%>
</
table
>
</
tbody
>
</
body
>
</
html
>
2、tree_htfl.js 代码
function showHide( id )
![](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
{
var el= document.getElementById( id );
var bExpand = true;
var images = el.getElementsByTagName("IMG");
if (images[0].src.indexOf("tree_minus.gif")!=-1)
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
{
bExpand = false;
images[0].src="../images/tree_plus.gif";
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
}else
{
images[0].src="../images/tree_minus.gif";
}
var subs=el.lastChild;
if(bExpand)
subs.style.display="block";
else
subs.style.display="none";
}
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
function getSubTree( id ,submitURL)
![](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
{
var submitURL=submitURL
postXmlHttp( submitURL, 'parseSubTree("'+id+'")' ,'load("'+id+'")');
}
function parseSubTree(id)
![](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
{
var el= document.getElementById( id );
var ulElmt= document.createElement("UL");
ulElmt.innerHTML=_xmlHttpRequestObj.responseText;
el.appendChild(ulElmt);
var images = el.getElementsByTagName("IMG");
images[0].setAttribute("src", "../images/tree_minus.gif");
images[0].setAttribute("onclick", new Function("showHide('"+id+"')"));
var aTag = el.getElementsByTagName("A");
aTag[0].setAttribute("onclick", new Function("showHide('"+id+"')"));
var loadDiv= document.getElementById( "load" );
loadDiv.style.display="none";
}
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
function load(id)
![](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
{
var loadDiv= document.getElementById( "load" );
loadDiv.style.display="block";
}
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
var _postXmlHttpProcessPostChangeCallBack;
var _xmlHttpRequestObj;
var _loadingFunction;
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
function postXmlHttp( submitUrl, callbackFunc ,loadFunc)
![](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
{
_postXmlHttpProcessPostChangeCallBack = callbackFunc;
_loadingFunction = loadFunc;
if(window.createRequest)
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
{
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
try
{
_xmlHttpRequestObj=window.createRequest();
_xmlHttpRequestObj.open('POST',submitUrl,true);
_xmlHttpRequestObj.onreadystatechange=postXmlHttpProcessPostChange;
_xmlHttpRequestObj.send();
}
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
catch(ee)
{}
}
else if(window.XMLHttpRequest)
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
{
_xmlHttpRequestObj=new XMLHttpRequest();
_xmlHttpRequestObj.overrideMimeType('text/xml');
_xmlHttpRequestObj.open('POST',submitUrl,true);
_xmlHttpRequestObj.onreadystatechange=postXmlHttpProcessPostChange;
_xmlHttpRequestObj.send("");
}
else if(window.ActiveXObject)
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
{
_xmlHttpRequestObj=new ActiveXObject("Microsoft.XMLHTTP");
_xmlHttpRequestObj.open('POST',submitUrl,true);
_xmlHttpRequestObj.onreadystatechange=postXmlHttpProcessPostChange;
_xmlHttpRequestObj.send();
}
}
;
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
function postXmlHttpProcessPostChange( )
![](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
{
if( _xmlHttpRequestObj.readyState==4)
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
{
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
if(_xmlHttpRequestObj.status==200)
{
setTimeout( _postXmlHttpProcessPostChangeCallBack, 2 );
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
}else
{
alert(_xmlHttpRequestObj.status);
}
}
if ( _xmlHttpRequestObj.readyState==1 )
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
{
setTimeout( _loadingFunction, 2 );
}
}
3、action代码
/** */
/**
* 展开第一层目录
*/
public
ActionForward doGetFolderList(
ActionMapping mapping,
ActionForm form,
HttpServletRequest req,
![](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
HttpServletResponse res)
{
List list = treeCatalogService.getChildren("0");
List TreeFolder=new ArrayList();
Iterator it=list.iterator();
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
while(it.hasNext())
{
TbJyhtflb htfl=(TbJyhtflb)it.next();
String s=treeCatalogService.renderTreeViewAjax(htfl);
TreeFolder.add(s);
}
req.setAttribute("treefolder",TreeFolder);
return mapping.findForward("foldertree");
}
![](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
/** */
/**
* 展开下级目录
*/
public
ActionForward doGetSubFolderList(
ActionMapping mapping,
ActionForm form,
HttpServletRequest req,
![](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
HttpServletResponse res)
{
String parentID = req.getParameter("parentID"); //获得id的值
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
if (parentID!=null&&!parentID.equals(""))
{ //如果不为null和空
res.setContentType("text/html;charset=GB2312");
List list = treeCatalogService.getChildren(parentID);
Iterator it=list.iterator();
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
try
{
PrintWriter out= res.getWriter();
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
while(it.hasNext())
{
TbJyhtflb htfl=(TbJyhtflb)it.next();
out.println(treeCatalogService.renderTreeViewAjax(htfl));
}
out.close();
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
}catch(Exception e)
{
e.printStackTrace();
}
}
return null;
}
4、service层代码
/** */
/**
* 函数说明:展开目录
* 参数说明: 目录对象
* 返回值:展开目录的HTML代码
*/
![](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
public
String renderTreeViewAjax(TbJyhtflb htfl)
{
StringBuffer content = new StringBuffer();
String ID=htfl.getTbJyhtflbZlId();
String NAME=htfl.getTbJyhtflbMc();
String FLAG=htfl.getTbJyhtflbLb();
content.append("<li id='"+ID+"'>");
if (treeCatalogDAO.canExpand(ID))
content.append("<img src=../images/tree_plus.gif onClick=/"getSubTree('"+ID+"')/">");
else
content.append("<img src=../images/tree_blank.gif>");
content.append("<img src=../images/tree_folder.gif><a href=/"javascript:ShowDetail('"+ID+"','"+NAME+"','"+FLAG+"')/"");
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
if (treeCatalogDAO.canExpand(ID))
{
String submitURL="getFolderList.go?method=doGetSubFolderList&parentID="+ID;
content.append(" onClick=/"getSubTree('"+ID+"',submitURL)/"");
}
content.append(">"+NAME+"</a>");
content.append("</li>");
return content.toString();
}
5、tree.css代码:
p
{
font-family:arial;
}
![](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
a
{
color:#000;
font-family:arial;
font-size:0.8em;
}
![](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
.tree
{
margin:0px;
padding:0px;
}
![](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
.tree ul
{ /**//*子结点*/
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
margin-left:20px; /**//* Left spacing */
padding-left:0px;
}
![](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
.tree li
{ /**//* 结点 */
list-style-type:none;
vertical-align:middle;
}
![](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
.tree li a
{ /**//* 结点连接 */
color:#000;
text-decoration:none;
font-family:arial;
font-size:0.8em;
padding-left:2px;
}