jqGrid从servlet读取数据

JQGrid从java Servlet读取数据
2008-10-24 10:32
新建Servlet如下
package org.crystal.jqgrid;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.crystal.mapping.*;

import java.io.PrintWriter;
import java.util.*;

public class Jqgrid extends HttpServlet {
    private String defaultEncoding = "ISO-8859-1";

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setContentType("application/json;charset=" + getEncoding());
        PrintWriter out = response.getWriter();
        String json="";
        /*System.out.println(request.getQueryString());
        System.out.println(request.getMethod());
        System.out.println(request.getParameter("name"));
        System.out.println(request.getParameter("mem"));
        */
        System.out.println("*******************************************");
        System.out.println("Out Print all Post & Get getParameter...");
        Map map=request.getParameterMap();
        Iterator it = map.entrySet().iterator();

        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            Object key = entry.getKey();
            Object value = entry.getValue();
            System.out.println("key:"+key.toString()+" value:"+request.getParameter(key.toString()));
        }
        System.out.println("End Out Print all Post & Get getParameter");
        System.out.println("*******************************************");
        //System.out.println("org.crystal.jqgrid.doGet");        String json="";
        json +="{ total: '200', page: '2', records: '30',rows : [ ";
        json +="{id:'1', cell:['cell11', 'cell12', 'cell13']}";
        json +=", {id:'2', cell:['cell21', 'cell22', 'cell23']}";
        json +=" ] }";
        out.print(""+json+"");
        //
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);

    }

    /**
    * Retrieve the encoding <p/>
    *
    * @return The encoding associated with this template (defaults to the value
    *         of 'struts.i18n.encoding' property)
    */
    protected String getEncoding() {
        String encoding = this.defaultEncoding;

        if (encoding == null) {
            encoding = System.getProperty("file.encoding");
        }

        if (encoding == null) {
            encoding = "UTF-8";
        }
        return encoding;
    }

}

在web.xml中配置访问上下文
    <servlet>
        <servlet-name>Jqgrid</servlet-name>
        <servlet-class>org.crystal.jqgrid.Jqgrid</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>Jqgrid</servlet-name>
        <url-pattern>/servlet/Jqgrid</url-pattern>
    </servlet-mapping>

编辑客户端的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>jqGrid</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" media="screen" href="jquery/jqGrid/themes/basic/grid.css" />
        <link rel="stylesheet" type="text/css" media="screen" href="jquery/jqGrid/themes/jqModal.css" />

        <script src="jquery/jquery.js" type="text/javascript"></script>
        <script src="jquery/jqGrid/jquery.jqGrid.js" type="text/javascript"></script>
        <script src="jquery/jqGrid/js/jqModal.js" type="text/javascript"></script>
        <script src="jquery/jqGrid/js/jqDnR.js" type="text/javascript"></script>
    </head>

    <body>
        <script type="text/javascript">
        jQuery(document).ready(function(){
            jQuery("#list").jqGrid({
                url:'servlet/Jqgrid',
                editurl:'servlet/Jqgrid', 
                datatype: "json",
                loadtext:'正在加载...',
                height: 250,
                   colNames:['id,'invdate', 'name'],
                   colModel:[
                       {name:'id',index:'id', width:70, sorttype:"int", editable: true},
                       {name:'invdate',index:'invdate', width:90, sorttype:"date", editable: true},
                       {name:'name',index:'name', width:100, editable: true}
                   ],
                pager: jQuery('#pager'),
                rowNum:4,
                rowList:[3,6,9,12],
                sortname: 'id',
                sortorder: "desc",
                viewrecords: true,
                   imgpath: 'jquery/jqGrid/themes/basic/images',
                   caption: '测试实例'
                }).navGrid('#pager',
                    {edit:true,add:true,del:true,search:false }
                    );
                  
});
  
//

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

    </body>
</html>

发布程序并访问客户端的jsp页面

你可能感兴趣的:(jqGrid从servlet读取数据)