用Struts2实现列表显示和分页功能

以下是在前两天一个项目中的分页功能截取。

BlogDAO.java文件

 

[java][/java] view plaincopyprint?
  1. /** 根据条件(默认一张表所有数据)返回多条记录 */
  2. public List<Blog> list(String kw,Integer pageCur,Integer pageSize) {
  3.     List<Blog> list = null;
  4.     Integer limitaInteger = (pageCur-1)*pageSize;
  5.     Integer limitbInteger = pageCur*pageSize;
  6.     Object[] params = {limitaInteger,limitbInteger};//代入的参数列表
  7.     String sqlWhere = “”;
  8.     String sql = “select * from csdn_blog where first=1 “;
  9.     if(kw!=null && !kw.equals(“”)) {
  10.         sqlWhere = ” and topic like ‘%”+kw+”%'”;
  11.     }
  12.     sql += sqlWhere;
  13.     sql += ” order by id desc limit ?,?”;
  14.     ResultSetHandler<List<Blog>> rsh = new BeanListHandler<Blog>(Blog.class);//把结果集转成BeanList
  15.     try {
  16.         list = qr.query(getConn(), sql, rsh, params);   //调用查询接口的查询函数
  17.     } catch (SQLException e) {
  18.         e.printStackTrace();
  19.     }
  20.     return list;
  21. }
	/** 根据条件(默认一张表所有数据)返回多条记录 */
	public List<Blog> list(String kw,Integer pageCur,Integer pageSize) {
		List<Blog> list = null;
		Integer limitaInteger = (pageCur-1)*pageSize;
		Integer limitbInteger = pageCur*pageSize;
		Object[] params = {limitaInteger,limitbInteger};//代入的参数列表
		String sqlWhere = "";
		String sql = "select * from csdn_blog where first=1 ";
		if(kw!=null && !kw.equals("")) {
			sqlWhere = " and topic like '%"+kw+"%'";
		}
		sql += sqlWhere;
		sql += " order by id desc limit ?,?";
		ResultSetHandler<List<Blog>> rsh = new BeanListHandler<Blog>(Blog.class);//把结果集转成BeanList
		try {
			list = qr.query(getConn(), sql, rsh, params);	//调用查询接口的查询函数
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

 

BlogActin.java文件

 

 

[java][/java] view plaincopyprint?
  1. private Integer pagenum;//页码
private Integer pagenum;//页码
[java][/java] view plaincopyprint?
  1. private List<Blog> allblogs;
	private List<Blog> allblogs;

并提供set  get 方法

[java][/java] view plaincopyprint?
  1. public List<Blog> getAllblogs() {
  2.     return allblogs;
  3. }
  4. public void setAllblogs(List<Blog> allblogs) {
  5.     this.allblogs = allblogs;
  6. }
	public List<Blog> getAllblogs() {
		return allblogs;
	}
	public void setAllblogs(List<Blog> allblogs) {
		this.allblogs = allblogs;
	}
[java][/java] view plaincopyprint?
  1. public Integer getPagenum() {
  2.     return pagenum;
  3. }
  4. public void setPagenum(Integer pagenum) {
  5.     this.pagenum = pagenum;
  6. }
	public Integer getPagenum() {
		return pagenum;
	}
	public void setPagenum(Integer pagenum) {
		this.pagenum = pagenum;
	}

列表方法

[java][/java] view plaincopyprint?
  1. /** 显示博客列表信息 */
  2. public String alllist() {
  3.     request=ServletActionContext.getRequest();
  4.     blogtopic=request.getParameter(“blogtopic”);
  5.     allblogs=blogDAO.list(blogtopic, pagenum, 10);
  6.     num_allblog=blogDAO.countAllNum(blogtopic);
  7.     num_allblogpage=num_allblog/10+1;
  8.     return “index”;
  9. }
	/** 显示博客列表信息 */ 
	public String alllist() {
		request=ServletActionContext.getRequest();
		blogtopic=request.getParameter("blogtopic");
		allblogs=blogDAO.list(blogtopic, pagenum, 10);
		num_allblog=blogDAO.countAllNum(blogtopic);
		num_allblogpage=num_allblog/10+1;
		return "index";
	}

JSP文件

[html][/html] view plaincopyprint?
  1. <div class=”content”>
  2.     <c:forEach items=”${allblogs}” var=”allblog”>
  3.         <div class=”blog_list”>
  4.         <h1><a href=”#” class=”category”>[${allblog.topic}]</a>
  5.             <a name=”11519817″ href=”Blog_getContent.action?id=${allblog.id}” target=”_blank”>${allblog.title}</a></h1>
  6.         <dl>
  7.         <dt><a href=”#”><img src=”img/oyuntaolianwu.jpg” alt=”jackyvincefu”></a></dt>
  8.         <dd><div class=”text_length”><pre>${allblog.content }</pre></div></dd></dl>
  9.         <p><div class=”lable”>
  10.             <a class=”tag” href=”#”>C++</a><a class=”tag” href=”#” target=”_blank”>指针常量</a>
  11.             <a class=”tag” href=”#”>常量指针</a><a class=”tag” href=”#” target=”_blank”>常量指针常量</a></div></p>
  12.         <div class=”about_info”>
  13.           <div align=”left”><span class=”fl”>
  14.           <table width=”535″ border=”0″>
  15.               <tr>
  16.                 <td width=”254″><a href=”#” target=”_blank” class=”user_name”><font size=”-1″>${allblog.writer}</font></a>
  17.                         <a href=”Blog_getContent.action?id=${allblog.id}” target=”_blank” class=”view”><font size=”-1″>阅读(${allblog.reader})</font></a>
  18.                         <a href=”Blog_getContent.action?id=${allblog.id}#lookcomment” target=”_blank” class=”comment”><font size=”-1″>评论(${allblog.discuss})</font></a></td>
  19.                 <td width=”271″ align=”right”><span class=”time”><font size=”-1″>${allblog.time}</font></span></td>
  20.               </tr></table></span></div>
  21.         </div>
  22.     </div>
  23.     </c:forEach>
  24. </div>
  25. <div class=”page_nav”>
  26.       <span> ${num_allblog}条数据  共${num_allblogpage}页  </span>
  27.       <c:forEach  begin=”1″ end=”${num_allblogpage}” var=”pagenum”>
  28.         [<a href=”Blog_alllist.action?pagenum=${pagenum}”>${pagenum}</a>]
  29.       </c:forEach>
  30.       <a href=”Blog_alllist.action?pagenum=${pagenum+1}”>  下一页  </a>
  31.       <a href=”Blog_alllist.action?pagenum=${num_allblogpage}”> 尾页</a> </div>
  32. </div>
<div>
    <c:forEach items="${allblogs}" var="allblog">
    	<div>
        <h1><a href="#">[${allblog.topic}]</a>
            <a name="11519817" href="Blog_getContent.action?id=${allblog.id}" target="_blank">${allblog.title}</a></h1>
        <dl>
        <dt><a href="#"><img src="img/oyuntaolianwu.jpg" alt="jackyvincefu"></a></dt>
        <dd><div><pre>${allblog.content }</pre></div></dd></dl>
        <p><div>
			<a href="#">C++</a><a href="#" target="_blank">指针常量</a>
			<a href="#">常量指针</a><a href="#" target="_blank">常量指针常量</a></div></p>
        <div>
          <div align="left"><span>
          <table width="535" border="0">
              <tr>
                <td width="254"><a href="#" target="_blank"><font size="-1">${allblog.writer}</font></a>
                        <a href="Blog_getContent.action?id=${allblog.id}" target="_blank"><font size="-1">阅读(${allblog.reader})</font></a>
                        <a href="Blog_getContent.action?id=${allblog.id}#lookcomment" target="_blank"><font size="-1">评论(${allblog.discuss})</font></a></td>
                <td width="271" align="right"><span><font size="-1">${allblog.time}</font></span></td>

              </tr></table></span></div>
        </div>
    </div>
    </c:forEach>
</div>
<div>
      <span> ${num_allblog}条数据  共${num_allblogpage}页  </span>
      <c:forEach  begin="1" end="${num_allblogpage}" var="pagenum">
      	[<a href="Blog_alllist.action?pagenum=${pagenum}">${pagenum}</a>] 
      </c:forEach>
      <a href="Blog_alllist.action?pagenum=${pagenum+1}">  下一页  </a> 
      <a href="Blog_alllist.action?pagenum=${num_allblogpage}"> 尾页</a> </div>
</div>

 

效果图

标签