1. 程式人生 > >用Servlet返回JSON文本動態創建DataGrid

用Servlet返回JSON文本動態創建DataGrid

Language path import doc -c default 哈哈 返回 shee

<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>用Servlet返回JSON文本動態創建DataGrid</title>
    <!-- 引入css文件,無順序 -->
    <link rel="stylesheet" href="themes/icon.css" type="text/css"
></link> <link rel="stylesheet" href="themes/default/easyui.css" type="text/css"></link> <!-- 引入js文件,有順序 --> <script type="text/javascript" src="js/jquery.min.js"></script> <script type="text/javascript" src="js/jquery.easyui.min.js"></script
> <script type="text/javascript" src="js/easyui-lang-zh_CN.js"></script> </head> <body> <table id="dg"></table> <script type="text/javascript"> $("#dg").datagrid({ url : "${pageContext.request.contextPath}/UserServlet?time=
"+new Date().getTime(), columns : [[ {field:id,title:編號,width:100}, {field:name,title:姓名,width:100}, {field:sal,title:薪水,width:100}, {field:sex,title:性別,width:100} ]], fitColumns : true, singleSelect : true, pagination : true, pageSize : 2, pageList : [2,4] }); </script> </body> </html>
package loaderman.bean2json;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

/**
 * 用Servlet返回JSON文本動態創建DataGrid
 *
 * @author AdminTC
 */
public class UserServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");

        //收集DataGrid向服務器發送的參數--page(當前頁號)
        String page = request.getParameter("page");
        System.out.println("page=" + page);

        //收集DataGrid向服務器發送的參數--rows(當前需要顯示的記錄數)
        String rows = request.getParameter("rows");
        System.out.println("rows=" + rows);

        List<User> userList = new ArrayList<User>();
        userList.add(new User(1, "哈哈", 1000, "男"));
        userList.add(new User(2, "呵呵", 2000, "男"));
        userList.add(new User(3, "嘻嘻", 3000, "女"));
        userList.add(new User(4, "笨笨", 4000, "男"));
        userList.add(new User(5, "聰聰", 5000, "男"));
        userList.add(new User(6, "月月", 6000, "女"));
        userList.add(new User(7, "花花", 7000, "女"));

        Map<String, Object> map = new LinkedHashMap<String, Object>();
        map.put("total", userList.size());
        map.put("rows", userList);

        JSONArray jsonArray = JSONArray.fromObject(map);
        String jsonJAVA = jsonArray.toString();

        jsonJAVA = jsonJAVA.substring(1, jsonJAVA.length() - 1);

        //以流的方式將JSON文本輸出到DateGrid組件中
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter pw = response.getWriter();
        pw.write(jsonJAVA);
        pw.flush();
        pw.close();
    }
}

用Servlet返回JSON文本動態創建DataGrid