2015-04-14 80 views
0

我在這裏很疲倦,在jsp中顯示JTable中的數據,但它總是在撥號框中顯示錯誤,如「與服務器通信時發生錯誤」。 我是新來的ajax jquery如何顯示數據給這種友善的幫助?jtable不顯示jsp中的數據它顯示錯誤

HTML:

<!-- Include one of jTable styles. --> 
<link href="blue/jtable.css" rel="stylesheet" type="text/css" /> 
<link href="js/jquery-ui.min.css" rel="stylesheet" type="text/css" /> 


<!-- Include jTable script file. --> 
<!-- Include jTable script file. --> 


<script src="js/jquery-1.11.2.min.js" type="text/javascript"></script> 
<script src="js/jquery-ui.js" type="text/javascript"></script> 
<script src="js/jquery.jtable.js" type="text/javascript"></script> 


<script type="text/javascript"> 



    $(document).ready(function() { 



     $('#test').jtable({ 
       title : 'Senthil Table List', 
      paging: true, //Enable paging 
      pageSize: 20, //Set page size (default: 10)   
      actions: 
      { 
       listAction: 'Form?action=listUser', 
      /* createAction:'Form?action=insert', 
       updateAction: 'Form?action=edit', 
       deleteAction: 'Form?action=delete' */ 
      }, 

       fields : { 
         Id : { 
           title : 'Id', 
           sort :true, 
           width : '30%', 
           key : true, 
           list : true, 
           edit : false, 
           create : true 
         }, 
         Name : { 
           title : 'Name', 
           width : '30%', 
           edit : true 
         }, 
         Job : { 
           title : 'Job', 
           width : '30%', 
           edit : true 
         }, 

       } 
     }); 
     $('#test').jtable('load'); 
}); 


<script> 
</head> 
<body> 


<div id="test"> 
</div> 

的Servlet:

package Servlet; 

import java.io.IOException; 

import javax.servlet.RequestDispatcher; 
import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 



import org.json.JSONObject; 

import Allmethod.Testmethod; 
import DAO.Dao; 

/** 
* Servlet implementation class Form 
*/ 
@WebServlet("/Form") 
public class Form extends HttpServlet 
{ 
    private static final long serialVersionUID = 1L; 

    /** 
    * @see HttpServlet#HttpServlet() 
    */ 



    private static String INSERT_OR_EDIT = "/Edit.jsp"; 
    private static String LIST_USER = "/listUser.jsp"; 



    private Testmethod t; 
    Dao dao=new Dao(); 
    public Form() 
    { 
     super(); 
     // TODO Auto-generated constructor stub 
     t=new Testmethod(); 
    } 
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    { 
     response.setContentType("application/Json"); 


     System.out.println("inside form"); 
     try 
     { 
     String forward=""; 
     String action = request.getParameter("action"); 

     System.out.println("action are========>"+action); 
if (action.equalsIgnoreCase("listUser")) 
     { 
     try 
     { 
      forward = LIST_USER; 
      System.out.println("inside========"); 

       // Fetch Data from Student Table 
       Object s = t.Select(1, 10); 
       // Get Total Record Count for Pagination 
       int userCount = t.getStudentCount(); 
       // Convert Java Object to Json 



       String jsonvalue=""; 


       JSONObject obj1=new JSONObject(); 
      obj1.put("users", s); 
      String resp = jsonvalue+"("+obj1.toString()+"TotalRecordCount"+ userCount + ")"; 

      System.out.println("response are====>"+resp); 

     response.getWriter().print(resp); 


     System.out.println("forward====>"+forward); 
     RequestDispatcher view = request.getRequestDispatcher(forward); 
     view.forward(request, response); 


     } 
       catch(Exception ex) 
     { 
         String error="{\"Result\":\"ERROR\",\"Message\":"+ex.getMessage()+"}"; 
         response.getWriter().print(error); 
     } 
     } 

     } 

     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 
} 

**java class:** 

public List<Dao> Select(int startPageIndex, int recordsPerPage) throws ClassNotFoundException, SQLException 
    { 
      List<Dao> daos = new ArrayList<Dao>(); 
      int range = startPageIndex+recordsPerPage; 
      String query="SELECT * from senthiil Limit "+startPageIndex+","+recordsPerPage+""; 
      // System.out.println(query); 

      con=databases.getconnection(); 
      try 
      { 
       PreparedStatement ps=con.prepareStatement(query); 

       System.out.println("ps====xxx======>"+ps); 
       ResultSet rs=ps.executeQuery(); 
       while(rs.next()) 
       { 
        Dao dao=new Dao(); 

        dao.setId(rs.getInt(1)); 
        dao.setName(rs.getString(2)); 
        dao.setJob(rs.getString(3)); 

        System.out.println("dao========>"+dao); 
        daos.add(dao); 
       } 

      } 
      catch (SQLException e) 
      { 
        System.err.println(e.getMessage()); 
      } 
      return daos; 
    } 

**My response are** 

    response are====>({"users":["Dao [id=145, name=ammudha, job=appa]","Dao [id=146, name=sgfs, job=sdfsd]","Dao [id=147, name=null, job=null]","Dao [id=148, name=null, job=null]","Dao [id=149, name=null, job=null]","Dao [id=150, name=null, job=null]","Dao [id=151, name=null, job=null]","Dao [id=152, name=null, job=null]","Dao [id=153, name=null, job=null]","Dao [id=154, name=null, job=null]"]}TotalRecordCount51) 

數據從servlet的來,但同時JSP加載到出錯

回答

0

爲了找到解決方案,你可以改變的方式你調用這個動作。

事情是這樣的:

$('#test').jtable({ 
      title : 'Senthil Table List', 
     paging: true, //Enable paging 
     pageSize: 20, //Set page size (default: 10)   
     actions: 
     { 
      listAction: function (postData, jtParams) { 
       return $.Deferred(function ($dfd) { 
        $.ajax({ 
         url: 'Form?action=listUser', 
         type: 'POST', 
         dataType: 'json', 
         data: postData, 
         success: function (data) { 
          $dfd.resolve(data); 
         }, 
         error: function (request, status, error) { 
          $dfd.reject(); 
         } 
        }); 
       }); 
      } 
     }, 

      fields : { 
        Id : { 
          title : 'Id', 
          sort :true, 
          width : '30%', 
          key : true, 
          list : true, 
          edit : false, 
          create : true 
        }, 
        Name : { 
          title : 'Name', 
          width : '30%', 
          edit : true 
        }, 
        Job : { 
          title : 'Job', 
          width : '30%', 
          edit : true 
        }, 

      } 
    }); 
    $('#test').jtable('load'); 

然後,您可以在Chrome或其他瀏覽器的調試,檢查錯誤函數的對象「錯誤」,並發現問題。