笔记(很乱,估计很多人看不懂)
1.action层代码
@Controller("userAction")
@Scope("prototype")
public class UserAction extends BaseAction<User> {
private static final long serialVersionUID = 1L;
@Resource
private UserService userService;
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
/**
* 登陆
*/
public String login() {
if (model != null) {
User userOk;
userOk = userService.logincheck(model);
writeObject2Json(userOk, null);
return NONE;
}
writeObject2Json(null, null);
return NONE;
}
}
2.关键工具pageBean和BaseAction
pageBean
package com.xie.action;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
/**
* 封装分页信息
*/
public class PageBean {
private int currentPage;//页码
private int pageSize;//每页显示的记录数
private DetachedCriteria detachedCriteria;//条件查询对象
private int total;//总记录数
private List rows;//显示的数据集合
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public DetachedCriteria getDetachedCriteria() {
return detachedCriteria;
}
public void setDetachedCriteria(DetachedCriteria detachedCriteria) {
this.detachedCriteria = detachedCriteria;
}
public List getRows() {
return rows;
}
public void setRows(List rows) {
this.rows = rows;
}
}
BaseAction
package com.xie.action;
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/**
* 表现层抽取
* @param <T>
*/
public class BaseAction<T> extends ActionSupport implements ModelDriven<T> {
protected PageBean pageBean = new PageBean();
DetachedCriteria detachedCriteria = null;
public void setPage(int page) {
pageBean.setCurrentPage(page);
}
public void setRows(int rows) {
pageBean.setPageSize(rows);
}
/**
* 将PageBean对象转为json并通过输出流写回客户端浏览器
* @param pageBean
* @param excludes
*/
public void writeObject2Json(Object object, String[] excludes) {
// 将pageBean对象转为json返回
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setExcludes(excludes);
String json = JSONObject.fromObject(object, jsonConfig).toString();
ServletActionContext.getResponse().setContentType(
"text/json;charset=UTF-8");
try {
ServletActionContext.getResponse().getWriter().print(json);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 将List对象转为json并通过输出流写回客户端浏览器
* @param List
* @param excludes
*/
public void writeList2Json(List list, String[] excludes) {
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setExcludes(excludes);
String json = JSONArray.fromObject(list, jsonConfig).toString();
ServletActionContext.getResponse().setContentType("text/json;charset=UTF-8");
try {
ServletActionContext.getResponse().getWriter().print(json);
} catch (IOException e) {
e.printStackTrace();
}
}
// 模型对象
protected T model;
public T getModel() {
return model;
}
public static final java.lang.String HOME = "home";
public static final java.lang.String LIST = "list";
// 在构造方法中动态创建模型对象
public BaseAction() {
ParameterizedType genericSuperclass = null;//(ParameterizedType) this.getClass().getGenericSuperclass();
Type genericSuperclass2 = this.getClass().getGenericSuperclass();
if(genericSuperclass2 instanceof ParameterizedType){
//Action没有被创建代理
genericSuperclass = (ParameterizedType) this.getClass().getGenericSuperclass();
}else{
//Action被创建代理
genericSuperclass = (ParameterizedType)this.getClass().getSuperclass().getGenericSuperclass();
}
Type[] actualTypeArguments = genericSuperclass.getActualTypeArguments();
// entityClass代表User
Class<T> entityClass = (Class<T>) actualTypeArguments[0];
// 实体类型可以确定了,创建条件查询对象
detachedCriteria = DetachedCriteria.forClass(entityClass);
pageBean.setDetachedCriteria(detachedCriteria);
// model代表new User()
try {
model = entityClass.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
3.easyui界面代码
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Pagination - jQuery EasyUI Demo</title>
<link rel="stylesheet" type="text/css"
href="js/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
href="js/themes/icon.css">
<link rel="stylesheet" type="text/css"
href="js/themes/demo.css">
<script type="text/javascript"
src="js/jquery-1.6.min.js"></script>
<script type="text/javascript"
src="js/jquery.easyui.min.js"></script>
<script type="text/javascript">
$(function(){
$('#test').datagrid({
title:'Test DataGrid',
iconCls:'icon-save',
width:700,
height:350,
pageSize: 3,//每页显示的记录条数,默认为10
pageList: [3, 5, 9],//可以设置每页记录条数的列表
nowrap:false,//是否在一行上显示
striped:true,//显示斑马线
collapsible:true,//显示折叠按钮
//url:'json/datagrid_data.json',
url:"employee_getEmployee.action",
sortName:'code',
sortOrder:'desc',
idField:'code',
frozenColumns:[[{field:'ck',checkbox:true},
{title:'id',field:'id',width:80,sortable:true}
]],
columns:[[
{title:'Base Information',colspan:3},
{field:'opt',title:'Operation',width:100,align:'center',rowspan:2,
formatter:function(value,rec){
return '<span style="color:red">Edit Delete</span>';
}}
],[
{field:'name',title:'Name',width:120,rowspan:2},
{field:'address',title:'Address',width:120,rowspan:2,sortable:true,
sorter:function(a,b){
return (a>b?1:-1);
}
},
{field:'age',title:'年龄',width:120,rowspan:2},
{field:'position',title:'职位',width:120,rowspan:2}
]],
pagination:true,//是否显示底部分页工具栏
});
});
</script>
</head>
<body>
<h2>I'm Creating Table</h2>
<table id="test"></table>
</body>
</html>
注意:JSONArray 的 fromObject()方法需要jar包,1.commons-beanutils-1.7.jar
2.commons-collections-3.2.1.jar
3.commons-lang-2.4.jar
4.commons-logging-1.0.4.jar
5.ezmorph-1.0.4.jar