jsp+jstl实现分页
来源:互联网 发布:韩寒代笔 知乎 编辑:程序博客网 时间:2024/06/05 07:26
demo1:body追求干净的jstl+el+mysql,实现了但有两点小问题:
1.文中红色标明的部分,总页面数处理不好,不是整数。
2.文中写法,mysql链接很容易丢失。
附上页面代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.util.*" isELIgnored="false" %>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@page import="tkl.download.dao.HibernateDAO"%>
<%@page import="tkl.download.dao.MessageBean"%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="css/cust.css" />
<title>留言列表</title>
</head>
<body>
<div class="lylb">
<div class="lylb1">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="20%" align="center">时间</td>
<td width="34%" align="center">留言主题</td>
<td width="32%" align="center"> </td>
<td width="14%" align="center">留言人</td>
</tr>
</table>
</div>
<!-- take database connection -->
<sql:setDataSource var="datasource" url="jdbc:mysql://...?default-character-set=UTF-8"
driver="org.gjt.mm.mysql.Driver" user="root" password="123456" />
<!-- fetch and set total row count -->
//此处要用下面的jstl+el显得很干净协调,但是pageCount变量不为整数,没想到很合适的转换方式。
//<sql:query var="countresults" dataSource="${datasource}">
//select count(*) as c from T_MESSAGE order by msgId DESC
//</sql:query>
//<c:set value="${countresults.rows[0].c}" var="totalSize" />
//<c:set var="maxRows" value="10" />
//<c:set var="pageCount" value="${totalSize % maxRows==0?(totalSize/maxRows+1):(totalSize/maxRows)}"/>
<%
int totalSize = HibernateDAO.getInstance().getReplayedMessgeCount();
int maxRows = 10;
int pageCount = 0;
if(totalSize % maxRows==0)
{
pageCount = totalSize/maxRows ;
}
else
{
pageCount = totalSize/maxRows + 1;
}
//define max rows per page
pageContext.setAttribute("totalSize",totalSize);
pageContext.setAttribute("maxRows",maxRows);
pageContext.setAttribute("pageCount",pageCount);
%>
<c:if test="${totalSize>0}">
<c:set var="pageIndex" value="${empty param.pageIndex? 0 : param.pageIndex}"/>
<sql:query var="queryresults" dataSource="${datasource}"
sql="select * from T_MESSAGE order by msgId DESC"
startRow="${maxRows*param.pageIndex}" maxRows="${maxRows}"></sql:query>
<div class="lylb2 a333"><c:forEach items="${queryresults.rows}" var="info">
<div class="li">
<div class="lbnum1">${fn:substring(info.leaveTime,0,10)}</div>
<div class="lbnum2 red"><a href="messagesee.jsp?msgId=${info.msgId}"/>${fn:substring(info.leaveTitle,0,29)}</a>
</div>
<div class="lbnum3">${info.leaveName}</div>
</div>
</c:forEach></div>
<div class="lylb3">
<table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr>
<td width="74%" align="middle">
<c:if test="${pageIndex>0}">
<a class = "red" href="?pageIndex=0">首页</a>
<a class = "red" href="?pageIndex=${param.pageIndex-1}">上一页</a>
</c:if>
<c:if test="${pageCount>1}">
<c:forEach begin="0" end="${pageCount-1}" step="1" var="pageNum">
<c:if test="${pageIndex!=pageNum}">
<a class = "red" href="?pageIndex=${pageNum}">[${pageNum+1}]</a>
</c:if>
<c:if test="${pageIndex==pageNum}">
<strong>${pageNum+1}</strong>
</c:if>
</c:forEach>
</c:if>
<c:if test="${pageIndex<pageCount-1}">
<a class = "red" href="?pageIndex=${param.pageIndex+1}">下一页</a>
<a class = "red" href="?pageIndex=${pageCount-1}">末页</a>
</c:if>
</tr>
</table>
</div>
</c:if></div>
</body>
</html>
demo2:java+el+hibernate+mysql
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.util.*" isELIgnored="false" %>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@page import="tkl.download.dao.HibernateDAO"%>
<%@page import="tkl.download.dao.MessageBean"%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="css/cust.css" />
<title>留言列表</title>
</head>
<body>
${pageIndex}
<div class="lylb">
<div class="lylb1">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="20%" align="center">时间</td>
<td width="34%" align="center">留言主题</td>
<td width="32%" align="center"> </td>
<td width="14%" align="center">留言人</td>
</tr>
</table>
</div>
<%
//<c:set var="maxRows" value="2" />
//<c:set var="pageCount" value="${totalSize % maxRows==0?(totalSize/maxRows+1):(totalSize/maxRows)}"/>
int totalSize = HibernateDAO.getInstance().getReplayedMessgeCount();
int maxRows = 10;
int pageCount = 0;
if(totalSize % maxRows==0)
{
pageCount =totalSize/maxRows ;
}
else
{
pageCount = totalSize/maxRows + 1;
}
//define max rows per page
pageContext.setAttribute("totalSize",totalSize);
pageContext.setAttribute("maxRows",maxRows);
pageContext.setAttribute("pageCount",pageCount);
%>
<c:if test="${totalSize>0}">
<c:set var="pageIndex" value="${empty param.pageIndex? 0 : param.pageIndex}"/>
<%
Object obj = pageContext.getAttribute("pageIndex");
List<MessageBean> queryresults = null;
try
{
int pageIndex = Integer.valueOf(obj.toString());
int startrow = pageIndex * maxRows;
queryresults = HibernateDAO.getInstance().retievePagedReplayedMessge(startrow,maxRows);
}catch(Exception ex)
{
queryresults = null;
}
pageContext.setAttribute("queryresults",queryresults);
%>
<c:choose>
<c:when test="${empty queryresults}">
<c:out value="选择的页面号码不对!"></c:out>
</c:when>
<c:otherwise>
<div class="lylb2 a333">
<c:set value="1" var="i"/>
<c:forEach items="${queryresults}" var="info">
<c:choose>
<c:when test="${i%2==0}" >
<div class="li" style="background: #FFFFFF">
<div class="lbnum1" style="background: #FFFFFF">${fn:substring(info.leaveTime,0,10)}</div>
<div class="lbnum2 red" style="background: #FFFFFF"><a href="messagesee.jsp?msgId=${info.msgId}"/>${fn:substring(info.leaveTitle,0,29)}</a>
</div>
<div class="lbnum3" style="background: #FFFFFF">${info.leaveName==null||info.leaveName==''?'游客':info.leaveName}</div>
</div>
</c:when>
<c:otherwise>
<div class="li" style="background: #EFEFF8">
<div class="lbnum1" style="background: #EFEFF8">${fn:substring(info.leaveTime,0,10)}</div>
<div class="lbnum2 red" style="background: #EFEFF8"><a href="messagesee.jsp?msgId=${info.msgId}"/>${fn:substring(info.leaveTitle,0,29)}</a>
</div>
<div class="lbnum3" style="background: #EFEFF8">${info.leaveName==null||info.leaveName==''?'游客':info.leaveName}</div>
</div>
</c:otherwise>
</c:choose>
<c:set value="${i + 1}" var="i"/>
</c:forEach>
</div>
<div class="lylb3">
<table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr>
<td width="74%" align="middle">
<c:if test="${pageIndex>0}">
<a class = "red" href="?pageIndex=0">首页</a>
<a class = "red" href="?pageIndex=${param.pageIndex-1}">上一页</a>
</c:if>
<c:if test="${pageCount>1}">
<c:forEach begin="0" end="${pageCount-1}" step="1" var="pageNum">
<c:if test="${pageIndex!=pageNum}">
<a class = "red" href="?pageIndex=${pageNum}">[${pageNum+1}]</a>
</c:if>
<c:if test="${pageIndex==pageNum}">
<strong>${pageNum+1}</strong>
</c:if>
</c:forEach>
</c:if>
<c:if test="${pageIndex<pageCount-1}">
<a class = "red" href="?pageIndex=${param.pageIndex+1}">下一页</a>
<a class = "red" href="?pageIndex=${pageCount-1}">末页</a>
</c:if>
</tr>
</table>
</div>
</c:otherwise>
</c:choose>
</c:if></div>
</body>
</html>
- jsp+jstl实现分页
- 一个完全用JSTL实现的JSP分页方案
- jsp EL表达式结合JSTL标准标签实现分页
- JSP+Javabean+JSTL实现了记录分页显示
- jstl+js+jsp 实现最简单的分页(附插件)
- jstl+EL+jsp 分页程序
- jstl分页标签实现
- JSTL实现分页
- JSTL 实现分页功能
- jstl 实现分页效果
- JSP中分页技术的实现(jstl,servlet,javabean三种不同实现方式)
- JSTL实现分页页码功能
- jsp实现翻页 -- 使用jstl
- jsp实现分页,jsp前端分页
- JSP分页技术实现
- JSP分页技术实现
- JSP分页技术实现
- JSP分页技术实现
- 在ARX中自动新建DWG文件
- SQLSERVER I/O 架构
- 关于java生成excel格式
- 网页设计注意事项
- CSS样式文件优化 更整洁而清晰
- jsp+jstl实现分页
- Prof-UIS程序做界面.
- 哥租的不是房子,而是街道
- Windows下配置 apache+php5及加载mysql模块
- 为VC MFC 窗口添加 Flash 控件
- 得到ARX程序当前的路径
- 程序员。。。是一种生活态度(start programming 。。。)
- Ext TreePnael选中节点及获取选中的节点
- FPGA设计之循环编解码(移动通信)