`

struts2+ibatis+mysql分页实现

阅读更多

把自己项目里的分页总结一下:

首先写了一个Pagination类

public class Pagination {

	/**
	 * 生成分页信息 包括第一页,上一页,下一页,最后一页等
	 * 
	 * @param pageNum
	 *            当前页数
	 * @param pageCount
	 *            总页数
	 * @param recordCount
	 *            总记录数
	 * @param pageUrl
	 *            页面URL
	 * @return
	 */
	public static String getPagination(int pageNum, int pageCount,
			int recordCount, String pageUrl) {

		if(pageCount <= 1){
			return "";
		}
		
		if (pageUrl == null || pageUrl.equals("")) {
			throw new RuntimeException("pageUrl is null or blank String");
		}
		
		String url = pageUrl.contains("?") ? pageUrl : pageUrl + "?";
	
		StringBuffer buffer = new StringBuffer();

		buffer.append("第  " + pageNum + "/" + pageCount + " 页  共  "
				+ recordCount + " 条记录  ");
		buffer.append(pageNum == 1 ? "第一页" : "<a href='" + url
				+ "&pageNum=1'>第一页</a>");
		buffer.append("&nbsp;&nbsp;");
		buffer.append(pageNum == 1 ? "上一页" : "<a href='" + url + "&pageNum="
				+ (pageNum - 1) + "'>上一页</a>");
		buffer.append("&nbsp;&nbsp;");
		buffer.append(pageNum == pageCount ? "下一页" : "<a href='" + url
				+ "&pageNum=" + (pageNum + 1) + "'>下一页</a>");
		buffer.append("&nbsp;&nbsp;");
		buffer.append(pageNum == pageCount ? "最后一页" : "<a href='" + url
				+ "&pageNum=" + pageCount + "'>最后一页</a>");
		buffer.append("&nbsp;&nbsp;&nbsp;&nbsp;");
		buffer
				.append("到<input type='text' name='goto_input' style='width:25px;font-size:12px;text-align:center' />页");
		buffer
				.append("<input type='button' name='goto_button' style='width:25px;font-size:12px' value='Go' />");

		buffer.append("<script type='text/javascript'>");
		buffer.append("    function helloweenvsfei_enter(){");

		buffer.append("    	  if(event.keyCode == 13){");
		buffer.append("    		  helloweenvsfei_goto();");
		buffer.append("    		  return false;");
		buffer.append("       }");
		buffer.append("    	  return true;");
		buffer.append("    }");

		buffer.append("function helloweenvsfei_goto(){");
		buffer.append("    var numText = document.getElementsByName('goto_input')[0].value;");
		buffer.append("    var num = parseInt(numText, 10);");
		buffer.append("    if(!num){");
		buffer.append("        alert('页数必须是数字');");
		buffer.append("        return;");
		buffer.append("    }");

		buffer.append("    if(num < 1 || num >" + pageCount + "){");
		buffer.append("        alert('页数必须大于0,且小于总页数" + pageCount + "');");
		buffer.append("        return;");
		buffer.append("    }");
		buffer.append("    location ='" + url + "&pageNum='+num;");
		buffer.append("}");

		buffer.append("document.getElementsByName('goto_input')[0].onkeypress = helloweenvsfei_enter;");
		buffer.append("document.getElementsByName('goto_button')[0].onclick = helloweenvsfei_goto;");
		
		buffer.append("</script>");
		
		return buffer.toString();
	}

}

 在action中定义变量,生成get,set方法

 

private int pageNum = 1;
	private String pagination = null;
	public int getPageNum() {
		return pageNum;
	}
	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}
	public String getPagination() {
		return pagination;
	}
	public void setPagination(String pagination) {
		this.pagination = pagination;
	}

 主体类

public String list(){
		authNews=new AuthNews();
		authNews.setType(Constants.PRODUCT_FILE_TYPE[0]);

		int recordCount = authNewsService.getCount(authNews);
		PageBean pageBean = new PageBean(pageNum, 2, recordCount);

		authNews.setPageBean(pageBean);
		try {
			listAuthNews=authNewsService.getAuthNewsByType(authNews);
		} catch (Exception e) {
			e.printStackTrace();
		}
		HttpServletRequest request = ServletActionContext.getRequest();
		String pageUrl = request.getContextPath()
				+ "/problemListAction.html";
		pagination = Pagination.getPagination(pageNum, authNews
				.getPageBean().getPageCount(), recordCount, pageUrl);
		
		return "list";
	}
 

ibatis中的配置文件

  <select id="getAuthNewsCount" resultClass="int" parameterClass="com.byd.bqs.model.AuthNews" >
    select count(*) from auth_news  where type = #type# order by publishdate desc
     
  </select>

 <select id="getAuthNewsByType" resultClass="com.byd.bqs.model.AuthNews" parameterClass="com.byd.bqs.model.AuthNews" >
    select Id, type, title, publishDate, author, content
    from auth_news
    where type = #type#
     limit #pageBean.beginRecord#,#pageBean.pageSize#
  </select>
 
2
2
分享到:
评论
5 楼 苍天百合 2013-03-21  
problemListAction.html 都没有写出来 怎么学
4 楼 flyfx 2012-04-23  
楼主例子看看行不?
qq:704887853@qq.com
3 楼 sijiesi 2011-10-25  
求完整代码阿、求项目。求例子啊。楼主~拜托
1516188@qq.com~~~~~~~~~~~~~~~~~~~~~~~
2 楼 sijiesi 2011-10-25  
求完整代码阿、求项目。求例子啊。楼主~拜托
1516188@qq.com~~~~~~~~~~~~~~~~~~~~~~~
1 楼 ceoajun 2011-08-30  
楼主求分页完整源码,我的邮箱ceoajun@sina.cn 谢谢

相关推荐

Global site tag (gtag.js) - Google Analytics