<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    jimphei學習工作室

    jimphei學習工作室

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      23 隨筆 :: 0 文章 :: 1 評論 :: 0 Trackbacks
    今天看到某人寫的分頁類,結果發(fā)現批人家的人不少,沒有必要,好的東西吸收學習,感覺不實用可以不用,何必發(fā)帖子挖苦人家。我前段時間也自己設計了一個分頁的方法,絕對是自己想到的,如果網上有一樣的,說明大家都思考了,有可取度,提供給大家參考。
    首先寫了一個分頁的類,其實只有主要屬性的setter和getter方法
    /**
    * 分頁類
    * @author qinglin876
    *
    */
    public class Pagination {
    private int start;
    //一次取得的數量
    private int size;
    //要取得頁數
    private int currentPage = 1;
    //總的記錄頁數
    private int totalPage = 0;
    //總的記錄條數
    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("請輸入正確的頁數");
    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">首頁</span>
    <span class="current">上一頁</span>
    </s:if>
    <s:else>
    <s:a href="%{first}">首頁</s:a>
    <s:a href="%{prior}">上一頁</s:a>
    </s:else>
    <s:if
    test="pagination.currentPage == pagination.totalPage || pagination.totalPage == 0">
    <span class="current">下一頁</span>
    <span class="current">末頁</span>
    </s:if>
    <s:else>
    <s:a href="%{next}">下一頁</s:a>&nbsp;&nbsp;
                      <s:a href="%{last}">末頁</s:a>
    </s:else>
    <span class="jumplabel">跳轉到</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">頁</span>
    <span class="jumplabel">共<s:property
    value="pagination.totalRecord" />條</span>
    <span class="jumplabel">當前是第<s:property
    value="pagination.currentPage" />/<s:property value="pagination.totalPage"/>頁</span>


    </s:if>

    </div>

    用的時候,在頁面include進去,注意上面的"%{#request.url}",即是在struts2的action里面有一個setter和getter方法,下面看action中的某個方法
    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();
    }
    });
    }


    基本上就這些,當然請求的action里面需要設置pagination的setter和getter方法
    這個分頁方法特點是簡單,只需在action中指明請求的url,用某種方法填充pagination,在顯示的頁面包含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;
    }
    }


    畫面的代碼:
    <s:iterator value="#request.pageView.pageIndex.pageList">
          <s:if test="#request.pageView.currentPage == 4"> <b> <font color="#FFFFFF">第 <s:property/>頁 </font> </b> </s:if>
        <s:if test="#request.pageView.currentPage != 4"> <a href="javascript:topage( <s:property/>)" class="a03">第 <s:property/>頁 </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值獲取不到正確的值,這是什么原因啊?
    問題補充:
      <s:iterator value="#request.pageView.pageIndex.pageList">
        <s:if test="{#request.pageView.currentPage == 4}"><b><font color="#FFFFFF">第<s:property/>頁</font></b></s:if>
        <s:if test="{#request.pageView.currentPage != 4}"><a href="javascript:topage(<s:property/>)" class="a03">第<s:property/>頁</a></s:if>
    </s:iterator>
    posted on 2009-09-22 12:03 jimphei 閱讀(215) 評論(0)  編輯  收藏

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


    網站導航:
     
    主站蜘蛛池模板: 亚洲中文字幕无码爆乳app| 亚洲国产综合无码一区二区二三区| 国产aⅴ无码专区亚洲av麻豆| 亚洲.国产.欧美一区二区三区| 三年片在线观看免费观看高清电影| 亚洲熟妇色自偷自拍另类| 在线观看免费视频资源| 亚洲最大中文字幕| 亚洲国产精品免费观看| 亚洲精品123区在线观看| 成人免费视频一区| 久久亚洲精品成人无码| 亚洲国产一区二区三区| 黄桃AV无码免费一区二区三区| 亚洲乱亚洲乱妇无码麻豆| 一级毛片成人免费看免费不卡| 亚洲自偷自拍另类图片二区 | 美女视频黄a视频全免费网站一区| 国产成人免费A在线视频| 免费精品视频在线| 亚洲AV无码精品无码麻豆| 亚洲成人免费电影| 亚洲av片在线观看| 国产亚洲av人片在线观看| 久99久精品免费视频热77| 亚洲AV无码国产精品色| 亚洲av午夜精品一区二区三区| 香蕉免费在线视频| 亚洲人成伊人成综合网久久| 日本一道高清不卡免费| 在线视频网址免费播放| 亚洲宅男天堂a在线| 四虎影视永久免费观看地址| 免费无码av片在线观看 | 亚洲国产成人一区二区三区 | 99久久免费国产精品特黄| 无人视频在线观看免费播放影院| 国产AV无码专区亚洲Av| 国内大片在线免费看| 日韩精品无码免费专区午夜不卡| 国产成人精品亚洲日本在线|