最近做了一个等级考试报名系统,需要用到struts2分页技术,在网上书上找了很长时间都没发现符合要求的。最后经过自己研究写了一个struts2分页的程序,特此贴出供大家参考。本人是新手,若有些地方不好的还请大家指教。
因为是一个项目的一个小的demo所以需要大家自己修改,已符合自己的要求。
struts.xml中的配置:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="structs.devMode" value="true" />
<constant name="structs.i18n.encoding" value="GBK" />
<!--管理后台的namespace -->
<package name="admin" namespace="/admin" extends="struts-default">
<!-- 第一页的处理 -->
<action name="Showfirst" class="com.sc.project.action.ShowAction" method="first">
<result name="success">/admin/Student_list.jsp</result>
</action>
<!--上一页的处理 -->
<action name="Showpre" class="com.sc.project.action.ShowAction" method="pre">
<result name="success">/admin/Student_list.jsp</result>
</action>
<!--下一页的处理 -->
<action name="Shownext" class="com.sc.project.action.ShowAction" method="next">
<result name="success">/admin/Student_list.jsp</result>
</action>
<!--对最后一页的处理 -->
<action name="Showlast" class="com.sc.project.action.ShowAction" method="last">
<result name="success">/admin/Student_list.jsp</result>
</action>
</package>
</struts><?xml version="1.0" encoding="UTF-8" ?>
jsp文件:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>报名信息管理</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--<meta http-equiv="refresh" content="20">
<link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css">
-->
共<s:property value="page.totalNum"/>条记录 共<s:property value="page.lastPage"/>页 第<s:property value="page.pageNow"/>页
<br/>
<s:url id="url_first" value="admin/Showfirst.action">
</s:url>
<s:url id="url_pre" value="admin/Showpre.action">
<s:param name="pageNow" value="pageNow-1"></s:param>
</s:url>
<s:url id="url_next" value="admin/Shownext.action">
<s:param name="pageNow" value="pageNow+1"></s:param>
</s:url>
<s:url id="url_last" value="admin/Showlast.action">
</s:url>
<s:a href="%{url_first}" mce_href="%{url_first}" >首页</s:a>
<s:a href="%{url_pre}" mce_href="%{url_pre}">上一页</s:a>
<s:iterator value="examinees" status="status">
<s:url id="url" value="admin/Student_list.action">
<s:param name="page.pageNow" value="page.pageNow"/>
</s:url>
</s:iterator>
<s:a href="%{url_next}" mce_href="%{url_next}">下一页</s:a>
<s:a href="%{url_last}" mce_href="%{url_last}">最后一页</s:a>
<br/>
</body>
</html>
实体类
Page类:
package com.sc.project.model;
public class Page {
private int totalNum; //总记录数
private int lastPage; //最后一页页数
private int firstPage; //第一页
private int pageNow; //当前页数
private int pageSize; //一页包含多少条记录
public int getTotalNum() {
return totalNum;
}
public void setTotalNum(int totalNum) {
this.totalNum = totalNum;
}
public int getLastPage() {
return lastPage;
}
public void setLastPage(int lastPage) {
this.lastPage = lastPage;
}
public int getFirstPage() {
return firstPage;
}
public void setFirstPage(int firstPage) {
this.firstPage = firstPage;
}
public int getPageNow() {
return pageNow;
}
public void setPageNow(int pageNow) {
this.pageNow = pageNow;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
}
ShowAction类
package com.sc.project.action;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
import com.sc.project.model.Examinee;
import com.sc.project.model.Page;
import com.sc.project.service.ExamineeService;
public class ShowAction extends ActionSupport {
private List<Examinee> examinees ; //用于存放考生信息的集合
public List<Examinee> getExaminees() {
return examinees;
}
public void setExaminees(List<Examinee> examinees) {
this.examinees = examinees;
}
private Page page; //引入Page类型变量
private int pageNow; //当前页数
public int getPageNow() {
return pageNow;
}
public void setPageNow(int pageNow) {
this.pageNow = pageNow;
}
public Page getPage() {
return page;
}
public void setPage(Page page) {
this.page = page;
}
public ExamineeService getExamineeService() {
return examineeService;
}
public void setExamineeService(ExamineeService examineeService) {
this.examineeService = examineeService;
}
private ExamineeService examineeService = new ExamineeService () ;
//对第一页的处理
public String first(){
page = examineeService.total(); //获得页数的信息如当前页数 一页包含多少条记录等
page = examineeService.firstPage(page); //设置当前页数为第一页
examinees = examineeService.queryByPage(page); //查找第一页的数据
return SUCCESS;
}
public String pre(){
page = examineeService.total(); //获得页数的信息如当前页数 一页包含多少条记录等
if(pageNow<1){ //页数小于1时 当前页数,任然为1
pageNow=1;
}
page.setPageNow(pageNow); //设置当前的页数
examinees = examineeService.queryByPage(page); //查找当前页数的数据
return SUCCESS;
}
public String next(){
page = examineeService.total(); //获得页数的信息如当前页数 一页包含多少条记录等
if(pageNow<2){ //如果当前页数小于2下一页链接无效,下一页的第一个当前页面为第二页
pageNow=2;
}
if(pageNow>page.getLastPage()){
pageNow=page.getLastPage(); //如果当前页数大于最大的页数,是当前页数永远是最后一页
}
page.setPageNow(pageNow);
examinees = examineeService.queryByPage(page); //查找当前页数的数据
return SUCCESS;
}
public String last(){
page = examineeService.total(); //获得页数的信息如当前页数 一页包含多少条记录等
page = examineeService.lastPage(page); //设置当前页数为最大的也是
examinees = examineeService.queryByPage(page); //查找最后一页的信息
return SUCCESS;
}
}
ExamineeService类:
package com.sc.project.service;
import java.util.List;
import com.sc.project.Dao.ExamineeDao;
import com.sc.project.Dao.Imp.ExamineeDaoImpl;
import com.sc.project.model.Examinee;
import com.sc.project.model.Page;
import com.sc.project.model.SystemSetting;
public class ExamineeService {
ExamineeDao examineeDao = new ExamineeDaoImpl();
//后台对页数的处理
public Page total(){
Page page = examineeDao.total();
return page;
}
//后台分页的设置
public Page firstPage(Page page) {
page = examineeDao.firstPage(page);
return page;
}
public List<Examinee> queryByPage(Page page) {
List<Examinee> examinees = examineeDao.list(page);
return examinees;
}
public Page lastPage(Page page) {
page = examineeDao.lastPage(page);
return page;
}
}
ExamineeDao类:
package com.sc.project.Dao;
import java.util.List;
import com.sc.project.model.Examinee;
import com.sc.project.model.Page;
import com.sc.project.model.SystemSetting;
public interface ExamineeDao {
public List<Examinee> list(Page page);
public Page total();
public Page firstPage(Page page);
public Page lastPage(Page page);
}
dao实现层:
ExamineeDaoImpl类:
package com.sc.project.Dao.Imp;
import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.sc.project.Dao.ExamineeDao;
import com.sc.project.model.Examinee;
import com.sc.project.model.Page;
import com.sc.project.model.SystemSetting;
import com.sc.project.util.DB;
import com.sc.project.util.SerialNumber;
public class ExamineeDaoImpl implements ExamineeDao {
public List<Examinee> list(Page page) {
int pageSize = page.getPageSize();
int pageNow = page.getPageNow();
Connection conn = DB.createConn();
String sql = "select * from grxx order by id limit " + (pageNow*pageSize-pageSize)+","+pageSize;
PreparedStatement ps = DB.prepare(conn, sql);
List<Examinee> examinees = new ArrayList<Examinee>();
Examinee examinee = null;
ResultSet rs = null;
try {
rs = ps.executeQuery();
while(rs.next()){
examinee = new Examinee();
int id = rs.getInt("id");
String zkzNum = rs.getString("Zkzh");
String loginNum = rs.getString("Bmh");
String examineeName = rs.getString("Xm");
String sex = rs.getString("Xb");
String registerBoolean = rs.getString("Bmzt");
examinee.setId(id);
examinee.setExamineeName(examineeName);
examinee.setSex(sex);
examinee.setLoginNum(loginNum);
examinee.setZkzNum(zkzNum);
examinee.setRegisterBoolean(registerBoolean);
examinees.add(examinee);
}
} catch (SQLException e) {
e.printStackTrace();
}
finally{
DB.close(ps);
DB.close(conn);
}
return examinees;
}
//统计记录数和页数
public Page total() {
int totalNum = 0;
Connection conn = DB.createConn();
String sql = "select count(*) from grxx";
PreparedStatement ps = DB.prepare(conn, sql);
ResultSet rs = null;
try {
rs = ps.executeQuery();
while(rs.next()){
totalNum = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
Page page = new Page();
page.setPageSize(10);
int pageSize = page.getPageSize();
int lastPage = totalNum/pageSize;
if(totalNum%pageSize==0){
page.setLastPage(lastPage);
}
if(totalNum%pageSize!=0){
page.setLastPage(lastPage+1);
}
page.setTotalNum(totalNum);
page.setPageNow(1);
return page;
}
public Page firstPage(Page page){
page.setPageNow(1);
return page;
}
public Page lastPage(Page page){
page.setPageNow(page.getLastPage());
return page;
}
}
连接数据库的类
DB类:
package com.sc.project.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DB {
public static Connection createConn() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
String url="jdbc:mysql://localhost/djks";
String user="root";
String password="root";
try {
conn = DriverManager.getConnection(url,user,password);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static PreparedStatement prepare(Connection conn, String sql) {
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return ps;
}
public static void close(Connection conn) {
try {
conn.close();
conn = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(Statement stmt) {
try {
stmt.close();
stmt = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(ResultSet rs) {
try {
rs.close();
rs = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
}
由于是从一个项目中截出来的 所以有些是多余的 大家自己删改。 今天时间不多了 就写到这里。以后会陆续贴出我在这个项目中学到的东西。。。。。