用Servlet返回JSON文本动态创建DataGrid

Posted 码上加油站

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用Servlet返回JSON文本动态创建DataGrid相关的知识,希望对你有一定的参考价值。

<%@ 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的主要内容,如果未能解决你的问题,请参考以下文章

用jquery的post方法提交数据需要servlet返回的数据类型是json的数据格式,应该怎么做??

Java Web可视化实践项目

用socket怎么获得javaWeb返回的json

servlet怎么将JSON 串返回给前台页面,前台又通过啥方式接受并显示出来呢?有没有实例参考下

动态设置 dojox.grid.datagrid 标题列宽

Java ee学习笔记