折腾了一上午终于吧分页弄出来。。。。太不容易了,
基类
public class Books {
private int id;
private String name;
private String Athor;
public Books() {
super();
// TODO Auto-generated constructor stub
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAthor() {
return Athor;
}
public void setAthor(String athor) {
Athor = athor;
}
}
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
DAO
public class BookDAO {
public int getCount(){
Connection conn = ConnectionUtils.getConnection();
PreparedStatement stmt = null;
ResultSet rs = null;
int count = 0;
String sql = "select count(*) from t_book";
try {
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
if(rs.next()){
count = rs.getInt(1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return count;
}
public List<Books> getPageBook(int pageNo, int rows){
Connection conn = ConnectionUtils.getConnection();
PreparedStatement pstmt = null;
ResultSet rs = null;
List<Books> list = new ArrayList<Books>();
String sql = "select * from t_book limit ?,?";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, ((pageNo-1)*rows));
pstmt.setInt(2, rows);
rs = pstmt.executeQuery();
while(rs.next()){
Books book = new Books();
book.setId(rs.getInt("id"));
book.setName(rs.getString("name"));
book.setAthor(rs.getString("athor"));
list.add(book);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
}
Anction
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class BookAction extends BaseAction{
private int total;
private List<Object> rows;
private Map<String,Object> result = new HashMap<String, Object>();
public Map<String, Object> getResult() {
return result;
}
public void setResult(Map<String, Object> result) {
this.result = result;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public List<Object> getRows() {
return rows;
}
public void setRows(List<Object> rows) {
this.rows = rows;
}
public String execute(){
int page = Integer.parseInt(request.getParameter("page"));
int row = Integer.parseInt(request.getParameter("rows"));
BookDAO dao = new BookDAO();
this.total = dao.getCount();
this.rows = new ArrayList<Object>();
List<Books> list = dao.getPageBook(page, row);
for(Books book : list){
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", book.getId());
map.put("name", book.getName());
map.put("athor", book.getAthor());
this.rows.add(map);
}
result.put("total", total);
result.put("rows", rows);
return "success";
}
}
配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<constant name="struts.i18n.encoding" value="UTF-8" />
<constant name="struts.ui.theme" value="simple" />
<package name="/" extends="json-default">
<action name="dataGrid" class="BookAction" >
<result name="success" type="json">
<param name="root">result</param>
</result>
</action>
</package>
<!-- Add packages here -->
</struts>
页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'datagrild.jsp' starting page</title>
<link rel="stylesheet" href="easyui/themes/icon.css" type="text/css"></link>
<link rel="stylesheet" href="easyui/themes/default/easyui.css" type="text/css"></link>
<script type="text/javascript" src="easyui/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="easyui/locale/easyui-lang-zh_CN.js"></script>
</head>
<body>
<table id="datagril" class="easyui-datagrid"></table>
</body>
<script type="text/javascript">
$(function(){
$('#datagril').datagrid({
url:'dataGrid.action',
striped:true,
singleSelect:true,
collapsible: true,
rownumbers: true,
fitColumns:true,
pagination:true,
columns:[[
{field:'id',title:'Id',width:100},
{field:'name',title:'Name',width:100},
{field:'athor',title:'Athor',width:100,align:'right'}
]]
});
$('#datagril').datagrid('getPager').pagination({
displayMsg:'当前显示从{from}到{to}共{total}记录',
onBeforeRefresh:function(pageNumber, pageSize){
$(this).pagination('loading');
$(this).pagination('loaded');
}
});
});
</script>
</html>
其中遇到了好多的问题
1:要加入struts2-json-plugin-2.1.8.1.jar
2:把xwork-core-2.3.15.1.jar换成 xwork-core-2.1.6.jar