当前位置:编程学习 > JS >>

jsp 自定义分页标签

花了一上午简单研究了下自定义标签。就弄了个自定义的分页标签。代码没有怎么仔细优化。先贴上来,:)

1,标签的实现类NumenTag.java

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp(SUN企业级应用的首选).jsp(SUN企业级应用的首选)Exception;
import javax.servlet.jsp(SUN企业级应用的首选).jsp(SUN企业级应用的首选)Writer;
import javax.servlet.jsp(SUN企业级应用的首选).tagext.TagSupport;

/** *//**
 * 自定义的分页标签
 *
 * @author 马万林
 *
 */
public class NumenTag extends TagSupport {

    private String url = null;

    private Result result;

    private String page = null;

    private String[] p;

    private Map kv = new HashMap();

    private List key = null;

    /** *//**
     * 分离参数
     * @param params
     */
    public void setParams(String params) {
        key = new ArrayList();
        p = params.split(";");
        for (int i = 0; p.length>1&&i < p.length; i++) {
            kv.put(p[i].split(":")[0], p[i].split(":")[1]);
            key.add(p[i].split(":")[0]);
        }
    }

    /** *//**
     * 分页的结果
     * @param result
     */
    public void setResult(String result) {
        this.result = (Result) pageContext.getRequest().getAttribute("result");
        this.page = (String) pageContext.getRequest().getAttribute("page");
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public int doStartTag() throws jsp(SUN企业级应用的首选)Exception {
        StringBuffer sb = null;
        if (result != null && result.getContent() != null
                && result.getContent().size() > 0) {
            sb = new StringBuffer("分页: ");
            if (result.getPage().getHasPrePage()){
                sb.append("<a href=").append(url);
                if (url.indexOf("?") == -1)
                    sb.append("?page=1");
                else
                    sb.append("&page=1");
                linkParams(sb);
                sb.append(">").append("首页").append("</a>").append(" | ");
                sb.append("<a href=").append(url);
                if (url.indexOf("?") == -1)
                    sb.append("?page=").append(
                            result.getPage().getPreviousPage());
                else
                    sb.append("&page=").append(
                            result.getPage().getPreviousPage());
                linkParams(sb);
                sb.append(">").append("上页").append("</a>").append(" | ");
            }
            if (result.getPage().getHasNextPage()) {
                sb.append("<a href=").append(url);
                if (url.indexOf("?") == -1)
                    sb.append("?page=").append(result.getPage().getNextPage());
                else
                    sb.append("&page=").append(result.getPage().getNextPage());
                linkParams(sb);
                sb.append(">").append("下页").append("</a>").append(" | ");
                sb.append("<a href=").append(url);
                if (url.indexOf("?") == -1)
                    sb.append("?page=").append(result.getPage().getTotalPage());
                else
                    sb.append("&page=").append(result.getPage().getTotalPage());
                linkParams(sb);
                sb.append(">").append("尾页").append("</a>");
            }
            sb
                    .append(" 跳转到:<select name=goToPage onchange=javascript:goPage(this.value)>");
            int s = result.getPage().getTo

补充:Web开发 , Jsp ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,