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">&nbsp;</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">&nbsp;</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>

原创粉丝点击