【求助】jqgrid里获取不到后台数据

lchina1314 2011-03-09 10:33:30
device_manage.html代码
<SCRIPT type="text/javascript">  
$(document).ready(function()
{
$("#gridTable").jqGrid({

url:'../src/db/ViewData'
datatype:'json',
autowidth : true,
height:'auto',
colNames:['设备ID','设备域名', '设备名称', 'IP地址', '端口号','更新时间','状态'],
colModel:[
{name:'devId',index:'devId', width:80, editable:true},
{name:'devDomainName',index:'devDomainName', width:120,editable:true},
{name:'devName',index:'devName', width:120},
{name:'ipAddress',index:'ipAddress', width:80, align:"right"},
{name:'port',index:'port', width:80,align:"right"},
{name:'updateTime',index:'updateTime', width:120,sorttype:'date', editrules:{date:true},formatter:'date', datefmt:'d/m/Y'},
{name:'state',index:'state', width:80, sortable:false}
],
jsonReader:{
root: "rows", // json中代表实际模型数据的入口
page: "page", // json中代表当前页码的数据
total: "total", // json中代表页码总数的数据
records: "records", // json中代表数据行总数的数据
repeatitems:false
},
width:2,
sortname:'devId',
sortorder:'asc',
viewrecords:true,
rowNum:10,
rowList:[10,20,30],
pager:"#pager",
cellEdit:false,
caption: "设备管理",
multiselect: true ,
sunGrid:true

}).navGrid('#pager',{edit:true,add:true,del:true,search:true,refresh:true,view:true});
var mydata = [
{devId:'ID0000801940411152210651',devDomainName:'weilan',devName:'dg',ipAddress:'119.39.97.85',port:'3000',updateTime:'2011-02-27 07:47:52.0',state:'1'},
{devId:'ID0000801940411152210651',devDomainName:'weilan',devName:'dg',ipAddress:'119.39.97.85',port:'3000',updateTime:'2011-02-27 07:47:52.0',state:'1'}

];
for(var i=0;i<=mydata.length;i++)
$("#gridTable").jqGrid('addRowData',i+1,mydata[i]);
});



</script>
<table id="gridTable" ></table>
<div id="pager" class="scroll" style="text-algin:center;"></div>

package db;
import java.io.IOException;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class ViewData extends HttpServlet {

/**
* @param args
*/
public void getData (HttpServletRequest request, HttpServletResponse response)throws Exception
//public void getData() //方面调试json,省去参数。
{
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/pcwebdb";
String user="root";
String password="root";
String sql=null;
Connection conn=null;
Statement statement=null;
ResultSet rs=null;
try
{
conn = DriverManager.getConnection(url,user,password);
statement = conn.createStatement();
sql = "select * from tab_dev";
rs=statement.executeQuery(sql);
Class.forName(driver);

if(!conn.isClosed())
{
System.out.println("Succeeded connecting to the Database!");
}
String dev_id=null;
String dev_memo=null;
String dev_name=null;
String dev_OuterIP=null;
String dev_OuterPort=null;
String dev_Time_UTC=null;
String dev_active=null;


while(rs.next())
{
dev_id=rs.getString("dev_id");
dev_memo=rs.getString("dev_memo");
dev_name=rs.getString("dev_name");
dev_OuterIP=rs.getString("dev_OuterIP");
dev_OuterPort=rs.getString("dev_OuterPort");
dev_Time_UTC=rs.getString("dev_Time_UTC");
dev_active=rs.getString("dev_active");

//String page = request.getParameter("page");
//String rows = request.getParameter("rows");
//int totalRecord = 80;

String page="1";
String rows="2";
page=request.getParameter("page");
rows=request.getParameter("rows");
int totalRecord=1648;

int totalPage = totalRecord%Integer.parseInt(rows) == 0 ?
totalRecord/Integer.parseInt(rows) : totalRecord/Integer.parseInt(rows)+1;
try {
int index = (Integer.parseInt(page)-1)*Integer.parseInt(rows);
int pageSize = Integer.parseInt(rows);

String json = "{total: "+totalPage+", page: "+page+", records: "+totalRecord+", rows: [";
for (int i = index; i < pageSize + index && i<totalRecord; i++) {
json += "{devId:'"+dev_id+"',devDomainName:'"+dev_memo+"',devName:'"+dev_name+"',ipAddress:'"+dev_OuterIP+"',port:'"+dev_OuterPort+"',updateTime:'"+dev_Time_UTC+"',state:'"+dev_active+"'}";
if (i != pageSize + index - 1 && i != totalRecord - 1) {
json += ",";
}
}
json += "]}";
System.out.println(json);
response.getWriter().write(json);
} catch (Exception ex)
{
System.out.print("Excep in ViewData when invoke method getData"+ex.getMessage());
}

}
rs.close();
conn.close();
}
catch(ClassNotFoundException e)
{
System.out.println("Sorry,can't find the Driver");
e.printStackTrace();
}
catch(SQLException e)
{
e.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doPost(request,response);
}
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
{
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/pcwebdb";
String user="root";
String password="root";
String sql=null;
Connection conn=null;
Statement statement=null;
ResultSet rs=null;
try
{
conn = DriverManager.getConnection(url,user,password);
statement = conn.createStatement();
sql = "select * from tab_dev";
rs=statement.executeQuery(sql);
Class.forName(driver);

if(!conn.isClosed())
{
System.out.println("Succeeded connecting to the Database!");
}
String dev_id=null;
String dev_memo=null;
String dev_name=null;
String dev_OuterIP=null;
String dev_OuterPort=null;
String dev_Time_UTC=null;
String dev_active=null;


while(rs.next())
{
dev_id=rs.getString("dev_id");
dev_memo=rs.getString("dev_memo");
dev_name=rs.getString("dev_name");
dev_OuterIP=rs.getString("dev_OuterIP");
dev_OuterPort=rs.getString("dev_OuterPort");
dev_Time_UTC=rs.getString("dev_Time_UTC");
dev_active=rs.getString("dev_active");

String page="1";
String rows="2";
page=request.getParameter("page");// 取得当前页数,注意这是jqgrid自身的参数
rows=request.getParameter("rows");// 取得每页显示行数,,注意这是jqgrid自身的参数
int totalRecord=1648;// 总记录数(应根据数据库取得,在此只是模拟)

int totalPage = totalRecord%Integer.parseInt(rows) == 0 ?
totalRecord/Integer.parseInt(rows) : totalRecord/Integer.parseInt(rows)+1; // 计算总页数
try {
int index = (Integer.parseInt(page)-1)*Integer.parseInt(rows); //开始记录数
int pageSize = Integer.parseInt(rows);

String json = "{total: "+totalPage+", page: "+page+", records: "+totalRecord+", rows: [";
for (int i = index; i < pageSize + index && i<totalRecord; i++) {
json += "{devId:'"+dev_id+"',devDomainName:'"+dev_memo+"',devName:'"+dev_name+"',ipAddress:'"+dev_OuterIP+"',port:'"+dev_OuterPort+"',updateTime:'"+dev_Time_UTC+"',state:'"+dev_active+"'}";
if (i != pageSize + index - 1 && i != totalRecord - 1) {
json += ",";
}
} //for 循环
json += "]}";
System.out.println(json);
response.getWriter().write(json); // 将JSON数据返回页面
}//try结束
catch (Exception ex)
{
System.out.println("Exception In getWriter:"+ex.getMessage());
}
}//while循环
}//try结束
catch(Exception ex)
{
System.out.println("Exception In Do Post:"+ex.getMessage());
}
}//doPost结束
public static void main(String[] args) {
// TODO Auto-generated method stub

//ViewData test=new ViewData();
//test.getData();


}

}

相关图片:



...全文
912 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
lchina1314 2011-03-13
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 lchina1314 的回复:]
device_manage.jsp文件

HTML code
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request……
[/Quote]
我这么配置还有哪里不对吗?望各位大侠指点迷津。
lchina1314 2011-03-13
  • 打赏
  • 举报
回复
device_manage.jsp文件
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<head>
<base href="<%=basePath%>">
</head>
<style>
html, body {
margin: 0;
padding: 0;
font-size: 100%;
}
.ui-jqgrid tr.jqgrow td {
white-space: normal !important;
height:auto;
vertical-align:text-top;
padding-top:10px;
}
</style>
<SCRIPT type="text/javascript">
$(document).ready(function()
{
$("#gridTable").jqGrid({

url:'/DNSWebs/servlet/ViewDataServlet',
mtype:"POST",
datatype:'json',
autowidth : true,
height:'auto',
colNames:['设备ID','设备域名', 'IP地址', '端口号','更新时间','状态'],
colModel:[
{name:'devId',index:'devId', width:120, editable:true},
{name:'devDomainName',index:'devDomainName', width:120,editable:true},
{name:'ipAddress',index:'ipAddress', width:80, align:"right"},
{name:'port',index:'port', width:80,align:"right"},
{name:'updateTime',index:'updateTime', width:120,sorttype:'date', editrules:{date:true},formatter:'date', datefmt:'d/m/Y'},
{name:'state',index:'state', width:80, sortable:false}
],
jsonReader:{
root: "rows", // json中代表实际模型数据的入口
page: "page", // json中代表当前页码的数据
total: "total", // json中代表页码总数的数据
records: "records", // json中代表数据行总数的数据
repeatitems:false
},
width:2,
sortname:'devId',
sortorder:'asc',
viewrecords:true,
rowNum:10,
rowList:[10,20,30],
pager:"#pager",
cellEdit:false,
caption: "设备管理",
multiselect: true ,
sunGrid:true

}).navGrid('#pager',{edit:true,add:true,del:true,search:true,refresh:true,view:true});
var mydata = [
{devId:'ID0000801940411152210651',devDomainName:'test1',ipAddress:'119.39.97.85',port:'3000',updateTime:'2011-02-27 07:47:52.0',state:'1'},
{devId:'ID0000801940410882100613',devDomainName:'test2',ipAddress:'58.45.131.233',port:'3000',updateTime:'2011-02-26 04:46:24.0',state:'1'},
{devId:'ID0000801940411232310669',devDomainName:'test3',ipAddress:'123.120.26.43',port:'3000',updateTime:'2011-02-23 04:35:26.0',state:'1'},
];
for(var i=0;i<=mydata.length;i++)
$("#gridTable").jqGrid('addRowData',i+1,mydata[i]);
});



</script>
<table id="gridTable" ></table>
<div id="pager" class="scroll" style="text-algin:center;"></div>

ViewDataServlet.java文件
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

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


public class ViewDataServlet extends HttpServlet {

/**
* Constructor of the object.
*/
public ViewDataServlet() {
super();
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

/**
* 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
*/
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 {
System.out.println("Test");
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/pcwebdb";
String user="root";
String password="root";
String sql=null;
Connection conn=null;
Statement statement=null;
ResultSet rs=null;
try
{
conn = DriverManager.getConnection(url,user,password);
statement = conn.createStatement();
sql = "select * from tab_dev";
rs=statement.executeQuery(sql);
Class.forName(driver);

if(!conn.isClosed())
{
System.out.println("Succeeded connecting to the Database!");
}
String dev_id=null;
String dev_memo=null;
String dev_OuterIP=null;
String dev_OuterPort=null;
String dev_Time_UTC=null;
String dev_active=null;


while(rs.next())
{
dev_id=rs.getString("dev_id");
dev_memo=rs.getString("dev_memo");
dev_OuterIP=rs.getString("dev_OuterIP");
dev_OuterPort=rs.getString("dev_OuterPort");
dev_Time_UTC=rs.getString("dev_Time_UTC");
dev_active=rs.getString("dev_active");

String page="1";
String rows="1";
page=request.getParameter("page");
rows=request.getParameter("rows");
int totalRecord=1648;

int totalPage = totalRecord%Integer.parseInt(rows) == 0 ?
totalRecord/Integer.parseInt(rows) : totalRecord/Integer.parseInt(rows)+1;
try {
int index = (Integer.parseInt(page)-1)*Integer.parseInt(rows);
int pageSize = Integer.parseInt(rows);

String json = "{total: "+totalPage+", page: "+page+", records: "+totalRecord+", rows: [";
for (int i = index; i < pageSize + index && i<totalRecord; i++) {
json += "{devId:'"+dev_id+"',devDomainName:'"+dev_memo+"',ipAddress:'"+dev_OuterIP+"',port:'"+dev_OuterPort+"',updateTime:'"+dev_Time_UTC+"',state:'"+dev_active+"'}";
if (i != pageSize + index - 1 && i != totalRecord - 1) {
json += ",";
}
}
json += "]}";
System.out.println(json);
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);
} catch (Exception ex)
{
System.out.print("Excep in ViewData when invoke method getData"+ex.getMessage());
}

}
rs.close();
conn.close();
}
catch(ClassNotFoundException e)
{
System.out.println("Sorry,can't find the Driver");
e.printStackTrace();
}
catch(SQLException e)
{
e.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
}

/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
System.out.print("ViewDataServlet");
}

}

web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>ViewDataServlet</servlet-name>
<servlet-class>ViewDataServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>ViewDataServlet</servlet-name>
<url-pattern>/servlet/ViewDataServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>



</web-app>

Error 404
zn85600301 2011-03-10
  • 打赏
  • 举报
回复
[Quote=引用楼主 lchina1314 的回复:]
device_manage.html代码
HTML code
<SCRIPT type="text/javascript">
$(document).ready(function()
{
$("#gridTable").jqGrid({

url:'../src/db/Vie……
[/Quote]
你的2次URL请求的路径都不一样
第一次是完整的网络地址
第二次出错时只有你的工程内部路径

你加上url标签试试
疯狂椰子 2011-03-10
  • 打赏
  • 举报
回复
servlet-name:viewData
url-patten:viewData.do


servlet-name:viewData
servlet-class:db.ViewData

ajax 请求 url:/myvideo/DNSwebs/viewData.do

大概是这个样子吧
lchina1314 2011-03-09
  • 打赏
  • 举报
回复
URL请求servlet时,地址应该是class文件吧?不过我改了还是404NOT Found。
cheniqit1 2011-03-09
  • 打赏
  • 举报
回复
断点吧 有点乱
2011-03-09
  • 打赏
  • 举报
回复
没玩过这东西,获取不到后台数据,通常看看是不是配置错误,还有单词名字写错了没
lchina1314 2011-03-09
  • 打赏
  • 举报
回复
大家帮我看看封装的json格式和请求的url有什么问题吗?我哪里需要修改吗?
lchina1314 2011-03-09
  • 打赏
  • 举报
回复

是web.xml没有配置啊,谁帮我看看怎么填写啊,我第一次做这个不知道。前面已经给出文件路径了。
  • 打赏
  • 举报
回复
不过 直接根据 项目路径来请求应该 错了... 又不是 .Net
  • 打赏
  • 举报
回复
这样吧,用个SSH 框架 请求的时候 把 url 改下 看行不? 你的服务器我没有用过, 也不知道 Json 请求的路径对不对
lchina1314 2011-03-09
  • 打赏
  • 举报
回复
报错在最后面2个图的。404not found,不知道为什么???
  • 打赏
  • 举报
回复
包神马错弄来看看
  • 打赏
  • 举报
回复
URL 请求 Servlet ? 说说错吧
lchina1314 2011-03-09
  • 打赏
  • 举报
回复
没人知道吗?

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧