struts 2+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>