问题描述
- EXTJS动态加载树,后台JSP,为什么报错NO DATA FOUND呢?代码在下,多谢了!
-
代码:LoadTree.htmltest3
<script type="text/javascript"> var tree = new Ext.tree.TreePanel({ loader : new Ext.tree.TreeLoader({ dataUrl : 'tree.jsp?Did=00001' }), border : false, root : new Ext.tree.AsyncTreeNode({ text : '集团', id : '00001' }) }); tree.on("beforeload", function(node) { tree.loader.dataUrl = 'tree.jsp?Did=' + node.id; }); new Ext.Window({ //将树放在窗口中 height : 200, width : 200, layout : 'fit', border : false, title : 'First Tree', items : tree }).show(); }); </script>
代码:tree.jsp
<%@ page language="java" pageEncoding="GBK"%>
/jsp:useBean
<%
String PID = "";
if (request.getParameter("Did") != null) {
PID = request.getParameter("Did").toString();
JSONTree.setPID(PID);
} else {
JSONTree.setPID("00001");
}
%>
<%=JSONTree.getJSONString()%>代码:JSONTree.java
package tree;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;public class JSONTree {
private String PID;public String getJSONString() { Connection conn = null; Statement st = null; ResultSet rs = null; StringBuffer dataJson = new StringBuffer(); String SQLString = "SELECT * FROM treeTabel WHERE sjgsdm=" + this.PID + " and yxbz = 'Y' ORDER BY gsdm"; String Sql="select sjgsdm from treeTabel where yxbz ='Y'group by sjgsdm order by sjgsdm"; //String Sql="select sjgsdm from treeTabel "; try { /** * 连接数据库 */ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); conn = DriverManager.getConnection( "Jdbc:Odbc:tree", "sa", "123456"); st = conn.createStatement(); rs = st.executeQuery(Sql); System.out.println(this.PID); StringBuffer parentIDBuffer = new StringBuffer(); parentIDBuffer.append("|"); //System.out.println(parentIDBuffer); while (rs.next()) { /** * 因数据库里有一个节点的父节点为空,所以这里要把空节点从父节点集合里去除 */ if (rs.getString("sjgsdm").equals(" ")) { continue; } parentIDBuffer.append(rs.getString("sjgsdm")); parentIDBuffer.append("|"); System.out.println(parentIDBuffer); } /** * 打印测试语句,查看所获取到的所有父节点有哪些 */ System.out.println(parentIDBuffer); String s = parentIDBuffer.toString(); rs = st.executeQuery(SQLString); dataJson.append("["); while (rs.next()) { // 判断是否为父节点 if (s.indexOf("|" + rs.getString("gsdm") + "|") >= 0) { dataJson.append("{id:'" + rs.getString("gsdm") + "',text:'" + rs.getString("shortname") + "',leaf:false}"); } else { dataJson.append("{id:'" + rs.getString("gsdm") + "',text:'" + rs.getString("shortname") + "',leaf:true}"); } /** * * 判断是否为最后一条记录,因Json里不同的{}之间要有一个','这个符号,但在最后一条记录时不需要加此符号,故有此判断 */ if (rs.isLast()) { System.out.println(rs.getString("shortname")); continue; } dataJson.append(","); } dataJson.append("]"); /** * 打印测试语句,查看json对象的字符串是什么样,是否符合要求 */ System.out.println(dataJson); } catch (Exception e) { e.printStackTrace(); } finally { try { rs.close(); st.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } } // 返回JSON数据 return dataJson.toString(); } public String getPID() { return PID; } public void setPID(String pid) { PID = pid; }
}
数据表:sjgsdm yxbz gsdm shortname 都是char型 其中sjgsdm为父节点 yxbz为子节点
多谢大家了!我是新学EXTJS的 需要大家的帮助!
时间: 2024-12-29 12:57:39