正在阅读:解决MVC下分页显示的问题解决MVC下分页显示的问题

2005-07-12 10:07 出处: 作者:ponky 责任编辑:moningfeng

  由于Action里面用到了查询的SQL语句,所有SQL语句写在一个特定的类中,这个类名定义为SQLBook,代码如下:

public class SQLBook {

public SQLBook(){}
/**
* computer sql
*/

public static final String Computer_select_SQL=
"select a.id,a.bookname,a.bookclass,b.classname,"+
"a.author,a.publish,a.bookno,a.content,a.prince,a.amount,"+
"a.Leav_number,a.regtime,a.picture from book a,bookclass b"+
" where a.Bookclass = b.Id and a.bookclass=? "+
" order by a.Id desc ";
public static final String Computer_select_count_sql=
"select count(*) from book a,bookclass b"+
" where a.Bookclass = b.Id and a.bookclass=? "+
" order by a.Id desc ";

}


  到此为止,基本上分页的代码基本完成,为了使得分页的代码共用,我把他封装成了一个标签,这个自定义的标签命名为PaginatorTag,代码如下:

package com.util;

import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class PaginatorTag extends BodyTagSupport {
protected Log log = LogFactory.getLog(this.getClass());
//以下是一标签中的一些属性,后面有较详细的介绍
int currentPage = 1;//当前页码
String url = "";//转向的地址
int totalSize = 0;//总的记录数
int perPage = 20;//每页显示的记录数目
boolean showTotal = true;//是否显示总数量
boolean showAllPages = false;//是否显示总页码
String strUnit ="";//计数单位

//得到当前页码
public int getCurrentPage() {
return currentPage;
}
//设置当前页码
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
//得到每页显示记录的数目
public int getMaxPerPage() {
return perPage;
}
//设置每页显示的记录数目
public void setMaxPerPage(int perPage) {
this.perPage = perPage;
}
//判断是否显示总的页码数目
public boolean isShowAllPages() {
return showAllPages;
}
//设置是否显示总的页码数目
public void setShowAllPages(boolean showAllPages) {
this.showAllPages = showAllPages;
}
//判断是否显示总的记录数目
public boolean isShowTotal() {
return showTotal;
}
//设置是否显示总的记录数目
public void setShowTotal(boolean showTotal) {
this.showTotal = showTotal;
}
//得到计数单位
public String getStrUnit() {
return strUnit;
}
//设置计数单位
public void setStrUnit(String strUnit) {
this.strUnit = strUnit;
}
//得到总的记录数目
public int getTotalPut() {
return totalSize;
}
//设置总的记录数目
public void setTotalPut(int totalSize) {
this.totalSize = totalSize;
}
//得到转向的链接地址
public String getUrl() {
return url;
}
//设置链接地址
public void setUrl(String url) {
this.url = url;
}
public int doStartTag() throws JspException {
return SKIP_BODY;
}


public int doEndTag() throws JspException {
String out = showPage(currentPage, url, totalSize, perPage, showTotal, showAllPages, strUnit);
try {
pageContext.getOut().print(out);
} catch (IOException e) {
e.printStackTrace();
}
return EVAL_PAGE;
}


/**
* 作 用:显示“上一页 下一页”等信息
*
* @param url
* ----链接地址
* @ param totalSize
* ----总数量
* @ param perPage
* ----每页数量
* @param showTotal
* ----是否显示总数量
* @param showAllPages
* ---是否用下拉列表显示所有页面以供跳转。有某些页面不能使用,否则会出现JS错误。
* @param strUnit
* ----计数单位
* @return .
* @ throws IOException
*/
protected String showPage(int currentPage,String url, int totalSize, int perPage,

boolean showTotal, boolean showAllPages, String strUnit){

int n = 0;

StringBuffer buf = new StringBuffer();

String strUrl;

n = (totalSize + perPage -1) / perPage;

buf.append("<table align='center'><tr><td>");

if (showTotal == true)

buf.append("共 <b>" + totalSize + "</b> " + strUnit

+ " ");

strUrl = JoinChar(url);

if (currentPage < 2) {

buf.append("首页 上一页 ");

} else {

buf.append("<a href='" + strUrl + "pageNo=1' title='首页'>首页</a>
");

buf.append("<a href='" + strUrl + "pageNo=" + (currentPage
- 1)

+ "' title='上一页'>上一页</a> ");

}

if (n - currentPage < 1)

buf.append("下一页 尾页");

else {

buf.append("<a href='" + strUrl + "pageNo=" + (currentPage
+ 1)

+ "' title='下一页'>下一页</a> ");

buf.append("<a href='" + strUrl + "pageNo=" + n + "'
title='尾页'>尾页</a>");

}

buf.append(" 页次:<strong><font color=red>" + currentPage

+ "</font>/" + n + "</strong>页 ");

buf.append(" <b>" + perPage + "</b>" + strUnit
+ "/页");

if (showAllPages == true) {

buf

.append(" 转到:<select name='page' size='1' onchange=\"javascript:window.location='"

+ strUrl

+ "pageNo="

+ "'+this.options[this.selectedIndex].value;\">");

for (int i = 1; i <= n; i++) {

buf.append("<option value='" + i + "'");

if(currentPage == i)

buf.append(" selected ");

buf.append(">第" + i + "页</option>");

}

buf.append("</select>");

}

buf.append("</td></tr></table>");

return (buf.toString());

}


/**

* 向地址中加入 ? 或 &

* @param strUrl

* ----网址.

* @return 加了 ? 或 & 的网址.

*/

protected String JoinChar(String strUrl) {

String result = "";

if (strUrl.equals("") || strUrl.length() <= 0) {

return result;

}

if (strUrl.indexOf("?") < strUrl.length()) {

if (strUrl.indexOf("?") > -1) {

if (strUrl.indexOf("&") < strUrl.length()) {

result = strUrl + "&";

} else {

result = strUrl;

}

} else {

result = strUrl + "?";

}

} else {

result = strUrl;

}

return result;

}
}

键盘也能翻页,试试“← →”键

关注我们

最新资讯离线随时看 聊天吐槽赢奖品