分页标签

分页总是让人头疼  今天就决心做一个分页标签!~

1.首先写一个标签类PagesTag.java,当然要继承SimpleTagSupport类咯~!

package com.xaccp.common.page;

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


public class PagesTag extends SimpleTagSupport {

    /**
     * Initialization of action property.
     * 标签中分页时的链接动作
     */
    private java.lang.String action;

    /**
     * Initialization of pagecount property.
     * 总页数
     */
    private int pagecount;

    /**
     * Initialization of curpage property.
     */
    private int curpage;

    /**
     * Initialization of target property.
     * 页面链接的目标,默认为_self
     */
    private java.lang.String target="_self";

    /**
     * Initialization of var property.
     * 分页后的链接参数名称
     */
    private java.lang.String var;
    
    /*
     * 分页显示的风格
     */
    private int style=1;

    public void doTag() throws JspException {
        
        JspWriter out=getJspContext().getOut();
        
        try {
            out.write("<table width=\"100%\"  border=\"0\" cellspacing=\"1\" cellpadding=\"1\">      ");
            out.write("<tr>        ");
            if (style==1)
            {
                if (this.curpage > 1 ){
                        out.write("<td>");
                            StringBuilder pageString=new StringBuilder();
                            pageString.append("<a href=\"");
                            pageString.append(action);
                            pageString.append("?");
                            pageString.append(var);
                            pageString.append("=");
                            pageString.append(this.curpage - 1);
                            pageString.append("\"><font color=#ff0000>[");
                            pageString.append("上一页");
                            pageString.append("]</font>");
                            pageString.append("</a>");
                        out.write(pageString.toString());
                        out.write("</td>        ");
                  }
                  if (this.curpage < this.pagecount){
                    out.write("<td>");
                        StringBuilder pageString=new StringBuilder();
                        pageString.append("<a href=\"");
                        pageString.append(action);
                        pageString.append("?");
                        pageString.append(var);
                        pageString.append("=");
                        pageString.append(this.curpage + 1);
                        pageString.append("\"><font color=#ff0000>[");
                        pageString.append("下一页");
                        pageString.append("]</font>");
                        pageString.append("</a>");
                    out.write(pageString.toString());
                    out.write("</td>        ");
                  }
            }
            else if(style==2)
            {
                if (this.curpage > 1) {
                     out.write("<td>");
                        StringBuilder pageString=new StringBuilder();
                        pageString.append("<a href=\"");
                        pageString.append(action);
                        pageString.append("?");
                        pageString.append(var);
                        pageString.append("=1");
                        //pageString.append(this.curpage - 1);
                        pageString.append("\"><font color=#ff0000>[");
                        pageString.append("首页");
                        pageString.append("]</font>");
                        pageString.append("</a>");
                    out.write(pageString.toString());
                    out.write("</td>        ");
                    
                    
                    out.write("<td>");
                        StringBuilder pageString1=new StringBuilder();
                        pageString1.append("<a href=\"");
                        pageString1.append(action);
                        pageString1.append("?");
                        pageString1.append(var);
                        pageString1.append("=");
                        pageString1.append(this.curpage - 1);
                        pageString1.append("\"><font color=#ff0000>[");
                        pageString1.append("上一页");
                        pageString1.append("]</font>");
                        pageString1.append("</a>");
                    out.write(pageString1.toString());
                    out.write("</td>        ");
                    
                }
                
                if (this.curpage < this.pagecount) {
                    out.write("<td>");
                        StringBuilder pageString1=new StringBuilder();
                        pageString1.append("<a href=\"");
                        pageString1.append(action);
                        pageString1.append("?");
                        pageString1.append(var);
                        pageString1.append("=");
                        pageString1.append(this.curpage + 1);
                        pageString1.append("\"><font color=#ff0000>[");
                        pageString1.append("下一页");
                        pageString1.append("]</font>");
                        pageString1.append("</a>");
                    out.write(pageString1.toString());
                    out.write("</td>        ");
                    
                    
                     out.write("<td>");
                        StringBuilder pageString=new StringBuilder();
                        pageString.append("<a href=\"");
                        pageString.append(action);
                        pageString.append("?");
                        pageString.append(var);
                        pageString.append("=");
                        pageString.append(pagecount);
                        pageString.append("\"><font color=#ff0000>[");
                        pageString.append("尾页");
                        pageString.append("]</font>");
                        pageString.append("</a>");
                    out.write(pageString.toString());
                    out.write("</td>        ");
               }
        }
            else
            {
                
                if (this.curpage==1)
                {
                    out.write("<td>");
                    out.write("<font color=#bbbbbb>[");
                    out.write("首页");
                    out.write("]</font>");
                    out.write("</td>        ");
                    out.write("<td>");
                    out.write("<font color=#bbbbbb>[");
                    out.write("第一页");
                    out.write("]</font>");
                    out.write("</td>        ");
                }
                if (this.curpage > 1 ){
                        out.write("<td>");
                        StringBuilder pageString=new StringBuilder();
                        pageString.append("<a href=\"");
                        pageString.append(action);
                        pageString.append("?");
                        pageString.append(var);
                        pageString.append("=1");
                        //pageString.append(this.curpage - 1);
                        pageString.append("\"><font color=#ff0000>[");
                        pageString.append("首页");
                        pageString.append("]</font>");
                        pageString.append("</a>");
                    out.write(pageString.toString());
                    out.write("</td>        ");
                        
                        out.write("<td>");
                            StringBuilder pageString2=new StringBuilder();
                            pageString2.append("<a href=\"");
                            pageString2.append(action);
                            pageString2.append("?");
                            pageString2.append(var);
                            pageString2.append("=");
                            pageString2.append(this.curpage - 1);
                            pageString2.append("\"><font color=#ff0000>[");
                            pageString2.append("上一页");
                            pageString2.append("]</font>");
                            pageString2.append("</a>");
                        out.write(pageString2.toString());
                        out.write("</td>        ");
                  }
                  if (this.curpage < this.pagecount){
                    out.write("<td>");
                        StringBuilder pageString3=new StringBuilder();
                        pageString3.append("<a href=\"");
                        pageString3.append(action);
                        pageString3.append("?");
                        pageString3.append(var);
                        pageString3.append("=");
                        pageString3.append(this.curpage + 1);
                        pageString3.append("\"><font color=#ff0000>[");
                        pageString3.append("下一页");
                        pageString3.append("]</font>");
                        pageString3.append("</a>");
                    out.write(pageString3.toString());
                    out.write("</td>        ");
                    
                    out.write("<td>");
                        StringBuilder pageString=new StringBuilder();
                        pageString.append("<a href=\"");
                        pageString.append(action);
                        pageString.append("?");
                        pageString.append(var);
                        pageString.append("=");
                        pageString.append(pagecount);
                        pageString.append("\"><font color=#ff0000>[");
                        pageString.append("尾页");
                        pageString.append("]</font>");
                        pageString.append("</a>");
                    out.write(pageString.toString());
                    out.write("</td>        ");
                  }
                if (this.curpage == this.pagecount){
                    out.write("<td>");
                    out.write("<font color=#bbbbbb>[");
                    out.write("下一页");
                    out.write("]</font>");
                    out.write("</td>        ");
                    out.write("<td>");
                    out.write("<font color=#bbbbbb>[");
                    out.write("尾页");
                    out.write("]</font>");
                    out.write("</td>        ");
                  }
                
            }
            out.write("</tr>    ");
            out.write("</table>    ");
        } catch (java.io.IOException ex) {
            throw new JspException(ex.getMessage());
        }
        
    }

    public void setAction(java.lang.String value) {
        this.action = value;
    }

    public void setPagecount(int value) {
        this.pagecount = value;
    }
    
    public void setCurpage(int value) {
        this.curpage = value;
    }

    public void setTarget(java.lang.String value) {
        this.target = value;
    }

    public void setVar(java.lang.String value) {
        this.var = value;
    }

    public int getStyle() {
        return style;
    }

    public void setStyle(int style) {
        this.style = style;
    }
}


2.接下来需要写一个Servlet,来让你的标签在web容器里能够运行~!

package com.xaccp.servlet;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;


@SuppressWarnings("serial")
public class PagesServlet extends HttpServlet {
    
   protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String curr=request.getParameter("page");
        if (curr==null)
        {
            curr="1";
        }
        request.setAttribute("curr",curr);
        RequestDispatcher rd=request.getRequestDispatcher("pagetest.jsp");
        rd.forward(request,response);
    }
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    public String getServletInfo() {
        return "Short description";
    }
}


3. 下来不用我说,大家都知道要在web.xml中配置这个Servlet,下面就是

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 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 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <servlet>
        <servlet-name>PagesServlet</servlet-name>
        <servlet-class>com.xaccp.servlet.PagesServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>PagesServlet</servlet-name>
        <url-pattern>/PagesServlet.go</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
	<welcome-file>
            index.jsp
        </welcome-file>
    </welcome-file-list>
</web-app>


4.下来也是最后一步了,就是我们的标签描述文件pages.tld咯,当然用xml文件来描述咯~!

<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.0" 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">
  <tlib-version>1.0</tlib-version>
  <short-name>p</short-name>
  <uri>/WEB-INF/tlds/pages</uri>
  <!-- A validator verifies that the tags are used correctly at JSP
         translation time. Validator entries look like this: 
      <validator>
          <validator-class>com.mycompany.TagLibValidator</validator-class>
          <init-param>
             <param-name>parameter</param-name>
             <param-value>value</param-value>
	  </init-param>
      </validator>
   -->
  <!-- A tag library can register Servlet Context event listeners in
        case it needs to react to such events. Listener entries look
        like this: 
     <listener>
         <listener-class>com.mycompany.TagLibListener</listener-class> 
     </listener>
   -->
  <tag>
    <name>page</name>
    <tag-class>com.xaccp.common.page.PagesTag</tag-class>
    <body-content>empty</body-content>
    <attribute>
      <name>action</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
      <type>java.lang.String</type>
    </attribute>
    <attribute>
      <name>pagecount</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
      <type>int</type>
    </attribute>
    <attribute>
      <name>curpage</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
      <type>int</type>
    </attribute>
    <attribute>
      <name>target</name>
      <rtexprvalue>true</rtexprvalue>
      <type>java.lang.String</type>
    </attribute>
    <attribute>
      <name>var</name>
      <rtexprvalue>true</rtexprvalue>
      <type>java.lang.String</type>
    </attribute>
    <attribute>
      <name>style</name>
      <rtexprvalue>true</rtexprvalue>
      <type>int</type>
    </attribute>
  </tag>
</taglib>


5.现在就大功告成了吗? 我们说不! 最重要的就是写个jsp来测试一下你的分页标签咯~!
名字就叫pagetest.jsp吧~
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@taglib prefix="p" uri="/WEB-INF/tlds/pages.tld" %>
<%--
The taglib directive below imports the JSTL library. If you uncomment it,
you must also add the JSTL library to the project. The Add Library... action
on Libraries node in Projects view can be used to add the JSTL 1.1 library.
--%>
<%--
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
--%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>

    <h1>JSP Page</h1>
    
    <p>
      <%--
    This example uses JSTL, uncomment the taglib directive above.
    To test, display the page like this: index.jsp?sayHello=true&name=Murphy
    --%>
      <%--
    <c:if test="${param.sayHello}">
        <!-- Let's welcome the user ${param.name} -->
        Hello ${param.name}!
    </c:if>
    --%>
</p>
    <p:page action="PagesServlet.go" curpage="${curr}" pagecount="10" style="2" target="_self" var="page"/>
    <p>&nbsp;</p>
    </body>
</html>


技巧:大家试着改变style的值,看看页面有什么变化~!

6.好,现在大功告成,打游戏去啦~!

你可能感兴趣的:(java,游戏,jsp,Web,servlet)