分享

jsp EL表达式结合JSTL标准标签实现分页

 Ethan的博客 2011-04-17
<%@ page contentType="text/html; charset=GBK" %>
<%@ taglib prefix="c" uri="http://java./jstl/core_rt" %>
<%@ taglib prefix="sql" uri="http://java./jstl/sql_rt" %>
<%@taglib uri="http://java./jstl/fmt_rt" prefix="fmt"%>
<html>
<head>
<title>使用SQL标签</title>
</head>
<body bgcolor="white">
<c:set var="noOfRows" value="10" />
<sql:setDataSource driver =
"sun.jdbc.odbc.JdbcOdbcDriver" url =
"jdbc:odbc:accp" user="sa" password="" var="conn"/>
<c:if test="${custList == null}">
<sql:query var="custList" scope="session"
sql="SELECT * FROM employee ORDER BY LName"
dataSource="${conn}"/>
</c:if>
<c:choose>
<c:when test="${custList.rowCount == 0}">
此处不再有其他客户...
</c:when>
<c:otherwise>
<b>以下是客户列表:</b>
<p>
<table border="1">
<th>姓氏</th>
<th>名字</th>
<th>年龄</th>
<c:forEach items="${custList.rows}" var="row"
begin="${param.start}" end="${param.start +
noOfRows - 1}">
<tr>
<td><c:out value="${row.LName}" /></td>
<td><c:out value="${row.FName}" /></td>
<td><c:out value="${row.job_id}" /></td>
</tr>
</c:forEach>
</table>
</c:otherwise>
</c:choose>
<p>
<c:choose>
<c:when test="${param.start > 0}">
<a href="foreachexample.jsp?start=<c:out
value="${param.start - noOfRows}" />">
上一页</a>
</c:when>
<c:otherwise>
上一页
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${param.start + noOfRows <
custList.rowCount}">
<a href="foreachexample.jsp?start=<c:out
value="${param.start + noOfRows}" />">
下一页</a>
</c:when>
<c:otherwise>
下一页
</c:otherwise>
</c:choose>
共${custList.rowCount}条记录 
共 <fmt:formatNumber type="number" value="${custList.rowCount%noOfRows==0?custList.rowCount/noOfRows:(custList.rowCount%noOfRows>4?(custList.rowCount/noOfRows):(custList.rowCount/noOfRows+1))}" maxFractionDigits="0"/> 页  
第 <fmt:formatNumber type="number" value="${(param.start%noOfRows==0?param.start/noOfRows:(param.start%noOfRows>4?(param.start/noOfRows):(param.start/noOfRows+1)))+1}" maxFractionDigits="0"/> 页
</body>
</html>
说明:这里使用到了标准标签库中SQL标签去查询数据记录。
使用条件标签判断分页中上下页的是否链接。使用EL表达式显示总记录数,<c:set var="noOfRows" value="10" />用来设置每页的显示记录数。通过总记录数和每页记录数来计算总页数。
因为EL表达式中‘/’结果是实际的浮点数,而不是整除,也没有函数来完成取整,这里就结合格式化标签<fmt:formatNumber type="number" value="${custList.rowCount%noOfRows==0?custList.rowCount/noOfRows:(custList.rowCount%noOfRows>4?(custList.rowCount/noOfRows):(custList.rowCount/noOfRows+1))}" maxFractionDigits="0"/>来完成格式化取整,maxFractionDigits="0"表示保留0位小数,即取整,但是这的取整也只是四舍五入,所以又给计算页数带来困难,
最后我通过${custList.rowCount%noOfRows==0?custList.rowCount/noOfRows:(custList.rowCount%noOfRows>4?(custList.rowCount/noOfRows):(custList.rowCount/noOfRows+1))}两个条件表达式嵌套完成页数计算
用同样的方法实现计算第几页的目的

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zp8126/archive/2008/02/29/2135447.aspx

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多