首页上一页 1 下一页尾页 1 条记录 1/1页
医药管理系统:分页的错误。代码如下。谢谢
发表在Java图书答疑
2011-09-20
是否精华
是
否
版块置顶:
是
否
BsaeAction:
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; // 结果集
int iCurrPage = 1; // 当前页码
// 如果传递了页码则对当前页码赋值
if (currPage != null && !currPage.isEmpty()) {
iCurrPage = Integer.parseInt(currPage);
}
// 实例化SupperDao对象
SupperDao dao = new SupperDao();
int pages = 0; // 总页数
// 获取总记录数
Long l = (Long) dao.uniqueResult("select count(*) " + hql, where);
int count = l.intValue(); // 将总记录数转为int型
if (count > 0) {
// 计算总页数
if (count % recPerPage == 0) {
pages = count / recPerPage;
} else {
pages = count / recPerPage + 1;
}
if (iCurrPage > pages) {
iCurrPage = pages;
}
if (iCurrPage < 1) {
iCurrPage = 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")
+ count);
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(" ");
}
// 显示当前页码
pagingBar.append("<font color='red'>" + iCurrPage + "</font>");
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;
}
MedicineAction:
// 分页查询药品信息
public ActionForward paging(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// 获取页码
String currPage = request.getParameter("currPage");
// 构建action地址
String action = request.getContextPath() + "/baseData/med.do?command=paging";
// HQL查询语句
String hql = "from Medicine";
// 分页查询,返回Map对象
Map map = this.getPage(hql, recPerPage, currPage, action, null);
//将结果集放到request中
request.setAttribute("list", map.get("list"));
//将结果集放到分页条中
request.setAttribute("pagingBar", map.get("bar"));
return mapping.findForward("findAllSuccess");
}
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; // 结果集
int iCurrPage = 1; // 当前页码
// 如果传递了页码则对当前页码赋值
if (currPage != null && !currPage.isEmpty()) {
iCurrPage = Integer.parseInt(currPage);
}
// 实例化SupperDao对象
SupperDao dao = new SupperDao();
int pages = 0; // 总页数
// 获取总记录数
Long l = (Long) dao.uniqueResult("select count(*) " + hql, where);
int count = l.intValue(); // 将总记录数转为int型
if (count > 0) {
// 计算总页数
if (count % recPerPage == 0) {
pages = count / recPerPage;
} else {
pages = count / recPerPage + 1;
}
if (iCurrPage > pages) {
iCurrPage = pages;
}
if (iCurrPage < 1) {
iCurrPage = 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")
+ count);
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(" ");
}
// 显示当前页码
pagingBar.append("<font color='red'>" + iCurrPage + "</font>");
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;
}
MedicineAction:
// 分页查询药品信息
public ActionForward paging(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// 获取页码
String currPage = request.getParameter("currPage");
// 构建action地址
String action = request.getContextPath() + "/baseData/med.do?command=paging";
// HQL查询语句
String hql = "from Medicine";
// 分页查询,返回Map对象
Map map = this.getPage(hql, recPerPage, currPage, action, null);
//将结果集放到request中
request.setAttribute("list", map.get("list"));
//将结果集放到分页条中
request.setAttribute("pagingBar", map.get("bar"));
return mapping.findForward("findAllSuccess");
}