81,092
社区成员
发帖
与我相关
我的任务
分享
protected int recPerPage = 10; // 分页中每页的记录数
protected Locale locale = null; // 本地语言信息
protected MessageResources message = null;// 消息资源
// 页容量
//private int pageSize = 10;
// 总页数
private int pages;
// 总记录数
private int allCount;
// 开始页码
private int startPage = 1;
// 结束页码
private int endPage;
private int iCurrPage;//当前页
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// 获取Locale信息
this.locale = this.getLocale(request);
// 获取消息资源对象
this.message = this.getResources(request);
// 如果用户没有登录,跳转到登录页面
//if (request.getSession().getAttribute("user") == null) {
//return mapping.findForward("login");
//}
return super.execute(mapping, form, request, response);
}
/**
* 分页
* @param hql hql语句(不包含select,从from子句开始)
* @param recPerPage 每页的记录数
* @param currPage 当前页码
* @param action 请求提交的action地址
* @param where 条件数组
* @return Map集合(装载结果集对象及分页条)
*/
public Map getPage(String hql, int recPerPage, String currPage,
String action, Object[] where) {
// 实例化一个Map对象
Map map = new HashMap();
// 分页条
StringBuffer pagingBar = new StringBuffer();
List list = null; // 结果集
iCurrPage = 1; // 当前页码
// 如果传递了页码则对当前页码赋值
if (currPage != null && !currPage.isEmpty()) {
iCurrPage = Integer.parseInt(currPage);
}
// 实例化SupperDao对象
SupperDao dao = new SupperDao();
pages = 0; // 总页数
// 获取总记录数
Long l = (Long) dao.uniqueResult("select count(*) " + hql, where);
allCount = l.intValue(); // 将总记录数转为int型
if (allCount > 0) {
// 计算总页数
if (allCount % recPerPage == 0) {
pages = allCount / recPerPage;
} else {
pages = allCount / recPerPage + 1;
}
if (iCurrPage > pages) {
iCurrPage = pages;
}
if (iCurrPage < 1) {
iCurrPage = 1;
}
// 显示页码计算
if (iCurrPage > 0) {
startPage = iCurrPage;
endPage = iCurrPage+9;
}
if (endPage > pages) {
if (pages>10)
startPage = pages - 9;
else
startPage = 1;
endPage = pages;
}
if (startPage < 1) {
startPage = 1;
}
// 分页查询获取结果集
list = dao.findPaging(hql, (iCurrPage - 1) * recPerPage,
recPerPage, where);
// 构造分页条
pagingBar.append("<form name='pagingForm' action='" + action
+ "' method='post'>");
// 在分页条中添加总记录数
pagingBar.append(message.getMessage(locale, "page.totalRecord")
+ allCount);
pagingBar.append(" ");
pagingBar.append(message.getMessage(locale, "system.total") + " "
+ pages + " " + message.getMessage(locale, "page.page"));
pagingBar.append(" ");
// 页数大于1显示上一页超链接,否则不显示超链接
if (iCurrPage > 1) {
pagingBar.append("<a href=" + action + "&currPage=1>"
+ message.getMessage(locale, "page.first") + "</a>");
pagingBar.append(" ");
pagingBar.append("<a href=" + action + "&currPage="
+ (iCurrPage - 1) + ">"
+ message.getMessage(locale, "page.previous") + "</a>");
pagingBar.append(" ");
} else {
pagingBar.append(message.getMessage(locale, "page.first"));
pagingBar.append(" ");
pagingBar.append(message.getMessage(locale, "page.previous"));
pagingBar.append(" ");
}
//显示数字页码,显示不出来,不知为什么?
for(int i=startPage; i<=endPage; i++){
if(i==iCurrPage){
pagingBar.append("<font color='red'>" + iCurrPage + "</font>");
pagingBar.append(" ");
}else{
pagingBar.append("<a href=" + action + "&currPage=" + i + ">"+i+"</a> ");
pagingBar.append(" ");
}
}
// 显示当前页码
// 页数小于总页数显示下一页超链接,否则不显示超链接
if (iCurrPage < pages) {
pagingBar.append("<a href=" + action + "&currPage="
+ (iCurrPage + 1) + ">"
+ message.getMessage(locale, "page.next") + "</a>");
pagingBar.append(" ");
pagingBar.append("<a href=" + action + "&currPage=" + pages
+ ">" + message.getMessage(locale, "page.last")
+ "</a>");
} else {
pagingBar.append(message.getMessage(locale, "page.next"));
pagingBar.append(" ");
pagingBar.append(message.getMessage(locale, "page.last"));
}
pagingBar.append(" ");
pagingBar.append("<input type='text' name='currPage' size='1'>");
pagingBar.append("<input type='submit' value='GO'>");
pagingBar.append("</form>");
}
map.put("list", list);// 结果集
map.put("bar", pagingBar.toString());// 分页条的字符串形式
return map;
}
}
startPage=iCurrPage>4?iCurrPage+5>pages?pages>10?pages-9:1:iCurrPage+4:1;
endPage=iCurrPage>4?iCurrPage+5>pages?pages:iCurrPage+5:pages>10?10:pages;
哪位高手能解释一下这段代码的意思?谢谢!!把 // 显示页码计算
if (iCurrPage > 0) {
startPage = iCurrPage;
endPage = iCurrPage+9;
}
if (endPage > pages) {
if (pages>10)
startPage = pages - 9;
else
startPage = 1;
endPage = pages;
}
if (startPage < 1) {
startPage = 1;
}
替换成
startPage=iCurrPage>9?iCurrPage+1>pages?pages>10?pages-9:1:iCurrPage+1:1;
endPage=iCurrPage>9?iCurrPage+1>pages?pages:iCurrPage+10:pages>10?10:pages;
然后修改成你要的效果