<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"> <taglib> <tlib-version>2.2.3</tlib-version> <jsp-version>1.2</jsp-version> <short-name>tangs</short-name> <uri>/tangs</uri> <display-name>"Tangs Tags"</display-name> <tag> <name>pages</name> <tag-class>com.tangs.tag.PageTag</tag-class> //標簽類 <body-content>empty</body-content> <attribute> //標簽中的屬性 <name>cpage</name> //屬性名 <required>true</required> //是否必須 <rtexprvalue>true</rtexprvalue> //表示該自定義標簽的某屬性的值可以直接指定或者通過動態計算指定 </attribute> <attribute> <name>total</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>url</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib> <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"><taglib> <tlib-version>2.2.3</tlib-version> <jsp-version>1.2</jsp-version> <short-name>tangs</short-name> <uri>/tangs</uri> <display-name>"Tangs Tags"</display-name> <tag> <name>pages</name> <tag-class>com.tangs.tag.PageTag</tag-class> //標簽類 <body-content>empty</body-content> <attribute> //標簽中的屬性 <name>cpage</name> //屬性名 <required>true</required> //是否必須 <rtexprvalue>true</rtexprvalue> //表示該自定義標簽的某屬性的值可以直接指定或者通過動態計算指定 </attribute> <attribute> <name>total</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>url</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag></taglib> 2.編寫標簽類 PageTag.java Java代碼 package com.tangs.tag; import com.opensymphony.xwork2.util.ValueStack; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.components.Component; import org.apache.struts2.views.jsp.ComponentTagSupport; /** * 分頁標簽 * @author tangs */ public class PageTag extends ComponentTagSupport { private String cpage; //當前頁 private String total; //總頁數 private String url; //請求地址 public void setCpage(String cpage) { this.cpage = cpage; } public void setTotal(String total) { this.total = total; } public void setUrl(String url) { this.url = url; } @Override public Component getBean(ValueStack arg0, HttpServletRequest arg1, HttpServletResponse arg2) { return new Pages(arg0); //返回Pages Component,分頁的邏輯處理都在這個Component中 } //獲得參數 protected void populateParams() { super.populateParams(); Pages pages = (Pages)component; pages.setCpage(cpage); pages.setTotal(total); pages.setUrl(url); } } package com.tangs.tag;import com.opensymphony.xwork2.util.ValueStack;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts2.components.Component;import org.apache.struts2.views.jsp.ComponentTagSupport;/** * 分頁標簽 * @author tangs */public class PageTag extends ComponentTagSupport { private String cpage; //當前頁 private String total; //總頁數 private String url; //請求地址 public void setCpage(String cpage) { this.cpage = cpage; } public void setTotal(String total) { this.total = total; } public void setUrl(String url) { this.url = url; } @Override public Component getBean(ValueStack arg0, HttpServletRequest arg1, HttpServletResponse arg2) { return new Pages(arg0); //返回Pages Component,分頁的邏輯處理都在這個Component中 } //獲得參數 protected void populateParams() { super.populateParams(); Pages pages = (Pages)component; pages.setCpage(cpage); pages.setTotal(total); pages.setUrl(url); }} Pages.java Java代碼 Struts2中實現自定義分頁標簽2 - thinking - ˋ●轉變_; 的博客 package com.tangs.tag; import com.opensymphony.xwork2.util.ValueStack; import java.io.IOException; import java.io.Writer; import java.util.logging.Level; import java.util.logging.Logger; import org.apache.struts2.components.Component; /** * 分頁邏輯Bean * @author tangs */ public class Pages extends Component { private String cpage; private String total; private String url; public String getCpage() { return cpage; } public void setCpage(String cpage) { this.cpage = cpage; } public String getTotal() { return total; } public void setTotal(String total) { this.total = total; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public Pages(ValueStack arg0) { super(arg0); } @Override public boolean start(Writer writer) { boolean result = super.start(writer); try { StringBuilder str = new StringBuilder(); boolean isValid = true; //從ValueStack中取出數值 if (isValid) { if (total.startsWith("%{") && total.endsWith("}")) { total = total.substring(2, total.length() -1); total = (String)this.getStack().findValue(total); isValid = total == null ? false : true; } else { isValid = false; } } if (isValid) { if (cpage.startsWith("%{") && cpage.endsWith("}")) { cpage = cpage.substring(2, cpage.length() - 1); cpage = (String)this.getStack().findValue(cpage); isValid = cpage == null ? false : true; } else { isValid = false; } } if (isValid) { if (url.startsWith("%{") && url.endsWith("}")) { url = url.substring(2, url.length() - 1); url = (String)this.getStack().findValue(url); isValid = url == null ? false : true; } else { isValid = false; } } if (isValid) { Integer cpageInt = Integer.valueOf(cpage); //當前頁與總頁數相等 if (cpage.equals(total)) { //如果total = 1,則無需分頁,顯示“[第1頁] [共1頁]” if ("1".equals(total)) { str.append("[第 " + cpage + " 頁]"); str.append(" [共 " + total + " 頁]"); } else { //到達最後一頁,顯示“[首頁] [上一頁] [末頁]” str.append("<a href='"); str.append(url); str.append("?cpage=1&total="+total+"&url="+url); str.append("'>[首頁]</a> <a href='"); str.append(url); str.append("?cpage=" + (cpageInt - 1) + "&total=" + total+"&url="+url); str.append("'>[上一頁]</a> <a href='"); str.append(url); str.append("?cpage=" + total + "&total=" + total+"&url="+url); str.append("'>[末頁]</a>"); } } else { //當前頁與總頁數不相同 if ("1".equals(cpage)) { //第一頁,顯示“[首頁] [下一頁] [末頁]” str.append("<a href='"); str.append(url); str.append("?cpage=1&total="+total+"&url="+url); str.append("'>[首頁]</a> <a href='"); str.append(url); str.append("?cpage=" + (cpageInt + 1) + "&total=" + total+"&url="+url); str.append("'>[下一頁]</a> <a href='"); str.append(url); str.append("?cpage=" + total + "&total=" + total+"&url="+url); str.append("'>[末頁]</a>"); } else { //不是第一頁,顯示“[首頁] [上一頁] [下一頁] [末頁]” str.append("<a href='"); str.append(url); str.append("?cpage=1&total="+total+"&url="+url); str.append("'>[首頁]</a> <a href='"); str.append(url); str.append("?cpage=" + (cpageInt - 1) + "&total=" + total+"&url="+url); str.append("'>[上一頁]</a> <a href='"); str.append(url); str.append("?cpage=" + (cpageInt + 1) + "&total=" + total+"&url="+url); str.append("'>[下一頁]</a> <a href='"); str.append(url); str.append("?cpage=" + total + "&total=" + total+"&url="+url); str.append("'>[末頁]</a>"); } } } writer.write(str.toString()); } catch (IOException ex) { Logger.getLogger(Pages.class.getName()).log(Level.SEVERE, null, ex); } return result; } } package com.tangs.tag;import com.opensymphony.xwork2.util.ValueStack;import java.io.IOException;import java.io.Writer;import java.util.logging.Level;import java.util.logging.Logger;import org.apache.struts2.components.Component;/** * 分頁邏輯Bean * @author tangs */public class Pages extends Component { private String cpage; private String total; private String url; public String getCpage() { return cpage; } public void setCpage(String cpage) { this.cpage = cpage; } public String getTotal() { return total; } public void setTotal(String total) { this.total = total; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public Pages(ValueStack arg0) { super(arg0); } @Override public boolean start(Writer writer) { boolean result = super.start(writer); try { StringBuilder str = new StringBuilder(); boolean isValid = true; //從ValueStack中取出數值 if (isValid) { if (total.startsWith("%{") && total.endsWith("}")) { total = total.substring(2, total.length() -1); total = (String)this.getStack().findValue(total); isValid = total == null ? false : true; } else { isValid = false; } } if (isValid) { if (cpage.startsWith("%{") && cpage.endsWith("}")) { cpage = cpage.substring(2, cpage.length() - 1); cpage = (String)this.getStack().findValue(cpage); isValid = cpage == null ? false : true; } else { isValid = false; } } if (isValid) { if (url.startsWith("%{") && url.endsWith("}")) { url = url.substring(2, url.length() - 1); url = (String)this.getStack().findValue(url); isValid = url == null ? false : true; } else { isValid = false; } } if (isValid) { Integer cpageInt = Integer.valueOf(cpage); //當前頁與總頁數相等 if (cpage.equals(total)) { //如果total = 1,則無需分頁,顯示“[第1頁] [共1頁]” if ("1".equals(total)) { str.append("[第 " + cpage + " 頁]"); str.append(" [共 " + total + " 頁]"); } else { //到達最後一頁,顯示“[首頁] [上一頁] [末頁]” str.append("<a href='"); str.append(url); str.append("?cpage=1&total="+total+"&url="+url); str.append("'>[首頁]</a> <a href='"); str.append(url); str.append("?cpage=" + (cpageInt - 1) + "&total=" + total+"&url="+url); str.append("'>[上一頁]</a> <a href='"); str.append(url); str.append("?cpage=" + total + "&total=" + total+"&url="+url); str.append("'>[末頁]</a>"); } } else { //當前頁與總頁數不相同 if ("1".equals(cpage)) { //第一頁,顯示“[首頁] [下一頁] [末頁]” str.append("<a href='"); str.append(url); str.append("?cpage=1&total="+total+"&url="+url); str.append("'>[首頁]</a> <a href='"); str.append(url); str.append("?cpage=" + (cpageInt + 1) + "&total=" + total+"&url="+url); str.append("'>[下一頁]</a> <a href='"); str.append(url); str.append("?cpage=" + total + "&total=" + total+"&url="+url); str.append("'>[末頁]</a>"); } else { //不是第一頁,顯示“[首頁] [上一頁] [下一頁] [末頁]” str.append("<a href='"); str.append(url); str.append("?cpage=1&total="+total+"&url="+url); str.append("'>[首頁]</a> <a href='"); str.append(url); str.append("?cpage=" + (cpageInt - 1) + "&total=" + total+"&url="+url); str.append("'>[上一頁]</a> <a href='"); str.append(url); str.append("?cpage=" + (cpageInt + 1) + "&total=" + total+"&url="+url); str.append("'>[下一頁]</a> <a href='"); str.append(url); str.append("?cpage=" + total + "&total=" + total+"&url="+url); str.append("'>[末頁]</a>"); } } } writer.write(str.toString()); } catch (IOException ex) { Logger.getLogger(Pages.class.getName()).log(Level.SEVERE, null, ex); } return result; }} 3.服務端 服務端主要是獲得請求然後轉向顯示的頁面 DisplayAction.java Java代碼 /* To change this template, choose Tools | Templates * and open the template in the editor. */ package com.tangs.action; import com.opensymphony.xwork2.ActionSupport; /** * * @author tangs */ public class DisplayAction extends ActionSupport { private String cpage; private String total; private String url; public String list() { //Get data from server //... //這裡沒有做任何邏輯,直接跳轉到了頁面 return SUCCESS; } public String getCpage() { return cpage; } public void setCpage(String cpage) { this.cpage = cpage; } public String getTotal() { return total; } public void setTotal(String total) { this.total = total; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } } /* * To change this template, choose Tools | Templates * and open the template in the editor. */package com.tangs.action;import com.opensymphony.xwork2.ActionSupport;/** * * @author tangs */public class DisplayAction extends ActionSupport { private String cpage; private String total; private String url; public String list() { //Get data from server //... //這裡沒有做任何邏輯,直接跳轉到了頁面 return SUCCESS; } public String getCpage() { return cpage; } public void setCpage(String cpage) { this.cpage = cpage; } public String getTotal() { return total; } public void setTotal(String total) { this.total = total; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } } struts.xml Xml代碼 Struts2中實現自定義分頁標簽2 - thinking - ˋ●轉變_; 的博客 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd" > <struts> <package name="tagTest" extends="struts-default"> <action name="list" class="com.tangs.action.DisplayAction"> <result name="success">/list.jsp</result> </action> </package> </struts> <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd" ><struts> <package name="tagTest" extends="struts-default"> <action name="list" class="com.tangs.action.DisplayAction"> <result name="success">/list.jsp</result> </action> </package></struts> 4.在頁面中使用標簽 list.jsp Html代碼 Struts2中實現自定義分頁標簽2 - thinking - ˋ●轉變_; 的博客 <%@page contentType="text/html" pageEncoding="UTF-8"%> <%@ taglib prefix="tangs" uri="/WEB-INF/tangs.tld"%> <%@ taglib prefix="s" uri="/WEB-INF/struts-tags.tld"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>List</title> </head> <body> 共 <s:property value="total"/> 頁 第 <s:property value="cpage"/> 頁<br> <tangs:pages cpage="%{cpage}" total="%{total}" url="%{url}"/> </body> </html>