jimphei學(xué)習(xí)工作室

          jimphei學(xué)習(xí)工作室

            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            23 隨筆 :: 0 文章 :: 1 評(píng)論 :: 0 Trackbacks
          今天看到某人寫(xiě)的分頁(yè)類,結(jié)果發(fā)現(xiàn)批人家的人不少,沒(méi)有必要,好的東西吸收學(xué)習(xí),感覺(jué)不實(shí)用可以不用,何必發(fā)帖子挖苦人家。我前段時(shí)間也自己設(shè)計(jì)了一個(gè)分頁(yè)的方法,絕對(duì)是自己想到的,如果網(wǎng)上有一樣的,說(shuō)明大家都思考了,有可取度,提供給大家參考。
          首先寫(xiě)了一個(gè)分頁(yè)的類,其實(shí)只有主要屬性的setter和getter方法
          /**
          * 分頁(yè)類
          * @author qinglin876
          *
          */
          public class Pagination {
          private int start;
          //一次取得的數(shù)量
          private int size;
          //要取得頁(yè)數(shù)
          private int currentPage = 1;
          //總的記錄頁(yè)數(shù)
          private int totalPage = 0;
          //總的記錄條數(shù)
          private int totalRecord;
          public int getTotalRecord() {
          return totalRecord;
          }
          public void setTotalRecord(int totalRecord) {
          this.totalRecord = totalRecord;
          }
          public Pagination(){

          }
          public Pagination(int size){
          this.size = size;
          }
          public int getSize() {
          return size;
          }
          public void setSize(int size) {
          this.size = size;
          }
          public int getStart() {
          return start;
          }
          public void setStart(int start) {
          this.start = start;
          }
          public int getCurrentPage() {
          return currentPage;
          }
          public void setCurrentPage(int currentPage) {
          this.currentPage = currentPage;
          }
          public int getTotalPage() {
          return totalPage;
          }
          public void setTotalPage(int totalPage) {
          this.totalPage = totalPage;
          }

          }

          另外pagination.jsp由pagination類填充

          <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

          <%@ taglib prefix="s" uri="/struts-tags"%>
          <SCRIPT type="text/javascript">

                function trim(str){
          return str.replace(/(^\s*)|(\s*$)/g, "");
          }

          function selectPage(input){

          var value = trim(input.value);
          if(value == ""){
          return;
          }

          if(/\d+/.test(value)){

          input.form.submit();
          return;
          }
          alert("請(qǐng)輸入正確的頁(yè)數(shù)");
          input.focus();

          }
             
          </SCRIPT>
          <div class="pagech">

          <s:if test="pagination.totalPage != 0">
          <s:url action="%{#request.url}" id="first">
          <s:param name="pagination.currentPage" value="1"></s:param>
          </s:url>
          <s:url action="%{#request.url}" id="next"  >
          <s:param name="pagination.currentPage"
          value="pagination.currentPage+1">
          </s:param>
          </s:url>
          <s:url action="%{#request.url}" id="prior" >
          <s:param name="pagination.currentPage"
          value="pagination.currentPage-1"></s:param>
          </s:url>
          <s:url action="%{#request.url}" id="last">
          <s:param name="pagination.currentPage" value="pagination.totalPage"></s:param>
          </s:url>
          <s:if test="pagination.currentPage == 1">
          <span class="current">首頁(yè)</span>
          <span class="current">上一頁(yè)</span>
          </s:if>
          <s:else>
          <s:a href="%{first}">首頁(yè)</s:a>
          <s:a href="%{prior}">上一頁(yè)</s:a>
          </s:else>
          <s:if
          test="pagination.currentPage == pagination.totalPage || pagination.totalPage == 0">
          <span class="current">下一頁(yè)</span>
          <span class="current">末頁(yè)</span>
          </s:if>
          <s:else>
          <s:a href="%{next}">下一頁(yè)</s:a>&nbsp;&nbsp;
                            <s:a href="%{last}">末頁(yè)</s:a>
          </s:else>
          <span class="jumplabel">跳轉(zhuǎn)到</span>
          <s:form action="%{#request.url}" theme="simple"
          cssStyle="display:inline">
          <s:hidden name="pagination.totalPage" value="%{pagination.totalPage}"></s:hidden>
          <input type="text" name="pagination.currentPage" size="2"
          onblur="selectPage(this)" />
          </s:form>

          <span class="jumplabel">頁(yè)</span>
          <span class="jumplabel">共<s:property
          value="pagination.totalRecord" />條</span>
          <span class="jumplabel">當(dāng)前是第<s:property
          value="pagination.currentPage" />/<s:property value="pagination.totalPage"/>頁(yè)</span>


          </s:if>

          </div>

          用的時(shí)候,在頁(yè)面include進(jìn)去,注意上面的"%{#request.url}",即是在struts2的action里面有一個(gè)setter和getter方法,下面看action中的某個(gè)方法
          public String showNotices() throws Exception{

          if(tip != null){
          tip = new String(tip.getBytes("iso8859-1"),"utf-8");
          }
          if(notices == null)
          this.notices = new ArrayList<Notice>();
          int size = Integer.valueOf(this.getText("per_page_notice_size"));
          if (pagination == null) {
          pagination = new Pagination(size);
          }
          pagination.setSize(size);
          if (pagination.getCurrentPage() <= 0) {
          pagination.setCurrentPage(1);
          }
          if (pagination.getTotalPage() != 0
          && pagination.getCurrentPage() > pagination.getTotalPage()) {
          pagination.setCurrentPage(pagination.getTotalPage());
          }
          url = "goto_showNotices.action"; this.notices.addAll(this.noticeDAO.showAll(pagination));
          if(this.notices.size() == 0 && pagination.getCurrentPage() != 1){
          pagination.setCurrentPage(pagination.getCurrentPage()-1);
          this.notices.addAll(this.noticeDAO.showAll(pagination));
          }
          return "success";
          }

          在上面的this.noticeDAO.showAll(pagination))中填充pagination,具體如下
          /*
          * 顯示所有的通告
          * @see com.qinglin.dao.NoticeDAO#showAll(com.qinglin.util.Pagination)
          */
          @SuppressWarnings("unchecked")
          public List<Notice> showAll(final Pagination pagination) {
          String hql = "from Notice as n";
          this.getHibernateTemplate().setCacheQueries(true);
          int totalRecord = ((Long) this.getSession().createQuery(
          "select count(*) " + hql).uniqueResult()).intValue();
          int totalPage = totalRecord % pagination.getSize() == 0 ? totalRecord
          / pagination.getSize() : totalRecord / pagination.getSize() + 1;
          pagination.setTotalRecord(totalRecord);
          pagination.setTotalPage(totalPage);
          hql += " order by n.add_date desc";
          final String hql1 = hql;

          return (List<Notice>) this.getHibernateTemplate().execute(
          new HibernateCallback() {
          public Object doInHibernate(Session session)
          throws HibernateException, SQLException {
          Query query = session.createQuery(hql1);
          query.setFirstResult((pagination.getCurrentPage() - 1)
          * pagination.getSize());
          query.setMaxResults(pagination.getSize());
          return query.list();
          }
          });
          }


          基本上就這些,當(dāng)然請(qǐng)求的action里面需要設(shè)置pagination的setter和getter方法
          這個(gè)分頁(yè)方法特點(diǎn)是簡(jiǎn)單,只需在action中指明請(qǐng)求的url,用某種方法填充pagination,在顯示的頁(yè)面包含pagination.jsp即可。



          package com.shop.bean;

          import java.util.List;

          public class PageView <T> {

          private int currentPage = 1;

          private long totalPage = 1;

          private long totalRecord = 1;

          private List <T> records;

          private int firstIndex = 1;

          private PageIndex pageIndex;

          private int maxResult = 12;

          public PageView(int currentPage, int maxResult) {
          this.currentPage = currentPage;
          this.maxResult = maxResult;
          this.firstIndex = currentPage * maxResult;
          }

          public int getCurrentPage() {
          return currentPage;
          }

          public void setCurrentPage(int currentPage) {
          this.currentPage = currentPage;
          }

          public void setQueryResult(QueryResult <T> qr){
          setTotalRecord(qr.getTotal());
          setRecords(qr.getDatas());
          }

          public long getTotalPage() {
          return totalPage;
          }

          public void setTotalPage(long totalPage) {
          this.totalPage = totalPage;
          this.pageIndex = WebTool.getPageIndex(this.maxResult, this.currentPage, this.totalPage);
          }

          public long getTotalRecord() {
          return totalRecord;
          }

          public void setTotalRecord(long totalRecord) {
          this.totalRecord = totalRecord;
          setTotalPage(totalRecord / this.maxResult == 0 ? totalRecord / this.maxResult : totalRecord / this.maxResult + 1);
          }

          public List <T> getRecords() {
          return records;
          }

          public void setRecords(List <T> records) {
          this.records = records;
          }

          public int getFirstIndex() {
          return firstIndex;
          }
          public PageIndex getPageIndex() {
          return pageIndex;
          }

          public void setPageIndex(PageIndex pageIndex) {
          this.pageIndex = pageIndex;
          }

          public int getMaxResult() {
          return maxResult;
          }

          public void setMaxResult(int maxResult) {
          this.maxResult = maxResult;
          }

          public void setFirstIndex(int firstIndex) {
          this.firstIndex = firstIndex;
          }
          }


          畫(huà)面的代碼:
          <s:iterator value="#request.pageView.pageIndex.pageList">
                <s:if test="#request.pageView.currentPage == 4"> <b> <font color="#FFFFFF">第 <s:property/>頁(yè) </font> </b> </s:if>
              <s:if test="#request.pageView.currentPage != 4"> <a href="javascript:topage( <s:property/>)" class="a03">第 <s:property/>頁(yè) </a> </s:if>
          </s:iterator>

          action中的代碼:
          Map <String, Object> request = (Map <String, Object>)ActionContext.getContext().get("request");
          request.put("pageView", pageView);




          <s:iterator value="#request.pageView.pageIndex.pageList">中="#request.pageView.pageIndex.pageList值能正常獲取,可是  <s:if test="#request.pageView.currentPage == 4"> 中的="#request.pageView.currentPage值獲取不到正確的值,這是什么原因啊?
          問(wèn)題補(bǔ)充:
            <s:iterator value="#request.pageView.pageIndex.pageList">
              <s:if test="{#request.pageView.currentPage == 4}"><b><font color="#FFFFFF">第<s:property/>頁(yè)</font></b></s:if>
              <s:if test="{#request.pageView.currentPage != 4}"><a href="javascript:topage(<s:property/>)" class="a03">第<s:property/>頁(yè)</a></s:if>
          </s:iterator>
          posted on 2009-09-22 12:03 jimphei 閱讀(214) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 额尔古纳市| 容城县| 当雄县| 五台县| 连平县| 芦溪县| 贡嘎县| 青州市| 刚察县| 东源县| 双城市| 毕节市| 云龙县| 大英县| 嘉峪关市| 无极县| 天气| 晴隆县| 南丰县| 天镇县| 阿图什市| 晋城| 广南县| 岳阳市| 莒南县| 凤城市| 都江堰市| 二连浩特市| 澄城县| 吴江市| 天门市| 富锦市| 常德市| 米林县| 大厂| 翁牛特旗| 荔浦县| 中方县| 辰溪县| 乐至县| 湄潭县|