首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

如果有牛人,(虽然还不是小弟我)

2013-12-17 
如果有牛人,,,(虽然还不是我)最近好几周都在搞一个动态菜单,利用了dtree控件,感觉问题出在$Ajax上边,求大

如果有牛人,,,(虽然还不是我)
最近好几周都在搞一个动态菜单,利用了dtree控件,感觉问题出在$Ajax上边,求大神帮忙看看,很苦恼,搞不出来也很无语,不懂dtree的大神也可以帮我看看$Ajax这块是不是出了问题,个人感觉dtree这块不可能有错误
以下我详细列出我各个文件内容,劳烦各位


文件一
node类:

package com.dao;
public class Node {
private String nodeId;  
    private String parentId;
    private String nodeName;
    
    public String getNodeId() {  
        return nodeId;  
    }  
 
    public void setNodeId(String nodeId) {  
        this.nodeId = nodeId;  
    }  
    
    public String getParentId() {  
        return parentId;  
    }  
 
    public void setParentId(String parentId) {  
        this.parentId = parentId;  
    }  
    
    public String getNodeName() {  
        return nodeName;  
    }  
 
    public void setNodeName(String nodeName) {  
        this.nodeName = nodeName;  
    }  
}
------------------------------------------------------
文件二
DaoTest类:

package com.dao;
import java.sql.*;  
import java.util.*;

import com.dao.Node;
public class DaoTest {
private Connection conn = null;
public void initConnection() throws Exception {  
Class.forName("com.mysql.jdbc.Driver");  
        conn = DriverManager.getConnection("jdbc:mysql://localhost/tree","root","mysqladmin");  
}
public  ArrayList<Node> getNodeInfo() throws Exception{  
ArrayList<Node> nodes = new ArrayList<Node>();
initConnection();
String sql = "select id,pid,name from t_tree"; 
Statement stat=conn.createStatement();
ResultSet rs = stat.executeQuery(sql);
while (rs.next()){  
            Node node = new Node();  
            //node.setHrefAddress(rs.getString("hrefAddress"));  
            node.setNodeId(rs.getString("id"));  
            node.setParentId(rs.getString("pid"));  
            node.setNodeName(rs.getString("name"));  
            nodes.add(node);  
        }  
closeConnection();
        
        return nodes;
    }
public void closeConnection() throws Exception{
    conn.close();
    }
}
---------------------------------------------------------
文件三(servlet,得到数据库信息,并以XML形式输出)

package com.handler;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.DaoTest;  
import com.dao.Node; 

public class NodesPrint extends HttpServlet {

/**
 * The doGet method of the servlet. <br>
 *
 * This method is called when a form has its tag value method equals to get.
 * 
 * @param request the request send by the client to the server
 * @param response the response send by the server to the client
 * @throws ServletException if an error occurred
 * @throws IOException if an error occurred
 */
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
    doPost(request, response);
}

/**
 * The doPost method of the servlet. <br>


 *
 * This method is called when a form has its tag value method equals to post.
 * 
 * @param request the request send by the client to the server
 * @param response the response send by the server to the client
 * @throws ServletException if an error occurred
 * @throws IOException if an error occurred
 */
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

request.setCharacterEncoding("utf-8");  
        response.setContentType("text/xml;charset=utf-8");  
        PrintWriter out = response.getWriter();  
        DaoTest test = new DaoTest();  
        try{
        ArrayList<Node> list=test.getNodeInfo();
            if(list!=null&&list.size()>0){  
             out.println("<?xml version="1.0" encoding="UTF-8"?>");  
             out.println("<nodes>");  
             for(int i=0;i<list.size();i++){  
                 Node node = list.get(i);  
                 out.println("<node nodeId='"+node.getNodeId()+"' parentId='"+node.getParentId()+"'>"+node.getNodeName()+"</node>");  
             }  
             out.println("</nodes>");  
           }
           }catch(Exception e){} 
}
}

-----------------------------------------------------------------------------------------------------------------------------------
JSP调用Dtree控件,利用$ajax方法实现菜单--tree.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
      
    <base href="<%=basePath%>">
    <!--
    <title>My JSP 'tree.jsp' starting page</title>
    -->
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
  <title>树形结构___ajax请求方式</title>  
  <script type="text/javascript" src="dtree.js"></script>  
  <script type="text/javascript" src="jQuery.js"></script>  
  <link rel="stylesheet" href="dtree.css" type="text/css"></link>  
  
  <script type="text/javascript">    
    tree = new dTree('tree');
    $.ajax({   
    url:'NodesPrint',
    type:'post', //数据发送方式   
    dataType:'xml', //接受数据格式   
    error:function(json){  
             alert( "not lived!");  
       },  
    async: false ,//同步方式  
    success: function(xml){  
         $(xml).find("node").each(function(){   
         var nodeId=$(this).attr("nodeId");    
         var parentId=$(this).attr("parentId");        
         var nodeName=$(this).text();   
         tree.add(nodeId,parentId,nodeName,"","","","","",false);  


                        });  
           }  
     });   
         document.write(tree);  
    </script> 
  </head>
  <body>
  </body>
</html>
--------------------------------------------------------------------------------------------------------------------------
工程部署到web后显示 not lived!


[解决办法]
$.ajax({   
    url:'NodesPrint',
    type:'post', //数据发送方式   
    dataType:'xml', //接受数据格式   
    error:function(json){  
             alert( "not lived!");  
       },  

那就是ajax请求出错了。你看看报了什么错
[解决办法]
url:'NodesPrint',你这URL写的没问题吗?什么也不加就会调用到你的Servlet 里了?建议楼主先在Servlet 里打断点,看看AJAX到底调用到你的Servlet 里没,然后建议你去掉out.println("<?xml version="1.0" encoding="UTF-8"?>");  这句话,有的时候解析XML格式文件的东西是不识别头的。
[解决办法]
writer.flush();
writer.close();
谢谢!
[解决办法]
碰到这种问题,就是debug起,打断点,一步步看
[解决办法]
该回复于2013-12-16 19:12:52被版主删除
[解决办法]
建议楼主用firefox firebug调试下,firebug可以看到你的ajax请求内容和返回内容是否正确。

热点排行