jsp 开发 百度 google 分页控件 struts2 实现

分页输出的实体

package com.loongtao.lucene.search.model;

import java.io.Serializable;

public class PageControl implements Serializable, Cloneable {

    private static final long serialVersionUID = 3686354990469630498L;
    private String pageName;
    private String href;
    private String isThisPage = "0";

    public String getPageName() {
        return pageName;
    }

    public void setPageName(String pageName) {
        this.pageName = pageName;
    }

    public String getHref() {
        return href;
    }

    public void setHref(String href) {
        this.href = href;
    }

    public String getIsThisPage() {
        return isThisPage;
    }

    public void setIsThisPage(String isThisPage) {
        this.isThisPage = isThisPage;
    }
}

  * 搜索引擎检索分页控件
 *
 * @author xiao.bin
 *

public class SearchPageControl {

   
    public static List<PageControl> getQueryPageControlByApi(ApiObject api) {
        List<PageControl> list = new ArrayList<PageControl>();
        // 获取当前页
        int page = api.getPage();
        // 获取所有条数
        int allarticleCount = api.getArticlescount();
        // 获取当前页面大小
        int pagesize = api.getPagesize();
        // 计算页数
        int countPage = allarticleCount / pagesize;
        // 计算是否需要多加一页
        if (allarticleCount % pagesize != 0) {
            countPage++;
        }
        int startX = 1;
        // 计算开始位置
        if (page > 4 && page <= countPage) {
            startX = page - 4;
        }
        // 上一页
        if (page > 1 && page <= countPage) {
            PageControl c = new PageControl();
            c.setPageName("上一页");
            c.setHref(appendHref(api, page - 1));
            list.add(c);
        }
        for (int i = startX; i < startX + 10 && i <= countPage; i++) {
            // 判断是否等于当前页
            PageControl c = new PageControl();
            c.setPageName(i + "");
            c.setHref(appendHref(api, i));
            if (i == page) {
                c.setIsThisPage("1");
            }
            list.add(c);
        }
        // 上一页
        if (page < countPage) {
            PageControl c = new PageControl();
            c.setPageName("下一页");
            c.setHref(appendHref(api, page + 1));
            list.add(c);
        }
        return list;
    }

   
    public static String appendHref(ApiObject api, int page) {
        StringBuffer sb = new StringBuffer("?");
        if (api.getTitle().length() > 0) {
            sb.append(appendField("title", api.getTitle()));
        }
        if (api.getContent().length() > 0) {
            sb.append(appendField("content", api.getContent()));
        }
        sb.append(appendField("page", page + ""));
        sb.append("pagesize=" + api.getPagesize());
        return sb.toString();
    }

   
    public static String appendField(String urlField, String urlValue) {
//urlencode转码
        return urlField + "="
                + StringUtils.StringToUrlEncode(urlValue, StringUtils.UTF8)
                + "&";
    }

    public static void main(String[] args) {

        ApiObject api = new ApiObject();
        api.setArticlescount("365");
        api.setPage("3");
        api.setTitle("xiao.bin");

        List<PageControl> list = getQueryPageControlByApi(api);

        for (PageControl pageControl : list) {
            System.out.println(pageControl.getHref());
        }
    }

}

由action 发送 List<PageControl>  到页面上.

  struts2 标签 输出
    <div id="page">
                            <s:iterator  value="#request.pagectr" id="id"   status="pagelist">
                                <s:if test="%{#id.isThisPage == 1}">
                                    <strong>${pageName}</strong>
                                </s:if>
                                <s:else>
                                <a href="<%=basePath%>search/list.html${href}">[${pageName}]</a>
                                </s:else>   
                                &nbsp;&nbsp;
                            </s:iterator>
                        </div>

实现效果
[上一页]     [1]     2     [3]     [4]     [5]     [6]     [7]     [8]     [9]     [10]     [下一页] 

你可能感兴趣的:(struts2)