备份一下,以后继续学习
1.类似百度的数字分页
2.首页、上一页、下一页、末页
我做的例子是Eclipse环境下的
这是我的结构图:
以下是代码:
1 . web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<jsp-config>
<taglib>
<taglib-uri>/WEB-INF/struts-tags.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-tags.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>
我自定义的tld文件
2 . wangs.tld
<?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>wangs</short-name>
<uri>/wangs</uri>
<tag>
<name>pages</name>
<tag-class>com.wangs.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>visitUrl</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>styleClass</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>theme</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
3 . struts-tags.tld 从struts2-core-2.1.8.1.jar 中复制
4 . struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="TagDemo" namespace="/" extends="struts-default">
<action name="list"
class="action.DemoAction" method="execute">
<result name="success">/list.jsp</result>
</action>
</package>
</struts>
5 . PageTag.java Bean
package com.wangs.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;
/**
* 分页标签
*/
public class PageTag extends ComponentTagSupport {
private String cpage;
private String total;
private String visitUrl;
private String styleClass;
private String theme;
public void setCpage(String cpage) {
this.cpage = cpage;
}
public void setTotal(String total) {
this.total = total;
}
public void setVisitUrl(String visitUrl) {
this.visitUrl = visitUrl;
}
public void setStyleClass(String styleClass) {
this.styleClass = styleClass;
}
public void setTheme(String theme) {
this.theme = theme;
}
@Override
public Component getBean(ValueStack arg0, HttpServletRequest arg1,
HttpServletResponse arg2) {
return new Pages(arg0, arg1);
}
protected void populateParams() {
super.populateParams();
Pages pages = (Pages) component;
pages.setCpage(cpage);
pages.setTotal(total);
pages.setVisitUrl(visitUrl);
pages.setStyleClass(styleClass);
pages.setTheme(theme);
}
}
6 . Pages.java 逻辑类
package com.wangs.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 javax.servlet.http.HttpServletRequest;
import org.apache.struts2.components.Component;
/**
* 分页逻辑
*/
public class Pages extends Component {
private HttpServletRequest request;
private String cpage;
private String total;
private String visitUrl;
private String styleClass;
private String theme;
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 getVisitUrl() {
return visitUrl;
}
public void setVisitUrl(String visitUrl) {
this.visitUrl = visitUrl;
}
public String getStyleClass() {
return styleClass;
}
public void setStyleClass(String styleClass) {
this.styleClass = styleClass;
}
public String getTheme() {
return theme;
}
public void setTheme(String theme) {
this.theme = theme;
}
public Pages(ValueStack arg0, HttpServletRequest request) {
super(arg0);
this.request = request;
}
@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 (visitUrl.startsWith("%{") && visitUrl.endsWith("}")) {
visitUrl = visitUrl.substring(2, visitUrl.length() - 1);
visitUrl = (String) this.getStack().findValue(visitUrl);
isValid = visitUrl == null ? false : true;
} else {
isValid = false;
}
}
if (isValid) {
Integer cpageInt = Integer.valueOf(cpage);
str.append("<span ");
if (styleClass != null) {
str.append(" class='" + styleClass + "'>");
} else {
str.append(">");
}
// 文本样式
if (theme == null || "text".equals(theme)) {
// 当前页与总页数相等
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(visitUrl);
str.append("?cpage=1&total=" + total + "&visitUrl="
+ visitUrl);
str.append("'>[首页]</a> <a href='");
str.append(visitUrl);
str.append("?cpage=" + (cpageInt - 1) + "&total="
+ total + "&visitUrl=" + visitUrl);
str.append("'>[上一页]</a> <a href='");
str.append(visitUrl);
str.append("?cpage=" + total + "&total=" + total
+ "&visitUrl=" + visitUrl);
str.append("'>[末页]</a>");
}
} else {
// 当前页与总页数不相同
if ("1".equals(cpage)) {
// 第一页,显示“[首页] [下一页] [末页]”
str.append("<a href='");
str.append(visitUrl);
str.append("?cpage=1&total=" + total + "&visitUrl="
+ visitUrl);
str.append("'>[首页]</a> <a href='");
str.append(visitUrl);
str.append("?cpage=" + (cpageInt + 1) + "&total="
+ total + "&visitUrl=" + visitUrl);
str.append("'>[下一页]</a> <a href='");
str.append(visitUrl);
str.append("?cpage=" + total + "&total=" + total
+ "&visitUrl=" + visitUrl);
str.append("'>[末页]</a>");
} else {
// 不是第一页,显示“[首页] [上一页] [下一页] [末页]”
str.append("<a href='");
str.append(visitUrl);
str.append("?cpage=1&total=" + total + "&visitUrl="
+ visitUrl);
str.append("'>[首页]</a> <a href='");
str.append(visitUrl);
str.append("?cpage=" + (cpageInt - 1) + "&total="
+ total + "&visitUrl=" + visitUrl);
str.append("'>[上一页]</a> <a href='");
str.append(visitUrl);
str.append("?cpage=" + (cpageInt + 1) + "&total="
+ total + "&visitUrl=" + visitUrl);
str.append("'>[下一页]</a> <a href='");
str.append(visitUrl);
str.append("?cpage=" + total + "&total=" + total
+ "&visitUrl=" + visitUrl);
str.append("'>[末页]</a>");
}
}
} else if ("number".equals(theme)) { // 数字样式 [1 2 3 4 5 6 7 8 9
// 10 > >>]
Integer totalInt = Integer.valueOf(total);
// 如果只有一页,则无需分页
str.append("[ ");
if (totalInt == 1) {
str.append("<strong>1</strong> ");
} else {
// 计算一共分几组
int group = (totalInt - 1) / 10 + 1;
// 当前第几组
int cgroup = (cpageInt - 1) / 10 + 1;
if (cgroup > 1) {
// 当前不是第一组,要显示“<< <”
// <<:返回前一组第一页
// <:返回前一页
str.append("<a href='");
str.append(visitUrl);
str.append("?cpage=" + ((cgroup - 2) * 10 + 1)
+ "&total=" + total + "&visitUrl=" + visitUrl);
str.append("'>«</a> ");
str.append("<a href='");
str.append(visitUrl);
str.append("?cpage=" + (cpageInt - 1) + "&total="
+ total + "&visitUrl=" + visitUrl);
str.append("'>‹</a> ");
}
// 10个为一组显示
for (int i = (cgroup - 1) * 10 + 1; i <= totalInt
&& i <= cgroup * 10; i++) {
if (cpageInt == i) { // 当前页要加粗显示
str.append("<strong>");
}
str.append("<a href='");
str.append(visitUrl);
str.append("?cpage=" + i + "&total=" + total
+ "&visitUrl=" + visitUrl);
str.append("'>" + i + "</a> ");
if (cpageInt == i) {
str.append("</strong>");
}
}
// 如果多于1组并且不是最后一组,显示“> >>”
if (group > 1 && cgroup != group) {
// >>:返回下一组最后一页
// >:返回下一页
str.append("<a href='");
str.append(visitUrl);
str.append("?cpage=" + (cpageInt + 1) + "&total="
+ total + "&visitUrl=" + visitUrl);
str.append("'>›</a> ");
str.append("<a href='");
str.append(visitUrl);
str.append("?cpage="
+ ((cgroup * 10 + 10) > totalInt ? totalInt
: (cgroup * 10 + 10)) + "&total="
+ total + "&visitUrl=" + visitUrl);
str.append("'>»</a> ");
}
}
str.append("]");
}
str.append("</span>");
}
writer.write(str.toString());
} catch (IOException ex) {
Logger.getLogger(Pages.class.getName()).log(Level.SEVERE, null, ex);
}
return result;
}
}
7 . DemoAction.java
package action;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
public class DemoAction extends ActionSupport {
private static final long serialVersionUID = 2120921059932936210L;
private String cpage;
private String total;
private String visitUrl;
public String execute() {
// 测试地址:
// (1).当前页第一页,共5页
// http://localhost:8080/TagDemo/list?cpage=1&total=5&visitUrl=list
// (2).当前页第一页,共18页
// http://localhost:8080/TagDemo/list?cpage=1&total=18&visitUrl=list
return Action.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 getVisitUrl() {
return visitUrl;
}
public void setVisitUrl(String visitUrl) {
this.visitUrl = visitUrl;
}
}
8 . 页面 list.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="wangs" uri="/WEB-INF/wangs.tld"%>
<%@ taglib prefix="s" uri="/WEB-INF/struts-tags.tld"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="css/style.css" rel="stylesheet" type="text/css"/>
<title>List</title>
</head>
<body>
共 <s:property value="total"/> 页 第 <s:property value="cpage"/> 页<br><br>
<wangs:pages cpage="%{cpage}" total="%{total}" visitUrl="%{visitUrl}" styleClass="page" theme="text"/>
</body>
</html>
theme 控制分页的形式:1.类似百度的数字分页 2.[ 首页 下一页 末页 ] 的文字分页