1.jsp页面
<table id="tablelist" class="table table-striped table-bordered table-hover"> <thead> <tr> <th>通知类型</th> <th>内容</th> <th>通知时间</th> <th>是否已读</th> </tr> <c:forEach items="${pager.lists}" var="item"> <tr> <td>${item.inform.classify}</td> <td>${item.texts}</td> <td>${item.strNotTime}</td> <td> <c:if test="${item.readss==0}"> 未读 </c:if> <c:if test="${item.readss==1}"> 已读 </c:if> </td> </tr> </c:forEach> </thead> <tbody id="fora"> </tbody> </table> <style type="text/css"> a { color: #555555; text-decoration: none; cursor: pointer; } .pages a.p_pre:hover { background-color: #eaeaea; color: #555555; border: 1px solid #cccccc; } .pages a { height: 36px; line-height: 36px; overflow: hidden; color: #666666; font-size: 16px; text-align: center; display: inline-block; padding: 0 12px; margin: 0 4px; border: 1px solid #cccccc; -webkit-border-radius: 2px; -moz-border-radius: 2px; border-radius: 2px; } .pages a:hover, .pages a.cur { color: #FFF; background-color: #DD4E62; border: 1px solid #DD4E62; } </style> <div class="pages"> <c:if test="${pager.totalPage>=1}"> <a href="${pageContext.request.contextPath}${pager.url}&pageIndex=1" class="p_pre">首页</a> <c:if test="${pager.pageIndex>1}"> <a href="${pageContext.request.contextPath}${pager.url}&pageIndex=${pager.pageIndex-1}" class="p_pre">上一页</a> </c:if> <c:forEach var="temp" begin="${pager.pageIndex>3?pager.pageIndex-3:1}" end="${pager.totalPage-pager.pageIndex>3?pager.pageIndex+3:pager.totalPage}" step="1"> <c:if test="${pager.pageIndex==temp}"> <a href="${pageContext.request.contextPath}${pager.url}&pageIndex=${temp}" class="cur">${temp}</a> </c:if> <c:if test="${pager.pageIndex!=temp}"> <a href="${pageContext.request.contextPath}${pager.url}&pageIndex=${temp}">${temp}</a> </c:if> </c:forEach> <c:if test="${pager.pageIndex<pager.totalPage}"> <a href="${pageContext.request.contextPath}${pager.url}&pageIndex=${pager.pageIndex+1}" class="p_pre">下一页</a> </c:if> <a href="${pageContext.request.contextPath}${pager.url}&pageIndex=${pager.totalPage}" class="p_pre">尾页</a> </c:if> <c:if test="${pager.totalPage==0}"> <a href="#" class="cur">暂无记录</a> </c:if> </div>
需要注意记得引入el和jstl
isELIgnored="false" <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
2.bin的注入
<bean id="noticepage" scope="prototype" class="com.dawn.action.AdminNoticeAction"> <property name="noticeDAO" ref="noticeDAO" /> </bean>
3.struts2的配置
<action name="noticepage" class="noticepage" method="noticepage"> <result name="success">note.jsp</result> <result name="login" type="redirect">login.jsp</result> </action>
4.struts action的书写
package com.dawn.action; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import org.apache.struts2.interceptor.SessionAware; import com.opensymphony.xwork2.ActionSupport; import com.dawn.dao.NoticeDAO; import com.dawn.model.Admin; import com.dawn.model.Notice; import com.dawn.util.PagerInfo; public class AdminNoticeAction extends ActionSupport{ private NoticeDAO noticeDAO; private Map<String, Object> session; private Integer pageIndex; private Integer id; /** * @return the noticeDAO */ public NoticeDAO getNoticeDAO() { return noticeDAO; } /** * @param noticeDAO the noticeDAO to set */ public void setNoticeDAO(NoticeDAO noticeDAO) { this.noticeDAO = noticeDAO; } /** * @return the pageIndex */ public Integer getPageIndex() { return pageIndex; } /** * @param pageIndex the pageIndex to set */ public void setPageIndex(Integer pageIndex) { this.pageIndex = pageIndex; } public String noticepage(){ HttpServletRequest request = ServletActionContext.getRequest(); HttpSession session = request.getSession(); Admin admin = (Admin)session.getAttribute("admin"); //设置当前页码,第一次进入为第一页 int index2=pageIndex==null?1:pageIndex; //创建分页工具类 PagerInfo<Notice> pager=new PagerInfo<Notice>(); //保存当前页码 pager.setPageIndex(index2); //保存每页行数 pager.setPageSize(5); //给登陆的人的id if(admin!=null&&admin.getId()!=0&&admin.getId()!=null){ id=admin.getId(); } //dao层查询数据集合 List<Notice> queryAll = noticeDAO.queryAll(pager, id); //给pager扔查询出来的集合 pager.setLists(queryAll); //获取总数 Integer totalSize = noticeDAO.pageAll(id); //保存到page里 pager.setTotalSize(totalSize); pager.setUrl("/noticepage.action?id="+id); session.setAttribute("pager", pager); return SUCCESS; } }