Tree控件的小bug?!
不知道是否是Tree控件的bug,下面的代码是可以从数据获得数据并填充Tree(多层次的),但如果把mx:HTTPService 的resultFormat="xml" 属性去掉,界面上Tree仍然可以正常显示数据内容,但却无法获得Tree里结点Node的数据(包括label和data),无论是用AS还是数据binding都无效,不知道其它朋友又没有碰到过,希望有类似情况的同志别走我的弯路(我跟踪了2天还被搞得莫名其妙)。
下面是代码的内容(注意标记红色的代码段)
MXML文件内容:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml"
verticalGap="10"
backgroundColor="#FFFFFF"
pageTitle="手机"
initialize="initApp()">
<mx:Style source="main.css"/>
<mx:HTTPService id="categoryTreeService"
url="categorytree.jsp" resultFormat="xml"/>
<mx:Model id="categorytree">
{categoryTreeService.result}
</mx:Model>
<mx:Script>
<![CDATA[
function initApp() {
categoryTreeService.send();
}
]]>
</mx:Script>
<mx:Tree width="165" height="318" change="changeCategory(event.target.selectedItem.attributes)" dataProvider="{categorytree}"/>
</mx:Application>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml"
verticalGap="10"
backgroundColor="#FFFFFF"
pageTitle="手机"
initialize="initApp()">
<mx:Style source="main.css"/>
<mx:HTTPService id="categoryTreeService"
url="categorytree.jsp" resultFormat="xml"/>
<mx:Model id="categorytree">
{categoryTreeService.result}
</mx:Model>
<mx:Script>
<![CDATA[
function initApp() {
categoryTreeService.send();
}
]]>
</mx:Script>
<mx:Tree width="165" height="318" change="changeCategory(event.target.selectedItem.attributes)" dataProvider="{categorytree}"/>
</mx:Application>
categorytree.jsp文件内容:
<?xml version="1.0" encoding="utf-8"?>
<%@ page contentType="text/html;charset=utf-8"%>
<%@ page import="java.sql.*"%>
<phone>
<%!
String browseTree(Connection conn, String id) {
String temp = "";
String end = "";
try {
String sql = "select id, name from tblMobileCategory where categoryid=" + id;
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()){
temp += "<node label="" + rs.getString(2) + "" data="" + rs.getString(1) ;
end = browseTree(conn, rs.getString(1));
if (end.equals("")) {
temp += ""/> ";
} else {
temp += ""> ";
temp += end;
temp += "</node> ";
}
}
rs.close();
stmt.close();
} catch (Exception e) {
//
}
return temp;
}
%>
<%
String sql = "";
String url = "";
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
url = "jdbc:mysql://localhost/web?user=flex&password=flex&useUnicode=true&characterEncoding=gb2312";
Connection conn = DriverManager.getConnection(url);
out.println(browseTree(conn, "0"));
conn.close();
} catch (Exception e) {
out.println(e);
}
%>
</phone>
<%@ page contentType="text/html;charset=utf-8"%>
<%@ page import="java.sql.*"%>
<phone>
<%!
String browseTree(Connection conn, String id) {
String temp = "";
String end = "";
try {
String sql = "select id, name from tblMobileCategory where categoryid=" + id;
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()){
temp += "<node label="" + rs.getString(2) + "" data="" + rs.getString(1) ;
end = browseTree(conn, rs.getString(1));
if (end.equals("")) {
temp += ""/> ";
} else {
temp += ""> ";
temp += end;
temp += "</node> ";
}
}
rs.close();
stmt.close();
} catch (Exception e) {
//
}
return temp;
}
%>
<%
String sql = "";
String url = "";
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
url = "jdbc:mysql://localhost/web?user=flex&password=flex&useUnicode=true&characterEncoding=gb2312";
Connection conn = DriverManager.getConnection(url);
out.println(browseTree(conn, "0"));
conn.close();
} catch (Exception e) {
out.println(e);
}
%>
</phone>