jsp如何用c标签实现分页
2个回答
展开全部
jsp用c标签实现分页的方式如下:
<%@ tag language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c"%>
<%@ attribute name="curIndex" type="java.lang.Long" required="true"%>
<%@ attribute name="pageSize" type="java.lang.Long" required="true"%>
<%@ attribute name="pagerRange" type="java.lang.Long" required="true"%>
<%@ attribute name="totalPage" type="java.lang.Long" required="true"%>
<%@ attribute name="formId" type="java.lang.String" required="true"%>
<%
long begin = Math.max(1, curIndex - pagerRange/2);
long end = Math.min(begin + (pagerRange-1),totalPage);
request.setAttribute("p_begin", begin);
request.setAttribute("p_end", end);
%>
<table class="pager">
<tr>
<% if (curIndex!=1){%>
<td><a href="javascript:gotoPage(1)">首页</a></td>
<td><a href="javascript:gotoPage(<%=curIndex-1%>)">上一页</a></td>
<%}else{%>
<td class="disabled"><a href="#">首页</a></td>
<td class="disabled"><a href="#">上一页</a></td>
<%}%>
<c:forEach var="i" begin="${p_begin}" end="${p_end}">
<c:choose>
<c:when test="${i == curIndex}">
<td class="active"><a href="#">${i}</a></td>
</c:when>
<c:otherwise>
<td><a href="javascript:gotoPage(${i})">${i}</a></td>
</c:otherwise>
</c:choose>
</c:forEach>
<% if (curIndex!=totalPage){%>
<td><a href="#">下一页</a></td>
<td><a href="#">末页</a></td>
<%}else{%>
<td class="disabled"><a href="javascript:gotoPage(<%=curIndex+1%>)">下一页</a></td>
<td class="disabled"><a href="javascript:gotoPage(<%=totalPage%>)">末页</a></td>
<%}%>
<td><a>共${totalPage}页</a></td>
<td class="input_li">跳转到:<input type="text" id="p_pageIndex" size="2" value="<c:out value="${pageIndex}"/>"/>页 <input type="button" id="gotoBtn" onclick="gotoPageByBtn()" value="GO"/></td>
<td class="input_li"> 每页:
<select id="p_pageSizeSelect" onchange="gotoPage(<%=curIndex%>)">
<option value="10" <c:if test="${pageSize==10}">selected</c:if>>10条</option>
<option value="20" <c:if test="${pageSize==20}">selected</c:if>>20条</option>
<option value="50" <c:if test="${pageSize==50}">selected</c:if>>50条</option>
</select>
</td>
</tr>
</table>
控制分页的代码如下
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c"%>
<%@ taglib uri="/WEB-INF/tld/fmt.tld" prefix="fmt"%>
<%@ taglib tagdir="/WEB-INF/tags" prefix="tags"%>
<head>
<style><!--分页样式-->
.pager { font: 12px Arial, Helvetica, sans-serif;}
.pager a {padding: 1px 6px; border: solid 1px #ddd; background: #fff; text-decoration: none;margin-right:2px;line-height:30px;vertical-align:middle;}
.pager .active a{color:red;border:none;}
.pager a:visited {padding: 1px 6px; border: solid 1px #ddd; background: #fff; text-decoration: none;}
.pager a:hover {color: #fff; background: #ffa501;border-color:#ffa501;text-decoration: none;}
.pager .input_li{padding: 1px 6px;}
</style>
<script><!--分页跳转脚本-->
function gotoPage(pageIndex){
var queryForm = document.getElementById("queryForm");
var action = queryForm.action;
var pageSize = document.getElementById("p_pageSizeSelect").value;
action += "?pageIndex=" + pageIndex + "&pageSize=" + pageSize;
//alert(action);
queryForm.action = action;
queryForm.submit();
}
function gotoPageByBtn(){
var pageIndex = document.getElementById("p_pageIndex").value;
var pageIndexInt = parseInt(pageIndex);
var totalPage = ${totalPage};
if(pageIndexInt>0 && pageIndexInt<totalPage){
gotoPage(pageIndex);
}
else{
alert("输入页数超出范围!");
}
}
</script>
</head>
<body>
<form id="queryForm" action="${basePath}/log/list" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="userName" value="<c:out value="${userName}"/>"/> </td>
<td><input type="submit" text="查询"/></td>
</tr>
</table>
</form>
<tags:pager pagerRange="10" pageSize="${pageSize}" totalPage="${totalPage}" curIndex="${pageIndex}" formId="queryForm"></tags:pager>
<table class="border">
<thead>
<tr>
<th width="100">用户名称</th>
<th width="500">操作内容</th>
<th width="200">操作时间</th>
</tr>
</thead>
<tbody>
<c:forEach items="${logList}" var="log">
<tr>
<td>${log.userName}</td>
<td>${log.result}</td>
<td>
<fmt:formatDate value="${log.createTime}" pattern="yyyy-MM-dd HH:mm:ss"/>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<tags:pager pagerRange="10" pageSize="${pageSize}" totalPage="${totalPage}" curIndex="${pageIndex}" formId="queryForm"></tags:pager>
</body>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询