扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
nowpage 默认是 request 作用范围的 在作为 servlet类里 应该 request.setRequest("nowpage",相应大变量);
朝阳县网站制作公司哪家好,找创新互联建站!从网页设计、网站建设、微信开发、APP开发、成都响应式网站建设公司等网站项目制作,到程序开发,运营维护。创新互联建站成立与2013年到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联建站。
jsp中分页最快捷的办法是用分页组件:
分页组件代码使用taglib实现的:
%@ 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){%
tda href="javascript:gotoPage(1)"首页/a/td
tda 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
tda href="javascript:gotoPage(${i})"${i}/a/td
/c:otherwise
/c:choose
/c:forEach
% if (curIndex!=totalPage){%
tda href="#"下一页/a/td
tda 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
%}%
tda共${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:if10条/option
option value="20" c:if test="${pageSize==20}"selected/c:if20条/option
option value="50" c:if test="${pageSize==50}"selected/c:if50条/option
/select
/td
/tr
/table
jsp中使用方法:
%@ 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(pageIndexInt0 pageIndexInttotalPage){
gotoPage(pageIndex);
}
else{
alert("输入页数超出范围!");
}
}
/script
/head
body
form id="queryForm" action="${basePath}/log/list" method="post"
table
tr
td用户名:/td
tdinput type="text" name="userName" value="c:out value="${userName}"/"/ /td
tdinput 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
page类
public class PageModel {
private int totalCount = 0;// 总记录数
private int pageCount;// 总页数
private int pageSize = 10;// 每页显示记录数
private int page = 1;// 当前页
private int num = 5;// 当前页之前和之后显示的页数个数 如:假设当前页是 6 共有11页 那么 显示分页条会显示 1 2 3 4
// 5 [6] 7 8 9 10 11
@SuppressWarnings("unchecked")
private List items = new ArrayList();// 当前页记录内容集合
private int prev;// 前一页
private int next;// 后一页
private int last;// 最后一页
private ListInteger prevPages;// 得到前num页的数据集合
private ListInteger nextPages;// 得到后num页的数据集合
/**
* 计算总页数
*
* @param totalCount
*/
public void setTotalCount(int totalCount) {
if (totalCount 0) {
this.totalCount = totalCount;
this.pageCount = (totalCount + pageSize - 1) / pageSize;
}
}
/**
* 判断是否有前一页
*
* @return boolean
*/
public boolean getIsPrev() {
if (page 1) {
return true;
}
return false;
}
/**
* 获取前一页
*
* @return int
*/
public int getPrev() {
if (getIsPrev()) {
return page - 1;
} else {
return page;
}
}
/**
* 判断是否有后一页
*
* @return boolean
*/
public boolean getIsNext() {
if (page pageCount) {
return true;
}
return false;
}
/**
* 获取后一页
*
* @return int
*/
public int getNext() {
if (getIsNext()) {
return page + 1;
}
return getPageCount();
}
/**
* 获取最后一页
*
* @return int
*/
public int getLast() {
return pageCount;
}
/**
* 当前页的前num条页 假设当前页是 6 共有11页 如:1 2 3 4 5
*
* @return ListInteger
*/
public ListInteger getPrevPages() {
ListInteger list = new ArrayListInteger();
int _frontStart = 1;
if (page num) {
_frontStart = page - num;
} else if (page = num) {
_frontStart = 1;
}
for (int i = _frontStart; i page; i++) {
list.add(i);
}
return list;
}
/**
* 当前页的后num条页 假设当前页是 6 共有11页 如:7 8 9 10 11
*
* @return ListInteger
*/
public ListInteger getNextPages() {
ListInteger list = new ArrayListInteger();
int _endCount = num;
if (num pageCount (page + num) pageCount) {
_endCount = page + _endCount;
} else if ((page + num) = pageCount) {
_endCount = pageCount;
}
for (int i = page + 1; i = _endCount; i++) {
list.add(i);
}
return list;
}
/**
* 获取每页显示记录数
*
* @return int
*/
public int getPageSize() {
return pageSize;
}
/**
* 设置每页显示记录数
*
* @param pageSize
*/
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
/**
* 得到当前页数
*
* @return int
*/
public int getPage() {
return page;
}
/**
* 设置当前页数
*
* @param page
*/
public void setPage(int page) {
this.page = page;
}
/**
* 获取当前页之前或之后显示的页数个数
*
* @return int
*/
public int getNum() {
return num;
}
/**
* 设置当前页之前或之后显示的页数个数
*
* @param num
*/
public void setNum(int num) {
this.num = num;
}
/**
* 获取当前页记录内容集合
*
* @return List
*/
@SuppressWarnings("unchecked")
public List getItems() {
return items;
}
/**
* 设置当前页记录内容集合
*
* @param items
*/
@SuppressWarnings("unchecked")
public void setItems(List items) {
this.items = items;
}
/**
* 获取总记录数
*
* @return int
*/
public int getTotalCount() {
return totalCount;
}
/**
* 得到总页数
*
* @return int
*/
public int getPageCount() {
return pageCount;
}
}
action代码:
PageModel pageModel = new PageModel();
// 获得当前页
if (page != 0) {
pageModel.setPage(page);
}
pageModel.setPageSize(10);// 设置页面显示最大 值
pageModel.setTotalCount(baseDAO.listAll("from Art where arttype.id=10 and mark=0 order by id desc")); // 数据总条数
pageModel.setNum(5); // 设置当前页的前后距离,/**前后各显示5页**/
// 通过当前页和
ListArt aboutList = baseDAO.listAll("from Art where arttype.id=10 and mark=0 order by id desc", pageModel.getPage(),pageModel.getPageSize());
pageModel.setItems(aboutList);
request.setAttribute("count", aboutList.size());// 放置在request中
request.setAttribute("pageModel", pageModel);
request.setAttribute("page", pageModel.getPage());
jsp代码
c:forEach var = "i" items="${requestScope.pageModel.items}" varStatus="items"
li/li
/c:forEach
div class="badoo"span class="disabled"第${pageModel.page}页/共${pageModel.pageCount}页/span a href="${pageuri}page=1"首页/a
c:if test="${pageModel.page1}"
a href="${pageuri}page=${pageModel.prev}"上一页/a
/c:if
c:forEach var="pre" items="${pageModel.prevPages }"
a href="${pageuri}page=${pre }"${pre}/a
/c:forEach
span class="current"${pageModel.page }/span
c:forEach var="next" items="${pageModel.nextPages }"
a href="${pageuri}page=${next }"${next}/a
/c:forEach
c:if test="${pageModel.pagepageModel.last}"
a href="${pageuri}page=${pageModel.next}"下一页/a
/c:if
a href="${pageuri}page=${pageModel.last}"尾页/a/div
如有不懂可以找我QQ聊
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){%
tda href="javascript:gotoPage(1)"首页/a/td
tda 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
tda href="javascript:gotoPage(${i})"${i}/a/td
/c:otherwise
/c:choose
/c:forEach
% if (curIndex!=totalPage){%
tda href="#"下一页/a/td
tda 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
%}%
tda共${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"nbsp;每页:
select id="p_pageSizeSelect" onchange="gotoPage(%=curIndex%)"
option value="10" c:if test="${pageSize==10}"selected/c:if10条/option
option value="20" c:if test="${pageSize==20}"selected/c:if20条/option
option value="50" c:if test="${pageSize==50}"selected/c:if50条/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(pageIndexInt0 pageIndexInttotalPage){
gotoPage(pageIndex);
}
else{
alert("输入页数超出范围!");
}
}
/script
/head
body
form id="queryForm" action="${basePath}/log/list" method="post"
table
tr
td用户名:/td
tdinput type="text" name="userName" value="c:out value="${userName}"/"/nbsp;/td
tdinput 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
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流