熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

用Struts2實現列表顯示和分頁功能

2013-11-23 20:03:45  來源: Java開源技術 

  以下是在前兩天一個項目中的分頁功能截取

  BlogDAOjava文件

  [java]

  /** 根據條件(默認一張表所有數據)返回多條記錄 */

  public List<Blog> list(String kwInteger pageCurInteger pageSize) {

  List<Blog> list = null;

  Integer limitaInteger = (pageCur)*pageSize;

  Integer limitbInteger = pageCur*pageSize;

  Object[] params = {limitaIntegerlimitbInteger};//代入的參數列表

  String sqlWhere = ;

  String sql = select * from csdn_blog where first= ;

  if(kw!=null && !kwequals()) {

  sqlWhere = and topic like %+kw+%;

  }

  sql += sqlWhere;

  sql += order by id desc limit ??;

  ResultSetHandler<List<Blog》 rsh = new BeanListHandler<Blog>(Blogclass)//把結果集轉成BeanList

  try {

  list = qrquery(getConn() sql rsh params)   //調用查詢接口的查詢函數

  } catch (SQLException e) {

  eprintStackTrace()

  }

  return list;

  }

  BlogActinjava文件

  [java]

  private Integer pagenum;//頁碼

  [java] view plaincopy

  private List<Blog> allblogs;

  並提供set  get 方法

  [java]

  public List<Blog> getAllblogs() {

  return allblogs;

  }

  public void setAllblogs(List<Blog> allblogs) {

  thisallblogs = allblogs;

  }

  [java]

  public Integer getPagenum() {

  return pagenum;

  }

  public void setPagenum(Integer pagenum) {

  thispagenum = pagenum;

  }

  列表方法

  [java]

  /** 顯示博客列表信息 */

  public String alllist() {

  request=ServletActionContextgetRequest()

  blogtopic=requestgetParameter(blogtopic

  allblogs=blogDAOlist(blogtopic pagenum

  num_allblog=untAllNum(blogtopic)

  num_allblogpage=num_allblog/+;

  return index;

  }

  JSP文件

  [html]

  <div class=content>

  <c:forEach items=${allblogs} var=allblog>

  <div class=blog_list>

  <h><a # class=category>[${allblogtopic}]</a>

  <a name= Blog_getContentaction?id=${allblogid} target=_blank>${allblogtitle}</a></h>

  <dl>

  <dt><a #><img src=img/oyuntaolianwujpg alt=jackyvincefu></a></dt>

  <dd><div class=text_length><pre>${ntent }</pre></div></dd></dl>

  <p><div class=lable>

  <a class=tag #>C++</a><a class=tag # target=_blank>指針常量</a>

  <a class=tag #>常量指針</a><a class=tag # target=_blank>常量指針常量</a></div></p>

  <div class=about_info>

  <div align=left><span class=fl>

  <table width= border=>

  <tr>

  <td width=><a # target=_blank class=user_name><font size=>${allblogwriter}</font></a>

  <a Blog_getContentaction?id=${allblogid} target=_blank class=view><font size=>閱讀(${allblogreader})</font></a>

  <a Blog_getContentaction?id=${allblogid}#lookcomment target=_blank class=comment><font size=>評論(${allblogdiscuss})</font></a></td>

  <td width= align=right><span class=time><font size=>${allblogtime}</font></span></td>

  </tr></table></span></div>

  </div>

  </div>

  </c:forEach>

  </div>

  <div class=page_nav>

  <span> ${num_allblog}條數據  共${num_allblogpage}頁  </span>

  <c:forEach  begin= end=${num_allblogpage} var=pagenum>

  [<a Blog_alllistaction?pagenum=${pagenum}>${pagenum}</a>]

  </c:forEach>

  <a Blog_alllistaction?pagenum=${pagenum+}>  下一頁  </a>

  <a Blog_alllistaction?pagenum=${num_allblogpage}> 尾頁</a> </div>

  </div>


From:http://tw.wingwit.com/Article/program/Java/ky/201311/27879.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.