package lcz.test.util;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
//标签实现类
public class PageTag extends TagSupport {
private static final long serialVersionUID = 1L;
private int currentPage = 1;// 当前是第几页
private int totalRows; // 总共有几个记录
private int pageSize = 10; // 每页有几个记录
private String url; // 请求的url地址
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getTotalRows() {
return totalRows;
}
public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
@Override
public int doStartTag() throws JspException {
if (totalRows == 0) {
return super.doStartTag();
}
// 计算总共有几页
int totalPage = (totalRows + pageSize - 1) / pageSize;
if (currentPage > totalPage) {
currentPage = totalPage;
}
if (currentPage < 1) {
currentPage = 1;
}
// 转化成字符串形式
StringBuilder sb = new StringBuilder();
// 前台的分页显示,把分页的格式添加到显示的页面上去
// 把当前页隐藏,以便jsp提交的时候,其它地方(本页,他页,后台)可以取到该值
sb.append(
"<form name='pageController' id='pageController' action='' method='post'>\r\n")
.append("<input type='hidden' id='currentPage' name='currentPage' value='"
+ currentPage + "' />\r\n");
// 获取当前页
HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
Enumeration<String> enumeration = request.getParameterNames();
String name = null;
String value = null;
while (enumeration.hasMoreElements()) {
name = enumeration.nextElement();
value = request.getParameter(name);
if (name.equals("currentPage")) {
if (null != value && !"".equals(value)) {
currentPage = Integer.parseInt(value);
}
continue;
}
sb.append("<input type='hidden' name='").append(name)
.append("' value='").append(value).append("'/>\r\n");
}
sb.append("第" + currentPage + "页 \r\n").append(" 共" + totalPage + "页,");
// 逻辑判断显示分页格式
if (currentPage == 1) {
sb.append("首页");
sb.append(" ");
sb.append("上一页\r\n");
} else {
sb.append("<a href='#' onclick='turnOverPage(1)'>首页</a>\r\n");
sb.append(" ");
sb.append("<a href='#' onclick='turnOverPage(")
.append((currentPage - 1)).append(")'>上一页</a>\r\n");
}
sb.append(" ");
if (currentPage == totalPage) {
sb.append("下一页");
sb.append(" ");
sb.append("尾页\r\n");
} else {
sb.append("<a href='#' onclick='turnOverPage(")
.append((currentPage + 1)).append(")'>下一页</a>\r\n");
sb.append(" ");
sb.append("<a href='#' onclick='turnOverPage(").append(totalPage)
.append(")'>尾页</a>\r\n");
}
sb.append(" 转到<select onChange='turnOverPage(this.value)'>\r\n");
for (int i = 1; i <= totalPage; i++) {
if (i == currentPage) {
sb.append(" <option value='").append(i)
.append("' selected='selected'>第").append(i)
.append("页</option>\r\n");
} else {
sb.append(" <option value='").append(i).append("'>第")
.append(i).append("页</option>\r\n");
}
}
sb.append("</select>\r\n");
sb.append(" \r\n");
sb.append("</form>\r\n");
// 生成提交表单的JS
sb.append("<script language='javascript'>\r\n");
//翻页函数
sb.append(" \t\n");
sb.append(" function turnOverPage(no){\r\n");
sb.append(" var form = document.pageController;\r\n");
//页号越界处理
sb.append(" \r\n");
sb.append(" if(no").append(">").append(totalPage).append(") {\r\n");
sb.append(" no=").append(totalPage).append(";\r\n");
sb.append(" }\r\n");
sb.append(" if(no").append("< 1){\r\n");
sb.append(" no=1;\r\n");
sb.append(" }\r\n");
sb.append(" form.").append("currentPage").append(".value=no;\r\n");
sb.append(" form.action='").append(url).append("';\r\n");
sb.append(" form.submit();\r\n");
sb.append(" }\r\n");
sb.append("</script>\r\n");
try {
pageContext.getOut().println(sb.toString());
} catch (IOException e) {
e.printStackTrace();
}
return super.doStartTag();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
ssh框架搭建实例源码1
共59个文件
java:14个
class:14个
xml:10个
4星 · 超过85%的资源 需积分: 12 376 下载量 81 浏览量
2012-02-28
17:27:18
上传
评论 1
收藏 38KB RAR 举报
温馨提示
本资源是在“ssh框架搭建实例源码”基础上增加了分页技术,增加一种dao层读取数据库的方法,并对文件目录做了微小的调整。本资源所需的jar包请到“ssh框架搭建实例源码”中下载:http://download.csdn.net/download/linchengzhi/4076267
资源推荐
资源详情
资源评论
收起资源包目录
ssh.rar (59个子文件)
ssh
.project 1KB
WebContent
WEB-INF
lib
page
output.jsp 706B
taglib.jsp 754B
web.xml 1KB
index.jsp 991B
META-INF
MANIFEST.MF 39B
resource
lcz
test
java2excel
hbm
Student.hbm.xml 604B
database
ssh.sql 827B
struts.xml 857B
applicationContext.xml 4KB
hibernate.cfg.xml 547B
jdbc.properties 453B
log4j.properties 477B
pager.tld 1KB
src
lcz
test
util
PageTag.java 5KB
java2excel
service
ComputeStudentService.java 116B
impl
ComputeStudentServiceImpl.java 762B
action
PageAction.java 1KB
PageServlet.java 801B
OutputAction.java 1KB
dao
ClazzDao.java 151B
StudentDao.java 161B
impl
ClazzDaoImpl.java 707B
StudentDaoImpl.java 909B
bean
Clazz.java 719B
Student.java 638B
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.jdt.ui.prefs 5KB
org.eclipse.wst.common.project.facet.core.xml 305B
org.eclipse.wst.ws.service.policy.prefs 118B
org.eclipse.jdt.core.prefs 23KB
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.component 593B
.jsdtscope 503B
build
classes
lcz
test
util
PageTag.class 5KB
java2excel
service
impl
ComputeStudentServiceImpl.class 1KB
ComputeStudentService.class 180B
ComputeStudentServiceTest.class 2KB
action
OutputAction.class 2KB
PageServlet.class 1KB
PageAction.class 1KB
dao
ClazzDaoTest.class 1KB
ClazzDao.class 181B
impl
StudentDaoImpl.class 2KB
ClazzDaoImpl.class 855B
StudentDao.class 189B
bean
Student.class 1KB
Clazz.class 1KB
hbm
Student.hbm.xml 604B
database
ssh.sql 827B
struts.xml 857B
applicationContext.xml 4KB
hibernate.cfg.xml 547B
jdbc.properties 453B
log4j.properties 477B
pager.tld 1KB
test
lcz
test
java2excel
service
ComputeStudentServiceTest.java 961B
dao
ClazzDaoTest.java 1KB
.classpath 895B
共 59 条
- 1
linchengzhi
- 粉丝: 13
- 资源: 17
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页