备份一下,以后继续学习
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.[ 首页 下一页 末页 ] 的文字分页
相关推荐
Struts2自定义分页标签很详细了,新手可以少走弯路,节约时间 资源包括了后台如何操作。
基于struts2的自定义分页标签实现的一个demo,连接数据库为Oracle
Struts2中实现自定义标签很简单,主要分为3步: 1.创建taglib文件(.tld),编写标签声明。 2.编写自定义标签类。 3.在页面中使用标签。 下面以一个自定义的分页标签为例,进行说明。 其实,开发自定义标签并不...
自定义struts2 分页标签,值得学习..
Struts 2 自定义标签分页,有简单上下分页和数字分页
标签源代码 博文链接:https://tangshuo.iteye.com/blog/189820
自已写的一个jsp自定义分页器zoo-paging-1.0.jar,附带有源码以及例子,希望对大家有用!
下面是我用Struts2做的一个分页显示实例,基本的思路是:把数据库表中的每一行数据封装成一个对象,用一个返回类型为List的方法返回这些对象,接着在Struts2的action里面定义一个List属性,用这个List来接收从数据库...
struts+hibernate+自定义标签 做的分页模板
自定义标签实现分页,分页信息都封装在一个继承自TagSupport的java文件中,自定义一个tld文件,jsp页面通过taglib指令引入tld文件,使用自定义标签。
JSP自定义标签分页+Struts1.x标签,包含标签分页类、标签tld文件、web.xml配置和调用例子,非常地简单。 只要在Action传request.setAttribute("listUser", listUser);到JSP即可实现自动分页,输出分页样式可以自己去...
jsp使用自定义标签taglib分页系列——完整例子(其中struts控件和包我没有加入,需要自己加入)
本网上书店系统采用MVC设计模式,用Struts控制页面流程,Hibernate做对象持久化操作,分页功能是用自定义标签,在用户的登录过程中还用到了验证码功能,用过滤器过滤用户的权限!
jquery java struts2 实现分页 非常好看 可自定义修改样式
在应用Struts框架的情况下,用自定义标签将查到的数据进行分页显示。