xloadtree的Demo可以從http://webfx.eae.net/dhtml/xloadtree/xloadtree.html下載,並有usage,api...
將xloadtree的Demo直接放置到Tomcat的Webapps目錄下,不能正確顯示,總顯示“Error Load tree.xml”,一步步對問題進行了跟蹤,找到了問題的所在。問題在於XmlHttp使用了responseXML,返回的消息必須是xml,更準確的說是必須能夠被識別爲xml,要做到這點其實很簡單,只要設置ContentType="test/xml"。
我的做法是對xml用jsp進行了包裝:
tree.xml <?xml version="1.0" encoding="UTF-8"?>
<tree>
<tree text="Load "tree1.xml"" src="tree1.xml" />
<tree text="Loaded Item 1" action="http://webfx.eae.net" />
<tree text="Loaded Item 2">
<tree text="Loaded Item 2.1" action="javascript:alert(2.1)" />
</tree>
<tree text="Loaded Item 3 (with target)" action="http://www.google.com" target="_new" />
<tree text="Load "tree1.xml"" src="tree1.xml" />
</tree>
tree.jsp <%
response.setContentType("text/xml");
out.println("<?xml version=/"1.0/" encoding=/"UTF-8/"?>/n" +
"/n" +
"<tree>/n" +
"/t<tree text=/"Load "tree1.xml"/" src=/"tree1.jsp/" />/n" +
"/t<tree text=/"Loaded Item 1/" action=/"http://webfx.eae.net/" />/n" +
"/t<tree text=/"Loaded Item 2/">/n" +
"/t/t<tree text=/"Loaded Item 2.1/" action=/"javascript:alert(2.1)/" />/n" +
"/t</tree>/n" +
"/t<tree text=/"Loaded Item 3 (with target)/" action=/"http://www.google.com/" target=/"_new/" />/n" +
"/t<tree text=/"Load "tree1.xml"/" src=/"tree1.jsp/" />/n" +
"</tree>");
%>
tree1.xml <?xml version="1.0"?>
<tree>
<tree text="Loads tree2.xml" src="tree2.xml"/>
<tree text="Loads NOT_AVAILABLE.xml" src="NOT_AVAILABLE.xml"
icon="images/xp/folder.png"/>
<tree text="Loads emptytree.xml" src="emptytree.xml"
icon="images/xp/folder.png"/>
<tree text="Loaded Item 3" action="javascript:alert(3)" />
<tree text="Loaded Item 4" action="javascript:alert(4)">
<tree text="Loaded Item 4.1" action="javascript:alert(2.1)" />
<tree text="Loaded Item 4.2">
<tree text="Loaded Item 4.2.1">
<tree text="Loaded Item 4.2.1.2"/>
<tree text="Loaded Item 4.2.1.3"/>
</tree>
<tree text="Loaded Item 4.2.2"/>
</tree>
</tree>
<tree text="WebFX Home" action="http://webfx.eae.net"
icon="http://webfx.eae.net/images/favicon.gif"/>
</tree>
tree1.jsp <%
response.setContentType("text/xml");
out.println("<?xml version=/"1.0/"?>/n" +
"/n" +
"<tree>/n" +
"/t<tree text=/"Loads tree2.xml/" src=/"tree2.jsp/"/>/n" +
"/t<tree text=/"Loads NOT_AVAILABLE.xml/" src=/"NOT_AVAILABLE.xml/"/n" +
"/t/ticon=/"images/xp/folder.png/"/>/n" +
"/t<tree text=/"Loads emptytree.xml/" src=/"emptytree.jsp/"/n" +
"/t/ticon=/"images/xp/folder.png/"/>/n" +
"/t<tree text=/"Loaded Item 3/" action=/"javascript:alert(3)/" />/n" +
"/t<tree text=/"Loaded Item 4/" action=/"javascript:alert(4)/">/n" +
"/t/t<tree text=/"Loaded Item 4.1/" action=/"javascript:alert(2.1)/" />/n" +
"/t/t<tree text=/"Loaded Item 4.2/">/n" +
"/t/t/t<tree text=/"Loaded Item 4.2.1/">/n" +
"/t/t/t/t<tree text=/"Loaded Item 4.2.1.2/"/>/n" +
"/t/t/t/t<tree text=/"Loaded Item 4.2.1.3/"/>/n" +
"/t/t/t</tree>/n" +
"/t/t/t<tree text=/"Loaded Item 4.2.2/"/>/n" +
"/t/t</tree>/n" +
"/t</tree>/n" +
"/t<tree text=/"WebFX Home/" action=/"http://webfx.eae.net/"/n" +
"/t/ticon=/"/n">http://webfx.eae.net/images/favicon.gif/"/>/n" +
"</tree>");
%>
tree2.xml <?xml version="1.0"?>
<tree>
<tree text="Loaded File 2 Item 1" action="javascript:alert(1)" />
<tree text="Loaded File 2 Item 2" action="javascript:alert(2)">
<tree text="Loaded File 2 Item 2.1" action="javascript:alert(2.1)" />
<tree text="Loaded File 2 Item 2.2 (LOOP)" src="tree1.xml" action="javascript:alert(2.1)" />
</tree>
<tree text="Loaded File 2 Item 3" action="javascript:alert(3)" />
</tree>
tree2.jsp <%
response.setContentType("text/xml");
out.println("<?xml version=/"1.0/"?>/n" +
"/n" +
"<tree>/n" +
"/t<tree text=/"Loaded File 2 Item 1/" action=/"javascript:alert(1)/" />/n" +
"/t<tree text=/"Loaded File 2 Item 2/" action=/"javascript:alert(2)/">/n" +
"/t/t<tree text=/"Loaded File 2 Item 2.1/" action=/"javascript:alert(2.1)/" />/n" +
"/t/t<tree text=/"Loaded File 2 Item 2.2 (LOOP)/" src=/"tree1.jsp/" action=/"javascript:alert(2.1)/" />/n" +
"/t</tree>/n" +
"/t<tree text=/"Loaded File 2 Item 3/" action=/"javascript:alert(3)/" />/n" +
"</tree>");
%>
emptytree.xml <?xml version="1.0"?>
<tree/>
emptytree.jsp <%
response.setContentType("text/xml");
out.println("<?xml version=/"1.0/"?>/n" +
"<tree/>");
%>
這樣就能夠正確的顯示樹圖了,這種方法比較適合動態的通過程序獲取樹圖。
還有種比較徹底的辦法,更改xloadtree關於xmlhttp的部分。