我在这里已经厌倦了在jsp中显示JTable中的数据,但它总是在拨号框中显示错误,如“与服务器通信时出错。” 我是ajax jquery的新手,如何向这个显示数据提供帮助???
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 :(得分: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或其他浏览器中进行调试,查看错误功能中的对象“错误”并找到问题。