• 用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:'编号',100},    
                                    {field:'name',title:'姓名',100},    
                                    {field:'sal',title:'薪水',100},
                                    {field:'sex',title:'性别',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();
        }
    }
  • 相关阅读:
    PAT 甲级 1113 Integer Set Partition
    简单 dp
    #Leetcode# 707. Design Linked List
    #Leetcode# 817. Linked List Components
    #Leetcode# 268. Missing Number
    #Leetcode# 328. Odd Even Linked List
    #Leetcode# 445. Add Two Numbers II
    jzoj 1252. 天平
    2019.01.26【NOIP提高组】模拟 B 组 总结
    【NOIP2011DAY2】计算系数
  • 原文地址:https://www.cnblogs.com/loaderman/p/10059875.html
Copyright © 2020-2023  润新知