javaweb自定义标签

标签处理类:PageTagLib.java

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class PageTagLib extends TagSupport {
    private JspWriter writeout;
    private int crruntPage;
    private int pageSize;
    private int totalCount;
    private String doClick;
    /**
     * 序列化
     */
    private static final long serialVersionUID = 3174234039143531070L;

    /**
     * @see 如若使用直接定位的 的input name名为crruntPage
     */
    @Override
    public int doEndTag() throws JspException {
        initWriter();
        showPageBody();
        return EVAL_PAGE;
    }

    public void showPageBody() {
        int PageSize = getPageSize() == 0 ? Constants.PAGESIZE : getPageSize();
        setPageSize(PageSize);
        StringBuffer out = new StringBuffer();
        out = out.append("<tr>");
        out = out.append("<td width=‘25%‘ height=‘29‘ nowrap=‘nowrap‘><span>");
        out = out.append("共 " + getTotalCount() + " 条纪录,当前第 [ "
                + getCrruntPage() + " / " + getMaxCount()[2] + " ] 页,每页"
                + PageSize + "条纪录");
        out = out.append("</span></td>");
        out = out.append("<td width=‘75%‘ valign=‘top‘><div align=‘right‘>");
        out = out
                .append("<table width=‘352‘ height=‘20‘ border=‘0‘ cellpadding=‘0‘ cellspacing=‘0‘>");
        out = out.append("<tr>");
        int lastPage = getCrruntPage() - 1 <= 0 ? 1 : getCrruntPage() - 1;
        int nextPage = getCrruntPage() + 1 > getMaxCount()[2] ? getMaxCount()[2]
                : getCrruntPage() + 1;
        out = out
                .append("<td width=‘62‘ height=‘22‘ valign=‘middle‘><div align=‘right‘><img src=‘images/first.gif‘ onclick =\""
                        + getDoClick()
                        + "\"  href=\"#\" id=1  width=‘37‘ height=‘15‘ /></div></td>");
        out = out
                .append("<td width=‘50‘ height=‘22‘ valign=‘middle‘><div align=‘right‘><img src=‘images/back.gif‘ onclick =\""
                        + getDoClick()
                        + "\"  href=\"#\" id="
                        + lastPage
                        + "  width=‘43‘ height=‘15‘ /></div></td>");
        out = out
                .append("<td width=‘54‘ height=‘22‘ valign=‘middle‘><div align=‘right‘><img src=‘images/next.gif‘ onclick =\""
                        + getDoClick()
                        + "\"  href=\"#\" id="
                        + nextPage
                        + " width=‘43‘ height=‘15‘ /></div></td>");
        out = out
                .append("<td width=‘49‘ height=‘22‘ valign=‘middle‘><div align=‘right‘><img src=‘images/last.gif‘ onclick =\""
                        + getDoClick()
                        + "\"  href=\"#\" id="
                        + getMaxCount()[2]
                        + "  width=‘37‘ height=‘15‘ /></div></td>");
        out = out
                .append("<td width=‘59‘ height=‘22‘ valign=‘middle‘><div align=‘right‘>转到第</div></td>");
        out = out
                .append("<td width=‘25‘ height=‘22‘ valign=‘middle‘><span class=‘STYLE7‘>");
        out = out
                .append("<input type=\"text\" class=\"pageText\" style=‘height:13px; width:25px;‘ size=‘5‘  onkeyup=\"this.value=this.value.replace(/\\D/g,‘‘);if(this.value > "
                        + getMaxCount()[2]
                        + "){this.value=‘‘}\" onafterpaste=\"this.value=this.value.replace(/\\D/g,‘‘)\"  name =‘crruntPage‘ id =‘crruntPage‘ value="
                        + getCrruntPage() + " />");
        out = out.append("</span></td>");
        out = out.append("<td width=‘23‘ height=‘22‘ valign=‘middle‘>页</td>");
        out = out
                .append("<td width=‘30‘ height=‘22‘ valign=‘middle‘><img src=‘images/go.gif‘ id=\"button\" onclick =\""
                        + getDoClick() + "\" width=‘37‘ height=‘15‘ /></td>");
        out = out.append("</tr>");
        out = out.append("</table>");
        out = out.append("</div></td>");
        out = out.append("</tr>");
        out = out.append("</table></td>");
        out = out
                .append("<td width=‘14‘><img src=‘images/tab_22.gif‘ width=‘14‘ height=‘29‘ /></td>");
        out = out.append("</tr>");

        try {
            this.writeout.println(out.toString());
            this.writeout.flush();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    /** 获取当前页数的和最大页数的算法 * */
    public int[] getMaxCount() {
        int x = 1, y = 0;
        // 总页面数
        int totalPage = (getTotalCount() % getPageSize() == 0 ? getTotalCount()
                / getPageSize() : getTotalCount() / getPageSize() + 1);
        if (getCrruntPage() < 5 && getCrruntPage() > 0) {
            if (totalPage >= 5) {
                x = 1;
                y = 5;
            } else {
                x = 1;
                y = totalPage;
            }
        }
        if (getCrruntPage() > 2 && (getCrruntPage() + 2) <= totalPage) {
            x = getCrruntPage() - 2;
            y = getCrruntPage() + 2;
        }
        if (getCrruntPage() > 4 && (getCrruntPage() + 2) > totalPage) {
            x = getCrruntPage() - 3;
            y = totalPage;
        }
        if (getCrruntPage() == totalPage && getCrruntPage() > 4) {
            x = getCrruntPage() - 4;
            y = totalPage;
        }
        if (totalPage == 1) {
            x = 1;
            y = 1;
        }
        if (totalPage == 0) {
            x = 1;
            y = 1;
        }
        int[] out = new int[] { x, y, totalPage };
        return out;
    }

    /** 获取页面中的out的对象 */
    public void initWriter() {
        try {
            this.pageContext.getResponse().flushBuffer();
            this.writeout = this.pageContext.getOut();
        } catch (Exception e) {
            // e.printStackTrace();
        }
    }

    public int getCrruntPage() {
        return crruntPage;
    }

    public void setCrruntPage(int crruntPage) {
        this.crruntPage = crruntPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public String getDoClick() {
        return doClick;
    }

    public void setDoClick(String doClick) {
        this.doClick = doClick;
    }
}

配置文件:page.tld

<?xml version="1.0" encoding="utf-8"?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee Web-jsptaglibrary_2_0.xsd"
 version="2.0" >
 <description>page Tag</description>
 <tlib-version>1.0</tlib-version>
 <short-name>page_tag</short-name>
 <uri>/page_tag</uri>
 <tag>
  <name>showPageTag</name>
  <tag-class>com.immunity.util.PageTagLib</tag-class>
  <body-content>empty</body-content>
  <attribute><!-- 当前页面 -->
   <name>crruntPage</name>
   <required>true</required>
   <rtexprvalue>true</rtexprvalue>
  </attribute>
  <attribute>
    <name>pageSize</name> <!-- 页面最大显示数 -->
    <required>false</required>
    <rtexprvalue>true</rtexprvalue>
  </attribute>
  <attribute>
    <name>totalCount</name><!-- 总记录数 -->
    <required>true</required>
    <rtexprvalue>true</rtexprvalue>
  </attribute>
  <attribute>
    <name>doClick</name><!-- 执行跳转的单击事件, -->
    <required>true</required>
    <rtexprvalue>true</rtexprvalue>
  </attribute>
 </tag>
</taglib>

web.xml

<jsp-config>
      <taglib>
        <taglib-uri>/page_tag</taglib-uri>
        <taglib-location>/WEB-INF/page.tld</taglib-location>
      </taglib>
      <jsp-property-group>
        <url-pattern>/jsp/*</url-pattern>
        <page-encoding>utf-8</page-encoding>
      </jsp-property-group>
</jsp-config>

页面使用 jsp文件中

<%@ taglib uri="/page_tag" prefix="page_tag"%>
<!--showPageTag是page.tld中tag标签的name属性-->
<page_tag:showPageTag  doClick="doPage(this);" totalCount="${totalCount}" crruntPage="${crruntPage}" />

javaweb自定义标签,布布扣,bubuko.com

javaweb自定义标签

上一篇:Java调试


下一篇:Spring与MyBatis整合