刚开始得到分页循环页数,但增加了跳转功能时,在select下拉框中总是重复循环最大页,通过查看代码修改,最终终于实现。解决思路就是需要增加一个跳转分页的javaBean,目的是封装成一个list,在得到页数后,在页面可以直接通过list的属性获取递增循环页数。
1.javaBean:
pageBean(分页类)
- public class PageBean {
- private List<实体类类名> list1; //返回某一页的记录列表
- private List<实体类类名> list2; //返回某一页的记录列表
- private int allRow; //总记录数
- private int pageSize; //每页记录数
- private int totalPage; //总页数
- private int currentPage; //当前页数
- private boolean isFirstPage; //是否为第一页
- private boolean isLastPage; //是否为最后一页
- private boolean hasPreviousPage; //是否有上一页
- private boolean hasNextPage; //是否有下一页
- /**
- * 初始化分页信息
- */
- public void init(){
- this.isFirstPage=isFirstPage();
- this.isLastPage=isLastPage();
- this.hasPreviousPage=isHasPreviousPage();
- this.hasNextPage=isHasNextPage();
- }
- /**
- * 判断分页信息,只需getter方法
- */
- public boolean isFirstPage(){
- return currentPage==1;
- }
- public boolean isLastPage(){
- return currentPage==totalPage;
- }
- public boolean isHasPreviousPage(){
- return currentPage!=1;
- }
- public boolean isHasNextPage(){
- return currentPage!=totalPage;
- }
- /**
- * 计算总页数,静态方法,供外部直接通过类名调用
- * @param pageSize 每页记录数
- * @param allRow 总记录数
- * @return 总页数
- */
- public static int countTotalPage(final int pageSize,final int allRow){
- int totalPage = allRow % pageSize == 0 ? allRow/pageSize : allRow/pageSize+1;
- return totalPage;
- }
- /**
- * 计算当前页开始记录
- * @parampageSize每页记录数
- * @paramcurrentPage当前第几页
- * @return当前页开始记录号
- */
- public static int countOffset(final int pageSize,final int currentPage){
- final int offset = pageSize*(currentPage-1);
- return offset;
- }
- /**
- * 计算当前页,若为0或者请求的URL中没有"?page=",则用1代替
- * @param page 传入的参数(可能为空,即0,则返回1)
- * @return 当前页
- */
- public static int countCurrentPage(int page){
- final int curPage = (page==0?1:page);
- return curPage;
- }
- public List<实体类类名> getList1() {
- return list1;
- }
- public void setList1(List<实体类类名> list1) {
- this.list1 = list1;
- }
- public List<实体类类名> getList2() {
- return list2;
- }
- public void setList2(List<实体类类名> list2) {
- this.list2 = list2;
- }
- public int getAllRow() {
- return allRow;
- }
- public void setAllRow(int allRow) {
- this.allRow = allRow;
- }
- public int getPageSize() {
- return pageSize;
- }
- public void setPageSize(int pageSize) {
- this.pageSize = pageSize;
- }
- public int getTotalPage() {
- return totalPage;
- }
- public void setTotalPage(int totalPage) {
- this.totalPage = totalPage;
- }
- public int getCurrentPage() {
- return currentPage;
- }
- public void setCurrentPage(int currentPage) {
- this.currentPage = currentPage;
- }
- }
pageNo(跳转时用到list属性封装类)
- public class PageNo {
- private int pageNumber; //跳转到第几页
- public int getPageNumber() {
- return pageNumber;
- }
- public void setPageNumber(int pageNumber) {
- this.pageNumber = pageNumber;
- }
- }
- @SuppressWarnings("serial")
- public class XxxAction extends ActionSupport implements Serializable {
- protected HttpSession getSession() {
- return ServletActionContext.getRequest().getSession();
- }
- private XxxService xxxService;
- public XxxService getXxxService() {
- return xxxService;
- }
- public void setXxxService(XxxService xxxService) {
- this.xxxService = xxxService;
- }
- private int page=1; //第几页
- private PageBean pageBean;
- public int getPage() {
- return page;
- }
- public void setPage(int page) {
- this.page = page;
- }
- public PageBean getPageBean() {
- return pageBean;
- }
- public void setPageBean(PageBean pageBean) {
- this.pageBean = pageBean;
- }
- private List<PageNo> numberList;
- private PageNo pageNo;
- public PageNo getPageNo() {
- return pageNo;
- }
- public void setPageNo(PageNo pageNo) {
- this.pageNo = pageNo;
- }
- /**
- * 列表并分页
- * @return
- */
- public String getListAndPage(){
- pageBean=xxxService.queryForPage(this,10,page);
- numberList=new ArrayList<PageNo>();
- if(pageBean.getTotalPage()>0){
- for(int i=0;i<pageBean.getTotalPage();i++){
- pageNo=new PageNo();
- pageNo.setPageNumber(i+1);
- numberList.add(i, pageNo);
- }
- }
- getSession().setAttribute("numberList", numberList);
- return "success";
- }
- }
4.daoImpl
- public class XxxDaoImpl extends BaseDAO implements XxxDao {
- /**
- * 列表并分页
- */
- @SuppressWarnings("unchecked")
- public PageBean queryForPage(XxxAction version,int pageSize,int page) {
- Session session=getSession();
- try{
- String sql=" from Entity as entity where 1=1 ";
- Query query1=session.createQuery(sql);
- Query query2=session.createQuery(sql);
- List list1 = query1.list();
- int allRow=list1.size(); //总记录数
- int totalPage=PageBean.countTotalPage(pageSize, allRow); //总页数
- final int length=pageSize; //每页记录数
- final int currentPage=PageBean.countCurrentPage(page); //当前页,不要为0或url为空
- final int offset=PageBean.countOffset(pageSize, page);
- query2.setFirstResult(offset);
- query2.setMaxResults(length);
- List<PaiPubVersion> list2=query2.list();
- //把分页信息保存到Bean中
- PageBean pageBean = new PageBean();
- pageBean.setPageSize(pageSize);
- pageBean.setCurrentPage(currentPage);
- pageBean.setAllRow(allRow);
- pageBean.setTotalPage(totalPage);
- pageBean.setList2(list2);
- pageBean.init();
- return pageBean;
- }catch(RuntimeException re){
- throw re;
- }finally{
- if(session!=null){
- session.close();
- }
- }
- }
- }
- <td colspan="6">
- 共<s:property value="pageBean.allRow"/> 条记录
- 共<s:property value="pageBean.totalPage"/> 页
- 当前第<s:property value="pageBean.currentPage"/>页
- <s:if test="%{pageBean.currentPage == 1}">
- <input type="button" value="第一页" disabled="disabled" />
- <input type="button" value="上一页" disabled="disabled" />
- </s:if>
- <s:else>
- <input type="button" value="第一页" onclick="pageNo('1')"/>
- <input type="button" value="上一页" onclick="pageNo('<s:property value="%{pageBean.currentPage-1}"/>')"/>
- </s:else>
- <s:if test="%{pageBean.currentPage != pageBean.totalPage}">
- <input type="button" value="下一页" onclick="pageNo('<s:property value="%{pageBean.currentPage+1}"/>')"/>
- <input type="button" value="最后一页" onclick="pageNo('<s:property value="%{pageBean.totalPage}"/>')"/>
- </s:if>
- <s:else>
- <input type="button" value="下一页" disabled="disabled" />
- <input type="button" value="最后一页" disabled="disabled" />
- </s:else>
- 跳转到:
- <select id="gotoNo" name="gotoNo" onchange="onTiao()">
- <s:iterator value="#session.numberList">
- <s:if test="pageBean.currentPage eq pageNumber">
- <option value="<s:property value="pageNumber"/>" selected="selected" ><s:property value="pageNumber"/></option>
- </s:if>
- <s:else>
- <option value="<s:property value="pageNumber"/>"><s:property value="pageNumber"/></option>
- </s:else>
- </s:iterator>
- </select>
- 页
- </td>
- function pageNo(fenye){
- document.pageform.action="getListAndPage.action?page="+fenye;
- document.pageform.submit();
- }
- function onTiao(){
- var gotoNo = document.getElementById('gotoNo').value;
- document.pageform.action="getListAndPage.action?page="+gotoNo;
- document.pageform.submit();
- }
7.页面显示效果