- public class SplitPage {
- // 声明一些常量
- final public static String FIRSTPAGE = "first";// 请求的是首页
- final public static String PREVIOUSPAGE = "previous";// 请求上一页
- final public static String NEXTPAGE = "next";// 请求下一页
- final public static String LASTPAGE = "last";// 请求最后一页
- // 声明一些变量
- private int pageRow = 3;// 每页显示记录数
- private int totalRow = 0;// 总的记录数,有数据库操作类DAO提供
- private int currentPage = 1;// 当前的页面
- private int firstPage = 1;// 首页位置
- private int totalPage = 1;// 总的页面数,默认为一页
- public int getPageRow() {
- return pageRow;
- }
- /**
- * 重新设置每页显示的记录数
- *
- * @param pageRow
- * 新的每页显示记录数
- */
- public void setPageRow(int pageRow) {
- if (pageRow == 0) {
- try {
- throw new Exception();// 如果pageRow被设置为零,应当抛出异常.
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- this.pageRow = pageRow;// 修改每页的记录数
- this.totalPage = this.totalRow / this.pageRow
- + ((this.totalRow % this.pageRow == 0) ? 0 : 1);
- }
- public int getTotalRow() {
- return totalRow;
- }
- public void setTotalRow(int totalRow) {// 设置分页对象的总记录属性后,就应该根据每页面显示记录数,计算得到总的页面数
- this.totalRow = totalRow;
- this.totalPage = this.totalRow/this.pageRow+((this.totalRow%this.pageRow==0)?0:1);
- System.out.println("当前页"+this.currentPage);
- }
- public int getCurrentPage() {
- return currentPage;
- }
- public void setCurrentPage(int currentPage) {
- this.currentPage = currentPage;
- }
- public int getFirstPage() {
- return firstPage;
- }
- public void setFirstPage(int firstPage) {
- this.firstPage = firstPage;
- }
- public int getTotalPage() {
- return totalPage;
- }
- //不应该提供方法设置总页面数,它是计算得到的
- /**
- * 根据请求的标示符参数重新计算要现实的页面
- *
- * @param flag
- * 请求转向的页面标示符
- * @return int 返回新页
- */
- public int toNewPage(String flag) {
- int newPage = this.currentPage;
- if (flag != null && !"".equals(flag)) {
- if (SplitPage.FIRSTPAGE.equals(flag)) {
- newPage = 1;
- } else if (SplitPage.LASTPAGE.equals(flag)) {
- newPage = this.totalPage;
- } else if (SplitPage.NEXTPAGE.equals(flag)) {
- newPage = this.currentPage
- + ((this.currentPage == this.totalPage) ? 0 : 1);// 如果当前页面与总的页面数相等则不再向后(+1)
- } else if (SplitPage.PREVIOUSPAGE.equals(flag)) {
- newPage = this.currentPage
- - ((this.currentPage == this.firstPage) ? 0 : 1);// 如果当前页面与首页相等则不再向前(-1)
- } else {
- // 传入的是个数字字符串参数
- newPage = Integer.parseInt(flag.trim());
- }
- } else {// 请求的参数为空,则当前页码不变
- newPage = this.currentPage;
- }
- this.setCurrentPage(newPage);// 记得重新设置当期页面
- return newPage;
- }
- }
- public interface PageInterface {
- /**
- * 查询所有的记录,调用分页生成器类中的分页方法查询数据
- *
- * @param splitPage
- * 分页对象
- * @return List<Object>
- * */
- public List<UserInfoVo> findUserAll(SplitPage splitPage);//查询用户列表
- /**
- * 查询所有的记录,调用分页生成器类中的分页方法查询数据
- *
- * @param splitPage
public interface PageInterface {
/**
* 查询所有的记录,调用分页生成器类中的分页方法查询数据
*
* @param splitPage
* 分页对象
* @return List<Object>
* */
public List<UserInfoVo> findUserAll(SplitPage splitPage);//查询用户列表
/**
* 查询所有的记录,调用分页生成器类中的分页方法查询数据
*
* @param splitPage
- * 分页对象
- * @return List<Object>
- * */
- public List<ForumInfoVo> findForumAll(SplitPage splitPage);//查询帖子
- /**
- * 提供总的记录数
- * */
- public int getTotalRows();
- /**
- * 查询所有的记录,调用分页生成器类中的分页方法查询数据
- *
- * @param splitPage
- * 分页对象
- * @return List<Object>
- * */
- public List<ReforumInfoVo> findReforumAll(SplitPage splitPage);
- }
- <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
- <%@taglib prefix="c" uri="http://java.sun.com/jsf/core"%>
- <%@page import="com.weiyi.bbs.dao.ForumInfoDAO"%>
- <%@page import="com.weiyi.bbs.util.*"%>
- <%@page import="com.weiyi.bbs.domain.*"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme() + "://"
- + request.getServerName() + ":" + request.getServerPort()
- + path + "/";
- %>
- <!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">
- <script type="text/javascript" src="js/common.js" charset="utf-8"></script>
- <script type="text/javascript" charset="utf-8">
- function goPage(){
- var v = document.getElementById("selectPage").value;
- var u = document.getElementById("userId").value;
- window.location.href="forumList.jsp?flag="+v+"&userId="+u;
- }
- </script>
- </head>
- <!--spage分页对象,保存分页的详细信息 ,存在Session中,每次查询或者是分页数据时只要设置此对象的当前页 -->
- <!-- dao帖子操作类,主要用来获取总记录数-->
- <jsp:useBean id="spage" class="com.weiyi.bbs.util.SplitPage"
- scope="session"></jsp:useBean>
- <jsp:useBean id="dao" class="com.weiyi.bbs.dao.ForumInfoDAO"
- scope="session"></jsp:useBean>
- <%
- String flag = request.getParameter("flag");
- int newPage = spage.toNewPage(flag.trim());
- dao.setTotalRows(dao.getAllRecord());
- int totalRows = dao.getTotalRows();
- spage.setTotalRow(totalRows);
- %>
- <body>
- <div id="wrap">
- <h3>
- 查看帖子列表
- </h3>
- <input type="hidden" name="userId" id="userId" value="<%=request.getParameter("userId")%>"/>
- <a href="destroyservlet">退出</a>
- <a
- href="forumcontroller?type=toNewForum&userId=<%=request.getParameter("userId")%>&power=user">发新帖</a>
- <table border="border">
- <thead>
- <tr>
- <th>
- ID
- </th>
- <th>
- 标题
- </th>
- <th>
- 发帖人
- </th>
- <th>
- 发帖时间
- </th>
- <th>
- 操作
- </th>
- </tr>
- </thead>
- <tbody>
- <%
- List<ForumInfoVo> list = dao.findForumAll(spage);
- for (ForumInfoVo forum : list) {
- %>
- <tr>
- <td><%=forum.getId()%></td>
- <td><%=forum.getTitle()%></td>
- <td><%=forum.getAuthorName()%></td>
- <td><%=forum.getCreateTime()%></td>
- <td>
- <a
- href="forumcontroller?type=look&power=user&userId=<%=request.getParameter("userId")%>&id=<%=forum.getId()%>">查看</a>
- </td>
- </tr>
- <%
- }
- %>
- </tbody>
- </table>
- <div id="foot">
- <a href="forumList.jsp?flag=<%=SplitPage.FIRSTPAGE%>&userId=<%=request.getParameter("userId")%>">首页</a>
- <a href="forumList.jsp?flag=<%=SplitPage.PREVIOUSPAGE%>&userId=<%=request.getParameter("userId")%>">上一页</a>
- <a href="forumList.jsp?flag=<%=SplitPage.NEXTPAGE%>&userId=<%=request.getParameter("userId")%>">下一页</a>
- <a href="forumList.jsp?flag=<%=SplitPage.LASTPAGE%>&userId=<%=request.getParameter("userId")%>">末页</a>
- <select id="selectPage" name="selectPage" οnchange="goPage();">
- <%
- for (int i = 1; i <= spage.getTotalPage(); ++i) {
- %>
- <option value="<%=i%>"
- <%=(spage.getCurrentPage() == i) ? "selected='selected'"
- : ""%> ><%=i%></option>
- <%
- }
- %>
- </select>
- <label><%=spage.getCurrentPage()%>/<%=spage.getTotalPage()%>页
- </label>
- </div>
- </div>
- </body>
- </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsf/core"%>
<%@page import="com.weiyi.bbs.dao.ForumInfoDAO"%>
<%@page import="com.weiyi.bbs.util.*"%>
<%@page import="com.weiyi.bbs.domain.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!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">
<script type="text/javascript" src="js/common.js" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
function goPage(){
var v = document.getElementById("selectPage").value;
var u = document.getElementById("userId").value;
window.location.href="forumList.jsp?flag="+v+"&userId="+u;
}
</script>
</head>
<!--spage分页对象,保存分页的详细信息 ,存在Session中,每次查询或者是分页数据时只要设置此对象的当前页 -->
<!-- dao帖子操作类,主要用来获取总记录数-->
<jsp:useBean id="spage" class="com.weiyi.bbs.util.SplitPage"
scope="session"></jsp:useBean>
<jsp:useBean id="dao" class="com.weiyi.bbs.dao.ForumInfoDAO"
scope="session"></jsp:useBean>
<%
String flag = request.getParameter("flag");
int newPage = spage.toNewPage(flag.trim());
dao.setTotalRows(dao.getAllRecord());
int totalRows = dao.getTotalRows();
spage.setTotalRow(totalRows);
%>
<body>
<div id="wrap">
<h3>
查看帖子列表
</h3>
<input type="hidden" name="userId" id="userId" value="<%=request.getParameter("userId")%>"/>
<a href="destroyservlet">退出</a>
<a
href="forumcontroller?type=toNewForum&userId=<%=request.getParameter("userId")%>&power=user">发新帖</a>
<table border="border">
<thead>
<tr>
<th>
ID
</th>
<th>
标题
</th>
<th>
发帖人
</th>
<th>
发帖时间
</th>
<th>
操作
</th>
</tr>
</thead>
<tbody>
<%
List<ForumInfoVo> list = dao.findForumAll(spage);
for (ForumInfoVo forum : list) {
%>
<tr>
<td><%=forum.getId()%></td>
<td><%=forum.getTitle()%></td>
<td><%=forum.getAuthorName()%></td>
<td><%=forum.getCreateTime()%></td>
<td>
<a
href="forumcontroller?type=look&power=user&userId=<%=request.getParameter("userId")%>&id=<%=forum.getId()%>">查看</a>
</td>
</tr>
<%
}
%>
</tbody>
</table>
<div id="foot">
<a href="forumList.jsp?flag=<%=SplitPage.FIRSTPAGE%>&userId=<%=request.getParameter("userId")%>">首页</a>
<a href="forumList.jsp?flag=<%=SplitPage.PREVIOUSPAGE%>&userId=<%=request.getParameter("userId")%>">上一页</a>
<a href="forumList.jsp?flag=<%=SplitPage.NEXTPAGE%>&userId=<%=request.getParameter("userId")%>">下一页</a>
<a href="forumList.jsp?flag=<%=SplitPage.LASTPAGE%>&userId=<%=request.getParameter("userId")%>">末页</a>
<select id="selectPage" name="selectPage" οnchange="goPage();">
<%
for (int i = 1; i <= spage.getTotalPage(); ++i) {
%>
<option value="<%=i%>"
<%=(spage.getCurrentPage() == i) ? "selected='selected'"
: ""%> ><%=i%></option>
<%
}
%>
</select>
<label><%=spage.getCurrentPage()%>/<%=spage.getTotalPage()%>页
</label>
</div>
</div>
</body>
</html>