第一步:建立分页实体page类

 package com.soda.util;
/**
* @description 分页实体类
* @author line
* @time 2016年8月28日11:16:50
*/
public class Page {
private int pageSize;//每页记录per
private int totalSize;//总记录total
private int currentPage;//当前页page
private int totalPage;//总页数lastpage
private String url;//要链接的sevlet
public Page() {
// TODO Auto-generated constructor stub
}
public Page(int pageSize, int totalSize, int currentPage, int totalPage,
String url) {
super();
this.pageSize = pageSize;
this.totalSize = totalSize;
this.currentPage = currentPage;
this.totalPage = totalPage;
this.url = url;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalSize() {
return totalSize;
}
public void setTotalSize(int totalSize) {
this.totalSize = totalSize;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
if (currentPage<=0) {
this.currentPage=1;
}else if(totalPage<=0){
this.currentPage = currentPage;
}else if (currentPage>=totalPage) {
this.currentPage=totalPage;
}else {
this.currentPage = currentPage;
}
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
} }

第二步:建立分页标签类PageTag(页码样式自己可以更改)

 package com.soda.util;

 import java.io.IOException;

 import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport; @SuppressWarnings("serial")
public class PageTag extends TagSupport {
private Page pi;// 持有页码对象的引用 public Page getPi() {
return pi;
} public void setPi(Page pi) {
this.pi = pi;
} @Override
public int doStartTag() throws JspException {
StringBuffer txt = new StringBuffer();
if (pi.getTotalSize() < 1) {
PageContext pc = this.pageContext;
JspWriter out = pc.getOut();
txt.append("<br/><div style='font-size:25px;text-align: center'>暂无查询结果</div>");
try {
out.append(txt.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
txt.append("<form action='" + pi.getUrl() + "' method='post'>");
txt.append("<table width='100%' border='0' cellspacing='0' cellpadding='0'>");
txt.append("<tr>");
txt.append("<td width='20%' height='25'>");
txt.append("<table border='0' cellspacing='0' cellpadding='3'>");
txt.append("<tr>");
txt.append("<td style='width:60px'></td>"); if (pi.getCurrentPage() == 1) {
txt.append("<td style='width:60px'><span>首页</span></td> ");
txt.append("<td style='width:60px'><span>上一页</span></td> ");
} else {
txt.append("<td style='width:60px'><a href='" + pi.getUrl()
+ "&currentPage=1'>首页</a></td> ");
txt.append("<td style='width:60px'><a href='" + pi.getUrl()
+ "&currentPage=" + (pi.getCurrentPage() - 1)
+ "'>上一页</a></td> ");
}
for (int i = 1; i <= pi.getTotalPage(); i++) {
if (pi.getCurrentPage() == i) {
txt.append("<td style='width:30px'>" + i + "</td>");
continue;
}
if (Math.abs(pi.getCurrentPage() - i) <= 3) {
txt.append("<td style='width:30px'><a href='" + pi.getUrl()
+ "&currentPage=" + i + "'>" + i + "</a></td> ");
continue;
}
if (Math.abs(pi.getCurrentPage() - i) < 7) {
txt.append("<td style='width:30px'>.</td>");
continue;
}
}
if (pi.getCurrentPage() >= pi.getTotalPage()) {
txt.append("<td style='width:60px'><span>下一页</span></td>");
txt.append("<td style='width:60px'><span>尾页</span></td>");
} else {
txt.append("<td style='width:60px'><a href='" + pi.getUrl()
+ "&currentPage=" + (pi.getCurrentPage() + 1)
+ "'>下一页</a></td>");
txt.append("<td style='width:60px'><a href='" + pi.getUrl()
+ "&currentPage=" + pi.getTotalPage()
+ "'>尾页</a></td> ");
}
txt.append("</tr></table></td>");
txt.append("<td width='20%' align='center'><table border='0' cellspacing='0' cellpadding='3'>"); txt.append("<tr>");
txt.append("<td>");
txt.append("<input type='image' src='houtai/images/next.gif' width='16' height='16' border='0' onclick='return tiao()'/>");
txt.append("<td><input name='currentPage' size='3' id='cid' style='width:25;height:18'/>/页</td> ");
txt.append("</tr></table></td>"); txt.append(" <td width='20%' align='right'>共" + pi.getTotalSize()
+ "条记录显示到" + pi.getCurrentPage() + "/" + pi.getTotalPage()
+ "</td>");
txt.append("</tr></table></form>");
PageContext pc = this.pageContext;
JspWriter out = pc.getOut();
try {
out.print("<script> function tiao(){"
+ "var id=document.getElementById('cid').value; "
+ "if(id==null||id.length==0){"
+ "alert('请输入页码'); return false;} " + "if(isNaN(id)){"
+ "alert('请输入数字'); return false;} "
+ "if(id.indexOf(' ')>=0){"
+ "alert('页码不能是空或含有有空格!'); return false;}"
+ "if(parseInt(id)<1||parseInt(id)>"
+ pi.getTotalPage() + "){"
+ "alert('您输入的页码不在页码范围之内!'); return false;}}"
+ "</script>");
out.print("<style>a{text-decoration: none;} "
+ "a:visited{color:#333333;}" + "</style>");
out.append(txt.toString()); } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return Tag.EVAL_PAGE;
}
}

第三步:建立分页标签pageTag.tld文件

 <?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee " version="2.1"> <description>pageTag</description>
<display-name>pageTag</display-name>
<tlib-version>1.1</tlib-version>
<short-name>PersonTag</short-name>
<uri>http://page/PersonTag</uri> <tag>
<name>page</name>
<tag-class>com.soda.util.PageTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>pi</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>

第四步:jsp页面运用(页面顶部引入标签,页面底部引用标签)注意:是红色字体所在行

 <%@page import="com.soda.bean.User"%>
<%@page import="com.soda.bean.UserCondition"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://page/PersonTag" prefix="page" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head> <title></title>
<link href="houtai/css/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="houtai/js/main.js"></script>
<script type="text/javascript">
var xmlhttp;
var useid;
function forbiduser(id){
useid=id;
var ss=document.getElementById("userStateId"+id).innerHTML;
var s;
var ustate;
if(ss=="启用"){
ustate=1;
s="禁用";
}else{
ustate=0;
s="启用";
}
if (confirm('是否'+s+'?')){
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=res;
xmlhttp.open("get", "<%=basePath%>UserServlet?method=forbiduser&useId="+id+"&useState="+ustate, true);
xmlhttp.send(null);
} }
function res() {
if(xmlhttp.readyState==4&&xmlhttp.status==200){
var txt = xmlhttp.responseText;
if(txt=="禁用"){
document.getElementById("btstate"+useid).innerHTML="启用";
document.getElementById("userStateId"+useid).innerHTML="禁用";
}
if(txt=="启用"){
document.getElementById("btstate"+useid).innerHTML="禁用";
document.getElementById("userStateId"+useid).innerHTML="启用";
}
}
}
//全选与反选
window.onload = function(){
document.getElementById("userids").onclick = function(){
var flag = this.checked;
var items = document.getElementsByName("items");
for(var i = 0; i < items.length; i++){
items[i].checked = flag;
}
}
var items = document.getElementsByName("items");
for(var i = 0; i < items.length; i++){
items[i].onclick = function(){
//记录有多少个 items 被选中了
var number = 0;
for(var j = 0; j < items.length; j++){
if(items[j].checked){
number++;
}
}
document.getElementById("userids").checked = (items.length == number);
}
}
}
</script>
</head>
<body><%-- <c:set value="${sessionScope.uc }" var="uu"></c:set> --%>
<table width="100%" height="100%" border="0" cellspacing="0" cellpadding="3">
<tr>
<td style="height:25px; background-color:#f3f3f3; font-weight:bold" valign="top">
&nbsp;&nbsp;&nbsp;&nbsp;当前位置:
<img src="houtai/images/arrow.gif" align="absmiddle">&nbsp;&nbsp;soda创业教育平台&nbsp;&nbsp;
<img src="houtai/images/arrow.gif" align="absmiddle">&nbsp;&nbsp;后台管理&nbsp;&nbsp;
<img src="houtai/images/arrow.gif" align="absmiddle">&nbsp;&nbsp;用户管理</td>
</tr>
<tr>
<td style="height:34px; background-image:url(houtai/images/main_header.gif); border-bottom:1px solid #cfd8e0; padding:0px">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="20" height="34"><img src="houtai/images/main_headerL.gif" width="20" height="34"></td>
<td style="color:#90c8e8;"><img src="houtai/images/icon_card.gif" width="16" height="16" align="absmiddle">&nbsp;&nbsp;<strong>用户管理</strong></td>
<td align="right" class="white" style="padding-right:20px"><a href="<%=basePath %>UserServlet?method=addlookrole" class="barBtn"><img src="houtai/images/5.png" align="absmiddle"> 新增</a>
<a href="javascript:history.go(-1);" class="barBtn" onClick="javascript:history.go(-1);"><img src="houtai/images/btn_left.gif" align="absmiddle"> 后退</a>
<a href="javascript:history.go(+1);" class="barBtn" onClick="javascript:history.go(+1);"><img src="houtai/images/btn_right.gif" align="absmiddle"> 前进</a>
<a href="javascript:history.go(0);" class="barBtn" onClick="javascript:history.go(0);"><img src="houtai/images/btn_refresh.gif" align="absmiddle"> 刷新</a></td>
</tr>
</table></td>
</tr>
<%
UserCondition uc=(UserCondition)session.getAttribute("uc");
if(uc==null){
uc = new UserCondition("","",-1,-1);
}
%> <tr>
<td style="height:30px; background-color:#bddbff; border-bottom:1px solid #cfd8e0;"><table width="100%" border="0" cellpadding="3" cellspacing="1" class="table1">
<form action="<%=basePath %>UserServlet" method="post">
<TR>
<th width="10%" align="center">用户登录名</th>
<td width="20%" class="BGCgray">
<input type="text" name="conseluname" id="textfield" value="<%=uc.getUserLoginName()==null?"":uc.getUserLoginName()%>"></td>
<th width="10%" align="center">真实姓名</th>
<td width="20%" class="BGCgray">
<input type="text" name="conselruname" id="textfield2" value="<%=uc.getUserRealName()==null?"":uc.getUserRealName()%>"></td>
<th width="10%" align="center"><STRONG>状态</STRONG></th>
<td width="20%" class="BGCgray">
<select name="userState" id="select2">
<option <%=uc.getUserState()==-1?"selected='selected'":""%> value="-1">全部</option>
<option <%=uc.getUserState()==1?"selected='selected'":""%> value="1">启用</option>
<option <%=uc.getUserState()==0?"selected='selected'":""%> value="0">禁用</option>
</select></td>
<th width="10%" align="center">
<BUTTON style="HEIGHT:25px" onClick="javascript:if (confirm('查询数据?')) return true;else return false;">
<img src="houtai/images/btn_search.gif" width="16" height="16" align="absmiddle"> 查询
<input type="hidden" name="method" value="showuser">
<input type="hidden" name="currentPage" value="1">
</BUTTON></th>
</TR>
</form>
</table></td>
</tr>
<tr>
<td height="100%" valign="top">
<div style="overflow:auto;height:100%; width:100%">
<table width="100%" border="0" cellpadding="3" cellspacing="1" class="table1">
<TR>
<th align="center"><input type="checkbox" name="checkbox" id="userids"></th>
<th align="center">用户姓名</th>
<th align="center">登录名</th>
<th align="center">联系电话</th>
<th align="center">地址</th>
<th align="center">邮箱</th>
<th align="center">状态</th>
<th width="200" align="center">操作</th>
</TR>
<c:forEach items="${users }" var="user">
<TR>
<TD align="center"><input type="checkbox" name="items" id="checkbox2"></TD>
<TD align="center">${user.useName }</TD>
<TD align="center">${user.useLoginName }</TD>
<TD align="center">${user.usePhone }</TD>
<TD align="center">${user.useAddress }</TD>
<TD align="center">${user.useEmail }</TD>
<TD align="center" id="userStateId${user.useId }">${user.useState==1?"启用":"禁用" }</TD>
<TD width="200" align="center">
<BUTTON style="height:21px; font-size:12px" onClick="javascript:if (confirm('查看该信息?')){ location.href='<%=basePath %>UserServlet?method=lookinfo&useId=${user.useId }'; return true;}else return false;">查看
</BUTTON>
<BUTTON style="height:21px; font-size:12px" onClick="javascript:if (confirm('编辑该信息?')){ location.href='<%=basePath %>UserServlet?method=editinfo&useId=${user.useId }'; return true;}else return false;">编辑
</BUTTON>
<BUTTON style="height:21px; font-size:12px" id="fid" onclick="forbiduser(${user.useId})"><span id="btstate${user.useId }">${user.useState==1?"禁用":"启用" }</span>
</BUTTON>
<BUTTON style="height:21px; font-size:12px" onClick="javascript:if (confirm('删除该信息?')){ location.href='<%=basePath %>UserServlet?method=deleteinfo&useId=${user.useId }'; return true;}else return false;">删除
</BUTTON>
</TD>
</TR>
</c:forEach>
</table>
<page:page pi="${page }"/>
</body>
</html>

第六步:这里省略sevlet代码,分页标签实体类中的URL在servlet中设置。

jsp自定义标签分页的更多相关文章

  1. JSP 自定义标签

    0 标签技术的API继承体系 1 作用 jsp自定义标签用于移除页面中的java代码 2 实现 2.1 标签处理类ViewIPTag.java package com.zsm.util; import ...

  2. JSP自定义标签开发入门

    一般情况下开发jsp自定义标签需要引用以下两个包 import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; 首先我们需要大致了解开发 ...

  3. 一个简单的jsp自定义标签

    学到了一个简单的jsp自定义标签,后面有更多的例子,会更新出来: 例子1: 步骤: 1.编写标签实现类: 继承javax.servlet.jsp.tagext.SimpleTagSupport; 重写 ...

  4. jsp自定义标签分析

    jsp自定义标签的优势体现在于jsp页面上面减少了java代码. jsp自定义标签有三大部分组成,首先是类继承TagSupport,实现doStartTag方法. public int doStart ...

  5. JSP自定义标签库

    总所周知,JSP自定义标签库,主要是为了去掉JSP页面中的JAVA语句 此处以格式化输出时间戳为指定日期格式为例,简单介绍下JSP自定义标签的过程. 编写标签处理类(可继承自javax.servlet ...

  6. JSP自定义标签配置

    JSP自定义标签配置 JSP自定义标签 <taglib>         <taglib-uri>/WEB-INF/you.tld</taglib-uri>     ...

  7. jsp 自定义标签解决jsp页面中int时间戳的时间格式化问题

    jsp 自定义标签解决jsp页面中int时间戳的时间格式化问题 之前在项目中根据需求,需要自定义标签,经过查询w3c文档,自己也踩了一些坑,特此记录自定义标签的步骤,下面就以我之前的一个例子中的定义一 ...

  8. java JSP自定义标签

    来至: http://blog.csdn.net/jiangwei0910410003/article/details/23915373 http://blog.csdn.net/jiangwei09 ...

  9. JSP自定义标签开发入门《转》

    JSP自定义标签开发入门 一般情况下开发jsp自定义标签需要引用以下两个包 import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; ...

随机推荐

  1. (四面体)CCPC网络赛 HDU5839 Special Tetrahedron

    CCPC网络赛 HDU5839 Special Tetrahedron 题意:n个点,选四个出来组成四面体,要符合四面体至少四条边相等,若四条边相等则剩下两条边不相邻,求个数 思路:枚举四面体上一条线 ...

  2. 《Java数据结构与算法》笔记-CH4-2用栈实现字符串反转

    import java.io.BufferedReader; import java.io.InputStreamReader; //用栈来实现一个字符串逆序算法 /** * 数据结构:栈 */ cl ...

  3. 成都Uber优步司机奖励政策(3月5日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  4. 用C语言实现ipv4地址字符串是否合法

    用程序实现ipv4地址字符串是否合法,主要考察的是C字符串的操作. 搜索了下,网上没有特别好的实现,自己实现了下,见笑于大家,请指正. #include <stdio.h> #includ ...

  5. codeforces 630B Moore's Law

    B. Moore's Law time limit per test 0.5 seconds memory limit per test 64 megabytes input standard inp ...

  6. Unity3D行为树插件Behave学习笔记

    Behave1.4行为树插件 下载地址:http://pan.baidu.com/s/1i4uuX0L 安装插件和使用 我们先来看看插件的安装和基本使用方法,新建一个Unity3D项目,这里我使用的是 ...

  7. 《UNIX环境高级编程》笔记--更改用户ID和组ID

    在unix系统中,特权是基于用户和组ID的,当程序需要增加特权,或需要访问当前并不允许访问的资源时,我们需要更换自己 用户ID或组ID,使的新ID具有合适的特权或访问权限.与此类似,当程序需要降低其特 ...

  8. Android Touch事件传递机制通俗讲解

    在讲正题之前我们讲一段有关任务传递的小故事,抛砖迎玉下: 话说一家软件公司,来一个任务,分派给了开发经理去完成: 开发经理拿到,看了一下,感觉好简单,于是 开发经理:分派给了开发组长 开发组长:分派给 ...

  9. PPTP + FreeRADIUS + MySQL 安装与配置

    原文地址:http://www.zhukun.net/archives/5375 PPTP + FreeRADIUS + MySQL 安装与配置 2012/03/29Linux运维centos.Fre ...

  10. Ubuntu Kylin14.04终于可以使用root登陆了

    Ubuntu Kylin14.04怎样使用root登陆? 方法:找到/usr/share/lightm/ightm.conf.d         用gedit或者vi 打开50-unity-greet ...