[color=red]///分页的辅助类util///[/color]
package com.svse.util;
import java.util.ArrayList;
import java.util.List;
public class Page {
private int totalpage;// 总页数
private int totalcount;// 集合总行数
private int linesize;// 每页显示的条数
private int currentpage;// 当前页
// 获取显示该页显示的信息
public List getList(List findlist) {
List list = new ArrayList();
// 获取显示页的开始条数(当前页-1乘以每页显示条数)
int start = (this.getCurrentpage() - 1) * this.getLinesize();
// 获取显示页的结束条数,有两种情况
int end1 = this.getCurrentpage() * this.getLinesize();// 刚好整除
int end2 = (this.getCurrentpage() - 1) * this.getLinesize()
+ this.getTotalcount() % this.getLinesize();
// 判断当前页是第一页或是最后一页
if (this.getCurrentpage() == this.getTotalpage()
&& this.getTotalcount() % this.getLinesize() != 0) {
for (int i = start; i < end2; i++) {
list.add(findlist.get(i));
}
}
else
{
for (int i = start; i < end1; i++) {
list.add(findlist.get(i));
}
}
return list;
}
public Page() {
// TODO Auto-generated constructor stub
}
public Page(int currentpage, int linesize, int totalcount, int totalpage) {
super();
this.currentpage = currentpage;
this.linesize = linesize;
this.totalcount = totalcount;
this.totalpage = totalpage;
}
public int getTotalpage() {
return totalpage;
}
public void setTotalpage() {
// 设置总页数
if (this.getTotalcount() % this.getLinesize() == 0)// 总行数和每页数刚好整除
{
this.totalpage = this.getTotalcount() / this.getLinesize();
} else// 总行数和每页数不能被整除
{
this.totalpage = this.getTotalcount() / this.getLinesize() + 1;
}
}
public int getTotalcount() {
return totalcount;
}
public void setTotalcount(int totalcount) {
this.totalcount = totalcount;
}
public int getLinesize() {
return linesize;
}
public void setLinesize(int linesize) {
this.linesize = linesize;
}
public int getCurrentpage() {
return currentpage;
}
public void setCurrentpage(int currentpage) {
this.currentpage = currentpage;
}
}
[color=red]实体类entity/[/color]
package com.svse.entity;
public class User {
private String userid;
private String username;
private String useraddr;
public User() {
// TODO Auto-generated constructor stub
}
public User(String userid, String username, String useraddr) {
super();
this.useraddr = useraddr;
this.userid = userid;
this.username = username;
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUseraddr() {
return useraddr;
}
public void setUseraddr(String useraddr) {
this.useraddr = useraddr;
}
}
[color=red]/数据层dao//[/color]
package com.svse.dao;
import java.util.ArrayList;
import java.util.List;
import com.svse.entity.User;
public class UserDao {
//获取所有的User信息
public List finduserlist()
{
List userlist=new ArrayList();
userlist.add(new User("101","张三","湖北武汉"));
userlist.add(new User("102","李四","湖北黄冈"));
userlist.add(new User("103","王五","湖北襄樊"));
userlist.add(new User("104","赵六","湖北十堰"));
userlist.add(new User("105","许毅","湖北荆州"));
userlist.add(new User("106","风影","湖北黄石"));
userlist.add(new User("107","绝影","湖北孝感"));
userlist.add(new User("108","骄阳","湖北恩施"));
userlist.add(new User("109","佳倩","湖北宜昌"));
userlist.add(new User("110","默函","湖北咸宁"));
userlist.add(new User("111","谭斌","湖北沙市"));
userlist.add(new User("112","段强","湖北江宁"));
return userlist;
}
}
[color=red]//Action///[/color]
package com.svse.struts;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.svse.dao.UserDao;
import com.svse.util.Page;
public class UserAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
Page page=new Page();
UserDao udao=new UserDao();
List findlist=udao.finduserlist();//获取所有user的集合
//给page对象设值
page.setTotalcount(findlist.size());//总行数
page.setLinesize(3);//每页显示几条
page.setTotalpage();//总页数
//判断是第一次反问还是点击访问
int currentpage=0;
if(request.getParameter("pag")==null)
{
currentpage=1;
}
else
{
currentpage=Integer.parseInt(request.getParameter("pag"));
if(currentpage<1)
{
currentpage=1;//首页
}
if(currentpage>page.getTotalpage())
{
currentpage=page.getTotalpage();//末页
}
}
page.setCurrentpage(currentpage);//设置当前页 数
List userlist=page.getList(findlist);
request.setAttribute("userlist", userlist);
request.setAttribute("page", page);
return mapping.findForward("show");
}
}
[color=red]JSP显示页面/[/color]
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>userlist.jsp</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<center>
<table align="center" border="1" width="60%">
<tr>
<td>
编号
</td>
<td>
姓名
</td>
<td>
地址
</td>
</tr>
<logic:iterate id="user" name="userlist">
<tr>
<td>${user.userid }</td>
<td>${user.username }</td>
<td>${user.useraddr }</td>
</tr>
</logic:iterate>
</table>
<a href="user.do?pag=1">首页</a>
<a href="user.do?pag=${page.currentpage-1 }">上一页</a>
<a href="user.do?pag=${page.currentpage+1 }">下一页</a>
<a href="user.do?pag=${page.totalpage}">末页</a>
<br>
当前是第【${page.currentpage}】------总共:【${page.totalpage}】
</center>
</body>
</html:html>
package com.svse.util;
import java.util.ArrayList;
import java.util.List;
public class Page {
private int totalpage;// 总页数
private int totalcount;// 集合总行数
private int linesize;// 每页显示的条数
private int currentpage;// 当前页
// 获取显示该页显示的信息
public List getList(List findlist) {
List list = new ArrayList();
// 获取显示页的开始条数(当前页-1乘以每页显示条数)
int start = (this.getCurrentpage() - 1) * this.getLinesize();
// 获取显示页的结束条数,有两种情况
int end1 = this.getCurrentpage() * this.getLinesize();// 刚好整除
int end2 = (this.getCurrentpage() - 1) * this.getLinesize()
+ this.getTotalcount() % this.getLinesize();
// 判断当前页是第一页或是最后一页
if (this.getCurrentpage() == this.getTotalpage()
&& this.getTotalcount() % this.getLinesize() != 0) {
for (int i = start; i < end2; i++) {
list.add(findlist.get(i));
}
}
else
{
for (int i = start; i < end1; i++) {
list.add(findlist.get(i));
}
}
return list;
}
public Page() {
// TODO Auto-generated constructor stub
}
public Page(int currentpage, int linesize, int totalcount, int totalpage) {
super();
this.currentpage = currentpage;
this.linesize = linesize;
this.totalcount = totalcount;
this.totalpage = totalpage;
}
public int getTotalpage() {
return totalpage;
}
public void setTotalpage() {
// 设置总页数
if (this.getTotalcount() % this.getLinesize() == 0)// 总行数和每页数刚好整除
{
this.totalpage = this.getTotalcount() / this.getLinesize();
} else// 总行数和每页数不能被整除
{
this.totalpage = this.getTotalcount() / this.getLinesize() + 1;
}
}
public int getTotalcount() {
return totalcount;
}
public void setTotalcount(int totalcount) {
this.totalcount = totalcount;
}
public int getLinesize() {
return linesize;
}
public void setLinesize(int linesize) {
this.linesize = linesize;
}
public int getCurrentpage() {
return currentpage;
}
public void setCurrentpage(int currentpage) {
this.currentpage = currentpage;
}
}
[color=red]实体类entity/[/color]
package com.svse.entity;
public class User {
private String userid;
private String username;
private String useraddr;
public User() {
// TODO Auto-generated constructor stub
}
public User(String userid, String username, String useraddr) {
super();
this.useraddr = useraddr;
this.userid = userid;
this.username = username;
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUseraddr() {
return useraddr;
}
public void setUseraddr(String useraddr) {
this.useraddr = useraddr;
}
}
[color=red]/数据层dao//[/color]
package com.svse.dao;
import java.util.ArrayList;
import java.util.List;
import com.svse.entity.User;
public class UserDao {
//获取所有的User信息
public List finduserlist()
{
List userlist=new ArrayList();
userlist.add(new User("101","张三","湖北武汉"));
userlist.add(new User("102","李四","湖北黄冈"));
userlist.add(new User("103","王五","湖北襄樊"));
userlist.add(new User("104","赵六","湖北十堰"));
userlist.add(new User("105","许毅","湖北荆州"));
userlist.add(new User("106","风影","湖北黄石"));
userlist.add(new User("107","绝影","湖北孝感"));
userlist.add(new User("108","骄阳","湖北恩施"));
userlist.add(new User("109","佳倩","湖北宜昌"));
userlist.add(new User("110","默函","湖北咸宁"));
userlist.add(new User("111","谭斌","湖北沙市"));
userlist.add(new User("112","段强","湖北江宁"));
return userlist;
}
}
[color=red]//Action///[/color]
package com.svse.struts;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.svse.dao.UserDao;
import com.svse.util.Page;
public class UserAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
Page page=new Page();
UserDao udao=new UserDao();
List findlist=udao.finduserlist();//获取所有user的集合
//给page对象设值
page.setTotalcount(findlist.size());//总行数
page.setLinesize(3);//每页显示几条
page.setTotalpage();//总页数
//判断是第一次反问还是点击访问
int currentpage=0;
if(request.getParameter("pag")==null)
{
currentpage=1;
}
else
{
currentpage=Integer.parseInt(request.getParameter("pag"));
if(currentpage<1)
{
currentpage=1;//首页
}
if(currentpage>page.getTotalpage())
{
currentpage=page.getTotalpage();//末页
}
}
page.setCurrentpage(currentpage);//设置当前页 数
List userlist=page.getList(findlist);
request.setAttribute("userlist", userlist);
request.setAttribute("page", page);
return mapping.findForward("show");
}
}
[color=red]JSP显示页面/[/color]
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>userlist.jsp</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<center>
<table align="center" border="1" width="60%">
<tr>
<td>
编号
</td>
<td>
姓名
</td>
<td>
地址
</td>
</tr>
<logic:iterate id="user" name="userlist">
<tr>
<td>${user.userid }</td>
<td>${user.username }</td>
<td>${user.useraddr }</td>
</tr>
</logic:iterate>
</table>
<a href="user.do?pag=1">首页</a>
<a href="user.do?pag=${page.currentpage-1 }">上一页</a>
<a href="user.do?pag=${page.currentpage+1 }">下一页</a>
<a href="user.do?pag=${page.totalpage}">末页</a>
<br>
当前是第【${page.currentpage}】------总共:【${page.totalpage}】
</center>
</body>
</html:html>