简易java分页标签
1,标签的实现类NumenTag.java
java 代码
1. import java.util.ArrayList;
2. import java.util.HashMap;
3. import java.util.List;
4. import java.util.Map;
5.
6. import javax.servlet.jsp.JspException;
7. import javax.servlet.jsp.JspWriter;
8. import javax.servlet.jsp.tagext.TagSupport;
9.
10. /** *//**
11. * 自定义的分页标签
12. *
13. * @author 马万林
14. *
15. */
16. public class NumenTag extends TagSupport {
17.
18. private String url = null;
19.
20. private Result result;
21.
22. private String page = null;
23.
24. private String[] p;
25.
26. private Map kv = new HashMap();
27.
28. private List key = null;
29.
30. /** *//**
31. * 分离参数
32. * @param params
33. */
34. public void setParams(String params) {
35. key = new ArrayList();
36. p = params.split(";");
37. for (int i = 0; p.length>1&&i < p.length; i++) {
38. kv.put(p[i].split(":")[0], p[i].split(":")[1]);
39. key.add(p[i].split(":")[0]);
40. }
41. }
42.
43. /** *//**
44. * 分页的结果
45. * @param result
46. */
47. public void setResult(String result) {
48. this.result = (Result) pageContext.getRequest().getAttribute("result");
49. this.page = (String) pageContext.getRequest().getAttribute("page");
50. }
51.
52. public void setUrl(String url) {
53. this.url = url;
54. }
55.
56. public int doStartTag() throws JspException {
57. StringBuffer sb = null;
58. if (result != null && result.getContent() != null
59. && result.getContent().size() > 0) {
60. sb = new StringBuffer("分页: ");
61. if (result.getPage().getHasPrePage()){
62. sb.append("<a href='").append(url);
63. if (url.indexOf("?") == -1)
64. sb.append("?page=1");
65. else
66. sb.append("&page=1");
67. linkParams(sb);
68. sb.append("'>").append("首页").append("</a>").append(" | ");
69. sb.append("<a href='").append(url);
70. if (url.indexOf("?") == -1)
71. sb.append("?page=").append(
72. result.getPage().getPreviousPage());
73. else
74. sb.append("&page=").append(
75. result.getPage().getPreviousPage());
76. linkParams(sb);
77. sb.append("'>").append("上页").append("</a>").append(" | ");
78. }
79. if (result.getPage().getHasNextPage()) {
80. sb.append("<a href='").append(url);
81. if (url.indexOf("?") == -1)
82. sb.append("?page=").append(result.getPage().getNextPage());
83. else
84. sb.append("&page=").append(result.getPage().getNextPage());
85. linkParams(sb);
86. sb.append("'>").append("下页").append("</a>").append(" | ");
87. sb.append("<a href='").append(url);
88. if (url.indexOf("?") == -1)
89. sb.append("?page=").append(result.getPage().getTotalPage());
90. else
91. sb.append("&page=").append(result.getPage().getTotalPage());
92. linkParams(sb);
93. sb.append("'>").append("尾页").append("</a>");
94. }
95. sb.append(" 跳转到:《select name='goToPage' οnchange='javascript:goPage(this.value)'》");
96. int s = result.getPage().getTotalCount();
97. for (int i = 1; i <= s; i++) {
98. sb.append("<option value=").append(i);
99. if (Integer.parseInt(page) == i)
100. sb.append(" selected");
101. sb.append(">第").append(i).append("页</option>");
102. }
103. sb.append("</select>");
104. sb.append("<script language=javascript>");
105. sb.append("function goPage(page){").append("window.location='")
106. .append(url);
107. if (url.indexOf("?") == -1)
108. sb.append("?page='+").append("page");
109. else
110. sb.append("&page='+").append("page");
111. sb.append("+'");
112. linkParams(sb);
113. sb.append("';");
114. sb.append("}");
115. sb.append("</script>");
116. }
117. try {
118. if (sb != null) {
119. JspWriter out = pageContext.getOut();
120. out.print(sb);
121. }
122. } catch (Exception e) {
123. throw new JspException(e);
124. }
125. return EVAL_PAGE;
126. }
127.
128. /** *//**
129. * 链接参数处理
130. * @param sb
131. */
132. private void linkParams(StringBuffer sb) {
133. for (int i = 0; i < key.size(); i++) {
134. sb.append("&").append(key.get(i)).append("=").append(
135. kv.get(key.get(i)));
136. }
137. }
138.
139. }
2,标签定义文件numentag.tld
xml 代码
1. <?xml version="1.0" encoding="utf-8" ?>
2. <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
3. <taglib>
4. <tlib-version>1.0</tlib-version>
5. <jsp-version>1.2</jsp-version>
6. <short-name>numen</short-name>
7. <!--OutputTag-->
8. <tag>
9. <name>out</name>
10. <tag-class>cn.cityyouth.util.NumenTag</tag-class>
11. <body-content>empty</body-content>
12. <attribute>
13. <name>url</name>
14. <required>true</required>
15. <rtexprvalue>true</rtexprvalue>
16. </attribute>
17. <attribute>
18. <name>result</name>
19. <required>true</required>
20. <rtexprvalue>true</rtexprvalue>
21. </attribute>
22. <attribute>
23. <name>params</name>
24. <required>false</required>
25. <rtexprvalue>true</rtexprvalue>
26. </attribute>
27. </tag>
28. </taglib>
3.js
xml 代码
1. <%@ page contentType="text/html; charset=utf-8"%>
2. <%@ include file="/common/taglibs.jsp"%>
3. <%@ taglib uri="/WEB-INF/numentag.tld" prefix="n"%>
4. <html>
5. <head>
6. </head>
7. <body>
8. <table>
9. <tr><td>
10. <n:out url="test.do" result="result" params="${type}"/>
11. </td></tr>
12. </table>
13. </body>
14. </html>