当前位置: 代码迷 >> Java Web开发 >> 请问struts分页有关问题
  详细解决方案

请问struts分页有关问题

热度:112   发布时间:2016-04-16 21:28:12.0
请教struts分页问题
public class BaseAction extends DispatchAction {

protected int recPerPage = 3; // 分页中每页的记录数
protected Locale locale = null; // 本地语言信息
protected MessageResources message = null;// 消息资源

@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; // 结果集
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;
}
}

运行结果:
第1页显示
首页 上一页 1 下一页 尾页
第2页显示
首页 上一页 2下一页 尾页
我想修改成这样显示方式
首页 上一页 1 2 3 4 5 下一页 尾页
请问怎么修改?谢谢!!
------解决思路----------------------
用struts的标签    <s:iterator> 标签在页面上遍历,记得 必须先引入标签库