码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2018-12-03 20:06:27      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:datagrid   back   创建   tle   ack   sonar   json   gettime   this   

<%@ 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

标签:datagrid   back   创建   tle   ack   sonar   json   gettime   this   

原文地址:https://www.cnblogs.com/loaderman/p/10059875.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!