第一种方式:直接返回JSON数据

package com.easyuijson.util;

import java.text.SimpleDateFormat;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor; public class DateJsonValueProcessor implements JsonValueProcessor{ private String format; public DateJsonValueProcessor(String format){
this.format = format;
} public Object processArrayValue(Object value, JsonConfig jsonConfig) {
return null;
} public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
if(value == null)
{
return "";
}
if(value instanceof java.sql.Timestamp)
{
String str = new SimpleDateFormat(format).format((java.sql.Timestamp)value);
return str;
}
if (value instanceof java.util.Date)
{
String str = new SimpleDateFormat(format).format((java.util.Date) value);
return str;
} return value.toString();
} }
package com.easyuijson.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.json.JSONObject; import com.easyuijson.model.Student;
import com.easyuijson.util.DateJsonValueProcessor;
import com.easyuijson.util.ResponseUtil; import net.sf.json.JSONArray;
import net.sf.json.JsonConfig; public class DatagridData extends HttpServlet {
private static final long serialVersionUID = 1L;
private static List<Student> studentList=null;
static {
studentList = new ArrayList<Student>();
Student student1 = new Student(1001, "Lucy", "男", 23, "Lucy@126.com", "84562548", "三个地方规划的恢复规划法规部分");
Student student2 = new Student(1002, "Lucy", "男", 23, "Lucy@126.com", "84562548", "三个地方规划的恢复规划法规部分");
Student student3 = new Student(1003, "Lucy", "男", 23, "Lucy@126.com", "84562548", "三个地方规划的恢复规划法规部分");
Student student4 = new Student(1004, "Lucy", "男", 23, "Lucy@126.com", "84562548", "三个地方规划的恢复规划法规部分");
Student student5 = new Student(1005, "Lucy", "男", 23, "Lucy@126.com", "84562548", "三个地方规划的恢复规划法规部分");
Student student6 = new Student(1006, "Lucy", "男", 23, "Lucy@126.com", "84562548", "三个地方规划的恢复规划法规部分");
studentList.add(student1);
studentList.add(student2);
studentList.add(student3);
studentList.add(student4);
studentList.add(student5);
studentList.add(student6);
} @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp){
try {
System.out.println("跳转成功!");
int total = studentList.size();
JSONObject result = new JSONObject();
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.registerJsonValueProcessor(java.util.Date.class,
new DateJsonValueProcessor("yyyy-MM-dd"));
JSONArray jsonArray = JSONArray.fromObject(studentList, jsonConfig);
result.put("rows", jsonArray);
result.put("total", total);
ResponseUtil.write(resp, result);
} catch (Exception ex) {
ex.printStackTrace();
}
} @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
} }
package com.easyuijson.util;

import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
public class ResponseUtil { public static void write(HttpServletResponse response,Object o)throws Exception{
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
out.println(o.toString());
out.flush();
out.close();
}
}

   1)使用Ajax请求数据

<body>
<table id="dg" class="easyui-datagrid" title="基本数据表格"
style="width: 700px; height: 250px"
data-options="singleSelect:true,collapsible:true">
<thead data-options="frozen:true">
<tr>
<th data-options="field:'stuId',width:100">学生ID</th>
<th data-options="field:'stuName',width:100">学生姓名</th>
</tr>
</thead>
<thead>
<tr>
<th data-options="field:'stuSex',width:100">学生性别</th>
<th data-options="field:'stuAge',width:100">学生年龄</th>
<th data-options="field:'stuEmail',width:100,align:'center'">学生邮箱</th>
<th data-options="field:'stuQQ',width:100,align:'right'">学生QQ</th>
<th data-options="field:'stuAddress',width:200,align:'center'">学生地址</th>
</tr>
</thead>
</table>
</body>
<script type="text/javascript">
$(function() {
//动态加载标题和数据
$.ajax({
url : "${pageContext.request.contextPath}/datagridData.do",
type : "post",
dataType : "json",
success : function(data) {
$("#dg").datagrid("loadData", data.rows); //动态取数据
}
});
});
</script>

   2)使用表格Url属性请求数据

<table class="easyui-datagrid" title="基本数据表格"
style="width: 700px; height: 250px"
data-options="singleSelect:true,collapsible:true,url:'${pageContext.request.contextPath}/datagridData.do'">
<thead data-options="frozen:true">
<tr>
<th data-options="field:'stuId',width:100">学生ID</th>
<th data-options="field:'stuName',width:100">学生姓名</th>
</tr>
</thead>
<thead>
<tr>
<th data-options="field:'stuSex',width:100">学生性别</th>
<th data-options="field:'stuAge',width:100">学生年龄</th>
<th data-options="field:'stuEmail',width:100,align:'center'">学生邮箱</th>
<th data-options="field:'stuQQ',width:100,align:'right'">学生QQ</th>
<th data-options="field:'stuAddress',width:200,align:'center'">学生地址</th>
</tr>
</thead>
</table>

   3)js创建表格的时候使用表格Url属性请求数据 

<body>
<table id="studentList"></table>
</body>
<script type="text/javascript">
$('#studentList').datagrid({
title : '基本数据表格',
width : 700,
height : 250,
url : '${pageContext.request.contextPath}/datagridData.do',
frozenColumns : [ [ {
field : 'stuId',
title : '学生ID',
width : 100
}, {
field : 'stuName',
title : '学生姓名',
width : 100
} ] ],
columns : [ [ {
field : 'stuSex',
title : '学生性别',
width : 100
}, {
field : 'stuAge',
title : '学生年龄',
width : 100
}, {
field : 'stuEmail',
title : '学生邮箱',
width : 100
}, {
field : 'stuQQ',
title : '学生QQ',
width : 100
}, {
field : 'stuAddress',
title : '学生地址',
width : 200,
align : 'center'
} ] ] });
</script>

   第二种方式:通过JSTL填充表格

   1)前端页面

<table class="easyui-datagrid" title="基本数据表格"
style="width: 700px; height: 250px"
data-options="singleSelect:true,collapsible:true,url:'${pageContext.request.contextPath}/datagridData.do'">
<thead data-options="frozen:true">
<tr>
<th data-options="field:'stuId',width:100">学生ID</th>
<th data-options="field:'stuName',width:100">学生姓名</th>
</tr>
</thead>
<thead>
<tr>
<th data-options="field:'stuSex',width:100">学生性别</th>
<th data-options="field:'stuAge',width:100">学生年龄</th>
<th data-options="field:'stuEmail',width:100,align:'center'">学生邮箱</th>
<th data-options="field:'stuQQ',width:100,align:'right'">学生QQ</th>
<th data-options="field:'stuAddress',width:200,align:'center'">学生地址</th>
</tr>
</thead>
<tbody>
<c:forEach var="student" items="${studentList}">
<tr>
<td>${student.stuId}</td>
<td>${student.stuName}</td>
<td>${student.stuSex}</td>
<td>${student.stuAge}</td>
<td>${student.stuEmail}</td>
<td>${student.stuQQ}</td>
<td>${student.stuAddress}</td>
</tr>
</c:forEach>
</tbody>
</table>

   2)后台代码,使用servlet处理数据

package com.easyuijson.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import com.easyuijson.model.Student; public class DatagridData extends HttpServlet {
private static final long serialVersionUID = 1L;
private static List<Student> studentList=null;
static {
studentList = new ArrayList<Student>();
Student student1 = new Student(1001, "Lucy", "男", 23, "Lucy@126.com", "84562548", "三个地方规划的恢复规划法规部分");
Student student2 = new Student(1002, "Lucy", "男", 23, "Lucy@126.com", "84562548", "三个地方规划的恢复规划法规部分");
Student student3 = new Student(1003, "Lucy", "男", 23, "Lucy@126.com", "84562548", "三个地方规划的恢复规划法规部分");
Student student4 = new Student(1004, "Lucy", "男", 23, "Lucy@126.com", "84562548", "三个地方规划的恢复规划法规部分");
Student student5 = new Student(1005, "Lucy", "男", 23, "Lucy@126.com", "84562548", "三个地方规划的恢复规划法规部分");
Student student6 = new Student(1006, "Lucy", "男", 23, "Lucy@126.com", "84562548", "三个地方规划的恢复规划法规部分");
studentList.add(student1);
studentList.add(student2);
studentList.add(student3);
studentList.add(student4);
studentList.add(student5);
studentList.add(student6);
} @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("跳转成功!");
HttpSession httpSession= req.getSession();
httpSession.setAttribute("studentList",studentList);for(Student stu:studentList) {
System.out.println(stu);
}
} @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
} }

EasyUI表格DataGrid获取数据的方式的更多相关文章

  1. Request三种获取数据的方式

    今天在做ajax请求后台代码时,发现ajax的方法都对,但就是请求不了后台代码,后来在同事帮助下才发现前台定义了两个相同参数导致请求出错. 下面记录一下request三种获取数据的方式: 1. Req ...

  2. MFC获取数据的方式

    假设输入框ID是:ID_NUMBER1,ID_NUMBER2,ID_NUMBER3. 获取数据的方式是: int number1,number2,number3; number1 = GetDlgIt ...

  3. Layui数据表格的接口数据请求方式为Get

    Layui数据表格的接口数据请求方式为Get

  4. EasyUI的datagrid获取所有正在编辑状态的行的行编号

    今天项目需要用了下EasyUI的datagrid的行编辑功能,跟着API来,只要是将各种状态时的处理逻辑弄好,还是蛮不错的. 开发过程中,遇到了个问题,在编辑完成后我需要获取datagrid所有处于编 ...

  5. 关于url拼接传参数和利用view的字典传参数时,模板获取数据的方式问题

    url = "{% url 'dashboard:internship-theme-stat' %}?teacher_name="+teacher_name+"& ...

  6. EasyUI表格DataGrid前端分页和后端分页的总结

    Demo简介 Demo使用Java.Servlet为后台代码(数据库已添加数据),前端使用EasyUI框架,后台直接返回JSON数据给页面 1.配置Web.xml文件 <?xml version ...

  7. EasyUI表格DataGrid格式化formatter用法

    1.通过HTML标签创建数据表格时使用formatter <!DOCTYPE html> <html> <head> <meta charset=" ...

  8. 经历:easyui的datagrid没有数据滚动条的显示

    今天,一个用户提出一个这样的问题,"查询不到结果时,为什么我看不到后面的标题呢?" 最初,我听到这个问题时,第一反应是:查出来数据不就有滚动条了吗,干嘛非要较真呢? 不过,后来想想 ...

  9. EL表达式获取数据的方式

    <%@page import="cn.jiemoxiaodi.domain.Person"%> <%@ page language="java" ...

随机推荐

  1. 《HTTP - 概述》

    晚上听着 木小雅<可能否>,让我这暴躁的一天得以回复一下. 学习是长久之事,不能急躁.应该系统的去学习一些东西. 1:HTTP 最严谨的叫法应该是 超文本转移协议,但大家一般都叫做 超文本 ...

  2. IntelliJ常用设置及快捷键

    转自: http://www.blogjava.net/rockblue1988/archive/2014/10/25/418994.html 一.黑色主题 Darcula眼睛舒服,最重要的是酷!设置 ...

  3. 知乎改版api接口之scrapy自动登陆

    最近使用scrapy模拟登陆知乎,发现所有接口都发生变化了,包括验证码也发生了很大变化,通过抓包分析,记录下改版后的知乎模拟登陆,废话不多说,直接上代码,亲测有效 # -*- coding: utf- ...

  4. JavaScript 学习笔记-HTML&&DOM

    HTML DOM (文档对象模型) 当网页被加载时,浏览器会创建页面的文档对象模型(Document Object Model). HTML DOM 模型被构造为对象的树. JavaScript 能够 ...

  5. laravel项目ThinkSNS+安装

    ThinkSNS+ 是一个使用 Laravel 开发,并且功能繁多且健壮的社交程序.今天我们来跟着ytkah一起来尝鲜一下.首先PHP 版本必须大于 7.1.3,已经下载并安装过 Composer,拥 ...

  6. 火币网API文档——REST 行情、交易API简介

    REST API 简介 火币为用户提供了一套全新的API,可以帮用户快速接入火币PRO站及HADAX站的交易系统,实现程序化交易. 访问地址 适用站点 适用功能 适用交易对 https://api.h ...

  7. 万恶之源 - Python 自定义模块

    自定义模块 我们今天来学习一下自定义模块(也就是私人订制),我们要自定义模块,首先就要知道什么是模块啊 一个函数封装一个功能,比如现在有一个软件,不可能将所有程序都写入一个文件,所以咱们应该分文件,组 ...

  8. 将常用的T-CODE收藏进 文件夹

    1:选中文件夹,右键>insert transaction>输入相应的t-code.

  9. [vue]基础篇stepbystep案例实践(废弃)

    去看这个就好了 总结: 1.子组件可以触发父组件的方法,this.$emit() //(通知父组件干活) 2.父组件可以调用子组件的方法() // ref 如果放在组件上 获取的是组件的实例 并不是组 ...

  10. (转)ThreadLocal-面试必问深度解析

    ThreadLocal是什么 ThreadLocal是一个本地线程副本变量工具类.主要用于将私有线程和该线程存放的副本对象做一个映射,各个线程之间的变量互不干扰,在高并发场景下,可以实现无状态的调用, ...