概述

  最近学到EasyUI的Datagrid数据网格,然后就做了一个小例子,中间层利用Struts2来完成,DAO层用的是Hibernate。

数据库

  数据库涉及到stuednt(name,noid,password,hobby,tno)表和teacher(tno,tname)表;实体的映射和实体之间关系的配置用的是注解的方式(mapper不能忘哦)。

 @Entity
@Table
public class Teacher {
private String tname;
@Id
private String tno;
//set/get方法略
}

Teacher.java

 @Entity
@Table
public class Student { private String name;
@Id
private String noid;
private String password;
private String hobby;
@ManyToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@JoinColumn(name="tno",nullable=false,insertable=false,updatable=false)
private Teacher teacher;
//set/get方法略 }

Student.java

  中间对数据库一个是获得所有的数据库的总条数,一个实现按条件分页查询代码如下:

 public List<Student> queryAllStudent(){
Session session = factory.openSession();
Transaction transaction = session.beginTransaction();
String sql = "from Student";
List<Student> list = null ;
try {
list = (List<Student>) session.createQuery(sql).list();
transaction.commit();
} catch (HibernateException e) {
transaction.rollback();
// TODO Auto-generated catch block
e.printStackTrace();
}finally{ session.close();
}
return list;
}
//通过学号姓名查询
public List<Student> queryStudentByNameOrNoid(String queryName,String queryNoid,int page,int rows){
String sql = "from Student where 1=1 ";
List<Student> list = null ;
boolean ss = null==queryName;
if((null==queryName) || ("".equals(queryName))){
sql = sql + "and to_char(1)=?";
queryName = "1";
}else{
sql = sql + "and name=?";
}
if((null == queryNoid) || ("".equals(queryNoid))){
sql = sql + "and to_char(1)=?";
queryNoid = "1"; }else{
sql = sql + "and noid=?";
}
Session session = factory.openSession();
Transaction transaction = session.beginTransaction();
try {
list = (List<Student>) session.createQuery(sql).setParameter(0, queryName)
.setParameter(1, queryNoid)
.setFirstResult((page-1)*rows)
.setMaxResults(rows).list();
transaction.commit();
} catch (HibernateException e) {
transaction.rollback();
// TODO Auto-generated catch block
e.printStackTrace();
}finally{ session.close();
}
return list;
}
//其他代码略

中间层(Struts2)

  中间的配置文件内容(struts.xml)为:

 <struts>
<constant name="struts.custom.i18n.resources" value="info">
</constant>
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<package name="hm" namespace="/" extends="struts-default,json-default">
<action name="queryStudentByNameOrNoid" class="com.lt.action.StudentAction" method="queryStudentByNameOrNoid">
<result name="success" type="json">
<param name="root">s</param>
<param name="excludeProperties">success</param></result>
<result name="error"></result>
</action>
</package>
</struts>
//中间其他配置内容略

  Action的方法为:

 public class StudentAction extends ActionSupport {
private Map <String,Object>request; private Map <String,Object>session; private Map <String,Object>application;
private Map<String,Object> s ;
private Student student; private String queryName;
private String queryNoid; private int page;
private int rows;
StudentDao studentDao = new StudentDao(); public String queryStudentByNameOrNoid(){
List<Student> student = new ArrayList<>();
List<Student> student1 = new ArrayList<>();
List<Teacher> student2 = new ArrayList<>();
List<Object> sl = new ArrayList<>();
s = new HashMap<String, Object>();
student = studentDao.queryStudentByNameOrNoid(queryName, queryNoid,page,rows);
student1 = studentDao.queryAllStudent();
s.put("total", student1.size());
System.out.println(student1.size());
s.put("rows", student);
return SUCCESS;
} //其他略
}

 页面内容:

 <%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>DataGrid Complex Toolbar - jQuery EasyUI Demo</title>
<link rel="stylesheet" type="text/css" href="jquery-easyui-1.5.3/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="jquery-easyui-1.5.3/themes/icon.css">
<script type="text/javascript" src="jquery-easyui-1.5.3/jquery.min.js"></script>
<script type="text/javascript" src="jquery-easyui-1.5.3/jquery.easyui.min.js"></script>
</head>
<body>
<script type="text/javascript">
function lal(){
queryName = $("#queryName").val();
queryNoid = $("#queryNoid").val();
//var url = "queryStudentByNameOrNoid.action";
//$("#detilfrom").attr("url",url);
$("#detilfrom").datagrid('load',{queryName:queryName,queryNoid:queryNoid});
} </script>
<h2>DataGrid Complex Toolbar</h2> <div style="margin:20px 0;"></div>
<table id="detilfrom" class="easyui-datagrid" title="学生列表" style="width:450px;height:300px"
pagination="true"
data-options="rownumbers:true,singleSelect:true,url:'queryStudentByNameOrNoid.action',method:'get',toolbar:'#tb,#ft',footer:'#ft'">
<thead>
<tr>
<th data-options="field:'name',width:80">姓名</th>
<th data-options="field:'noid',width:100">学号</th>
<th data-options="field:'password',width:80,align:'right'">密码</th>
<th data-options="field:'hobby',width:80,align:'right'">爱好</th>
<th data-options="field:'tname',width:80,align:'right', formatter: function (value, rec) {
return rec.teacher['tname'];}">教师姓名</th>
</tr>
</thead>
</table>
<div id="tb" style="padding:2px 5px;">
姓名: <input id="queryName" style="width:110px">
学号: <input id="queryNoid" style="width:110px">
<a href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="lal()">Search</a>
</div>
<div id="ft" style="padding:2px 5px;">
<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true"></a>
<a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true"></a>
<a href="#" class="easyui-linkbutton" iconCls="icon-save" plain="true"></a>
<a href="#" class="easyui-linkbutton" iconCls="icon-cut" plain="true"></a>
<a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true"></a>
</div>
</body>
</html>

  需要注意的是:

  1.datagrid接收的数据格式为(从EasyUI的Demo中copy):

{"total":28,"rows":[
{"productid":"FI-SW-01","unitcost":10.00,"status":"P","listprice":36.50,"attr1":"Large","itemid":"EST-1"},
{"productid":"K9-DL-01","unitcost":12.00,"status":"P","listprice":18.50,"attr1":"Spotted Adult Female","itemid":"EST-10"},
{"productid":"RP-SN-01","unitcost":12.00,"status":"P","listprice":28.50,"attr1":"Venomless","itemid":"EST-11"},
{"productid":"RP-SN-01","unitcost":12.00,"status":"P","listprice":26.50,"attr1":"Rattleless","itemid":"EST-12"},
{"productid":"RP-LI-02","unitcost":12.00,"status":"P","listprice":35.50,"attr1":"Green Adult","itemid":"EST-13"},
{"productid":"FL-DSH-01","unitcost":12.00,"status":"P","listprice":158.50,"attr1":"Tailless","itemid":"EST-14"},
{"productid":"FL-DSH-01","unitcost":12.00,"status":"P","listprice":83.50,"attr1":"With tail","itemid":"EST-15"},
{"productid":"FL-DLH-02","unitcost":12.00,"status":"P","listprice":63.50,"attr1":"Adult Female","itemid":"EST-16"},
{"productid":"FL-DLH-02","unitcost":12.00,"status":"P","listprice":89.50,"attr1":"Adult Male","itemid":"EST-17"},
{"productid":"AV-CB-01","unitcost":92.00,"status":"P","listprice":63.50,"attr1":"Adult Male","itemid":"EST-18"}
],"footer":[
{"unitcost":19.80,"listprice":60.40,"productid":"Average:"},
{"unitcost":198.00,"listprice":604.00,"productid":"Total:"}
]}

  其中total和rows必不可少,total为总条数,rows为内容。

  2.在上述的数据格式中若有嵌套的json数据,要想在表格中展示可以利用formatter属性(见jsp页面中的‘’教师姓名‘’列);我的表格中的数据格式为(student实体属性中包含teache属性,为俩张变中的内容,现在利用formatter可以全部显示)

对于datagrid显示多表数据也可以利用后台数据输出格式来控制(可以利用mybatis查询的结果控制):

 {"total":2,"rows":[{"hobby":"123","name":"刘晓杰","noid":"123","password":"123","teacher":{"tname":"小刘","tno":"012"}},{"hobby":"456","name":"拉拉","noid":"456","password":"456","teacher":{"tname":"小拉","tno":"125"}}]}

  3.下面是查询的标签设置,若像下面书写,href为#时,点击查询只发送一条请求,若为其他则会发送俩次请求,导致查询结果出错。查看其他的解决datagrid发送俩次请求的解决方式—— 一种是去掉class,一种是去除url设置。由于和此处的用法有出入,因此没用采用。

<a href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="lal()">Search</a>

参考内容:

http://www.cnblogs.com/easypass/archive/2012/12/16/2820996.html

http://blog.csdn.net/qiushisoftware/article/details/37991591

Struts2+Datagrid表格显示(可显示多表内容)的更多相关文章

  1. EasyUI Datagrid 鼠标悬停显示单元格内容 复制代码

    EasyUI Datagrid 鼠标悬停显示单元格内容 ,halign:, align: 0 « 上一篇:LINQ to Entities 中的查询» 下一篇:去掉字符串中的非数字字符 posted ...

  2. EasyUI的Datagrid鼠标悬停显示单元格内容

    功能描述:table鼠标悬停显示单元格内容 1.js函数 function hoveringShow(value) { return "<span title='" + va ...

  3. EasyUI Datagrid 鼠标悬停显示单元格内容

    第一种方式: .js 定义函数 <script type="text/javascript"> //格式化单元格提示信息 function formatCellTool ...

  4. js控制select选中显示不同表单内容

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. Django项目:CRM(客户关系管理系统)--17--09PerfectCRM实现King_admin显示注册表的内容

    {#table_data_list.html#} {## ————————08PerfectCRM实现King_admin显示注册表的字段表头————————#} {% extends 'king_m ...

  6. Java连接MySQL数据库。编写一个应用程序,在主类Test_4类中,通过JDBC访问stu数据库,显示t_student表中的内容(表结构见表1),显示效果自己设计。

    题目2:编写一个应用程序,在主类Test_4类中,通过JDBC访问stu数据库,显示t_student表中的内容(表结构见表1),显示效果自己设计.之后,可根据显示的内容进行某条记录的删除(以id为条 ...

  7. sql已经在视图展示的语句如何显示别的表中的内容而不改变原有的值

    1.这个功能是我在公司的时候的一个需求,我师傅和我说你不可能就是说你可以添加的时候是数字但是展现给客户看的时候是数字最好是名称因为客户不知道这是什么意思 2.于是我陷入了漫长的实现这个功能中一开始只是 ...

  8. SSh结合Easyui实现Datagrid的分页显示

    近日学习Easyui,发现非常好用,界面很美观.将学习的心得在此写下,这篇博客写SSh结合Easyui实现Datagrid的分页显示,其他的例如添加.修改.删除.批量删除等功能将在后面的博客一一写来. ...

  9. 实例:SSh结合Easyui实现Datagrid的分页显示

    近日学习Easyui,发现非常好用,界面很美观.将学习的心得在此写下,这篇博客写SSh结合Easyui实现Datagrid的分页显示,其他的例如添加.修改.删除.批量删除等功能将在后面的博客一一写来. ...

随机推荐

  1. Azure 中 Linux 虚拟机的大小

    本文介绍可用于运行 Linux 应用和工作负荷的 Azure 虚拟机的可用大小与选项. 此外,还提供在计划使用这些资源时要考虑的部署注意事项. 本文也适用于 Windows 虚拟机. 类型 大小 说明 ...

  2. DevExpress中 TreeList控件的常规配置

    //以下为TreeList控件样式相关设置 this.treelist_SystemCfg.BackColor = Color.Transparent; this.treelist_SystemCfg ...

  3. sql server中同时执行select和update语句死锁问题

    原始出处 http://oecpby.blog.51cto.com/2203338/457054 最近在项目中使用SqlServer的时候发现在高并发情况下,频繁更新和频繁查询引发死锁.通常我们知道如 ...

  4. 使用CoreImage教程

    使用CoreImage教程 CoreImage包含有很多实用的滤镜,专业处理图片的库,为了能看到各种渲染效果,请使用如下图片素材. 现在可以开始教程了: #define FIX_IMAGE(image ...

  5. 监控DAG状态

    Add-PSSnapin microsoft.exchange* Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 $server ...

  6. iframe加载顺序导致数据访问出现问题

    背景: 一个页面A内有一个iframe,src指向了B页面. 问题: 页面A通过Ajax获取服务器数据,并赋值给了页面A的全局变量gData,页面B要用到页面A的数据gData.那么问题来了当B访问g ...

  7. July 20th 2017 Week 29th Thursday

    The darkness is no darkness with you. 有了你,黑暗将不再是黑暗. The darkness will not be driven out if we failed ...

  8. easyui学习笔记5—panel加载其他的页面

    上一篇中我们看到了panel的基本实现,没有什么难度,最重要的是data-options和class两个标签属性的定义.这里我们将看一下在panel中如何加载其他的页面. 1.先看看引用的资源文件和h ...

  9. 《SQL必知必会》总结

    目录   第1章 了解SQL 第2章 检索数据 第3章 排序检索数据 第4章 过滤数据 第5章 高级数据过滤 第6章 用通配符进行过滤 第7章 创建计算字段 第8章 使用数据处理函数 第9章 汇总数据 ...

  10. 使用nodejs代码在SAP C4C里创建Individual customer

    需求:使用nodejs代码在SAP Cloud for Customer里创建Individual customer实例. 代码: var createAndBind = require('../je ...