jsp页面:
在峰峰矿等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供做网站、成都网站建设 网站设计制作按需策划,公司网站建设,企业网站建设,品牌网站制作,全网营销推广,外贸营销网站建设,峰峰矿网站建设费用合理。
%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%
%@page import="java.util.*,system.VO.*,system.DAO.*,system.Util.*"%
%
Utils user=null;
if(request.getSession().getAttribute("user")!=null){
user=(Utils)request.getSession().getAttribute("user");
}
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
WishPagin wpagin = new WishPagin();
String sql = "select * from wishes order by wish_time desc;";
System.out.println(sql);
wpagin.setSql(sql);
ArrayListWish wishes = wpagin.getWishes(request
.getParameter("page"));
%
!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ""
html
head
meta http-equiv="Content-Type" content="text/html; charset=UTF-8"
title许愿墙/title
div id="Content"
center
div style="width: 900px; height: 15px; clear: left"/div
%
int intPage = wpagin.getIntPage();
int pageCount = wpagin.getPageCount();
int rowCount = wpagin.getRowCount();
%span本网站共有%=rowCount%条留言 共%=pageCount%页 第%=intPage%页 /span
%
if (intPage pageCount) {
%
form action="%=request.getContextPath()%/pages/wish.jsp"
method="post"a
href="%=request.getContextPath()%/pages/wish.jsp?page=%=intPage + 1%"下一页/a %
}
% %
if (intPage 1) {
%a
href="%=request.getContextPath()%/pages/wish.jsp?page=%=intPage - 1%"上一页/a %
}
%
/form
div style="width: 900px; height: 10px; clear: left"/div
div id="neirong"
table class="TABLEI" border="1" cellpadding="5" cellspacing="5"
tr
th祝愿人/th
th接受人/th
th祝愿内容/th
th祝愿时间/th
/tr
%
if (wishes.size() != 0) {
for (Wish wish : wishes) {
%
tr
td class="ListSender"a
href="%=request.getContextPath()%/Read_wishServlet?id=%=wish.getId()%"%=new UserDAO().findById(wish.getWisher_id())
.getUserName()%/a/td
td class="ListAccpeter"a
href="%=request.getContextPath()%/Read_wishServlet?id=%=wish.getId()%"%=wish.getReceiver_name()%/a/td
td class="ListInfo"a
href="%=request.getContextPath()%/Read_wishServlet?id=%=wish.getId()%"
%
if (wish.getWishContent().length() 20) {
wish.setWishContent(wish.getWishContent().substring(0, 20));
out.print(wish.getWishContent() + "...");
} else {
out.print(wish.getWishContent());
}
% /a/td
td class="ListDate"a
href="%=request.getContextPath()%/Read_wishServlet?id=%=wish.getId()%"
%
String time = wish.getWishTime();
out.print(time.substring(0, time.length() - 10));
% /a/td
/tr
%
}
}
%
/table
/div
/center
/div
/body
/html
java类:
package system.Util;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import system.VO.Wish;
/**
* 祝愿分页显示
*
* @author jyuanqi
*
*/
public class WishPagin {
private int pageSize = 10; // 一页显示的记录数
private int rowCount; // 记录总数
private int pageCount; // 总页数
private int intPage; // 待显示页码
private String sql = "";
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getRowCount() {
return rowCount;
}
public void setRowCount(int rowCount) {
this.rowCount = rowCount;
}
public int getPageCount() {
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
public int getIntPage() {
return intPage;
}
public void setIntPage(int intPage) {
this.intPage = intPage;
}
public String getSql() {
return sql;
}
public void setSql(String sql) {
this.sql = sql;
}
public ArrayListWish getWishes(String strPage) {
ArrayListWish wishes = new ArrayListWish();
Connection Conn = null;
Statement stmt = null;
ResultSet rs = null;
String url;
if (strPage == null) {// 表明在QueryString中没有page这一个参数,此时显示第一页数据
intPage = 1;
} else {// 将字符串转换成整型
intPage = java.lang.Integer.parseInt(strPage);
if (intPage 1)
intPage = 1;
}
// 装载JDBC驱动程序
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
// 设置数据库连接字符串
url = "jdbc:mysql://localhost:3306/piaobozz";
// 连接数据库
try {
Conn = java.sql.DriverManager.getConnection(url, "root", "123");
} catch (SQLException e) {
e.printStackTrace();
}
// 创建一个可以滚动的只读的SQL语句对象
try {
stmt = Conn.createStatement(
java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,
java.sql.ResultSet.CONCUR_READ_ONLY);
} catch (SQLException e) {
e.printStackTrace();
}// 准备SQL语句
// 执行SQL语句并获取结果集
try {
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
// 获取记录总数
try {
rs.last();
} catch (SQLException e) {
e.printStackTrace();
}// ??光标在最后一行
try {
rowCount = rs.getRow();
} catch (SQLException e) {
e.printStackTrace();
}// 获得当前行号
// 记算总页数
pageCount = (rowCount + pageSize - 1) / pageSize;
// 调整待显示的页码
if (intPage pageCount) {
intPage = pageCount;
}
if (pageCount 0) {
// 将记录指针定位到待显示页的第一条记录上
try {
rs.absolute((intPage - 1) * pageSize + 1);
} catch (SQLException e) {
e.printStackTrace();
}
// 显示数据
int i = 0;
try {
while (i pageSize !rs.isAfterLast()) {
Wish wish = new Wish(rs.getInt("id"), rs
.getInt("wisher_id"),
rs.getString("receiver_name"), rs
.getString("wish_content"), rs
.getString("wish_time"));
wishes.add(wish);
i++;
rs.next();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
// 关闭结果集
try {
rs.close();
// 关闭SQL语句对象
stmt.close();
// 关闭数据库
Conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return wishes;
}
}
可以把要从数据库里查询出的数据条数用sql语句写出来。
int rowBegin=0;
if(page1)
rowBegin=10*(page-1); 10就是你要显示的行数
String sql="select top 10 * from table where id not in(select top+"+rowBegin+" id from table )";
按下页时直接传参就可以了
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
CREATE PROCEDURE [up_ARCH_DCFJ_Paged]
@pagesize int, --每页显示的记录数
@pageindex int, --当前页索引,最小值为1
@RecordCount int output --总记录数,0时不统计结果记录
AS
SET NOCOUNT ON
DECLARE @indextable TABLE(rowid INT IDENTITY(1,1),nid INT)
INSERT INTO @indextable(nid) --将符合的记录插入到临时表中
SELECT [FileID]
FROM [ARCH_DCFJ]
ORDER BY [FileID] DESC
IF(@RecordCount0)
BEGIN
SELECT @RecordCount= Count(1) FROM @indextable
END
DECLARE @PageLowerBound INT
DECLARE @PageUpperBound INT
SET @PageLowerBound=(@pageindex-1)*@pagesize
SET @PageUpperBound=@PageLowerBound+@pagesize
SET ROWCOUNT @PageUpperBound--最多执行行数 若下面还有超过此值的行数,请注释此行
SELECT a.*
FROM [ARCH_DCFJ] a,@indextable t
WHERE a.[FileID]=t.nid and t.rowid@PageLowerBound and t.rowid=@PageUpperBound
ORDER BY t.rowid
SET NOCOUNT OFF
GO
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聊
以下是最简单的分页(一个方法搞定):
首先是方法:
//用于按照页数和每页显示的记录数进行图书查询
public List getBookList(int page,int pageSize)throws Exception{
List list=new ArrayList();
Connection conn=this.getConn();
String sql="select top "+page+" * from bookInfo " +
"where bookId not in" +
"(select top "+((pageSize-1)*page)+" bookId from bookInfo)";
PreparedStatement pstmt=conn.prepareStatement(sql);
ResultSet rs=pstmt.executeQuery();
while(rs.next()){
//每条数据对应一个实体类
BookInfo book=new BookInfo();
book.setBookId(rs.getInt("bookId"));
book.setBookName(rs.getString("bookName"));
book.setBookType(rs.getString("bookType"));
book.setBookStatus(rs.getString("bookStatus"));
list.add(book);
}
this.closeAll(conn, pstmt, rs);
return list;
}
2:JSP页面代码:
%@ page language="java" import="java.util.*,entity.*,operator.*" pageEncoding="gbk"%
html
head
/head
body
table border="1"
tr
td width="150"图书名称/td
td width="100"类别/td
td width="100"状态/td
/tr
%
request.setCharacterEncoding("gbk");
int pageNum=1;
String pageStr=request.getParameter("pageNum");
if(pageStr==null){
pageNum=1;
}else{
pageNum=Integer.parseInt(pageStr);
}
Operator o=new Operator();
List list=o.getBookList(6,pageNum);
for(int i=0;ilist.size();i++){
BookInfo book=(BookInfo)list.get(i);
%
tr
td%=book.getBookName() %/td
td%=book.getBookType() %/td
td%=book.getBookStatus() %/td
/tr
%
}
%
tr
td colspan="3"a href="infoList.jsp?pageNum=%=pageNum==1?pageNum:pageNum-1%"上一页/a ||
a href="infoList.jsp?pageNum=%=list.size()6?pageNum:pageNum+1 %"下一页/a/td//二元运算符,相当于if判断 上一页时如果当前页=1为true,就是当前页,否则还可以减1
//下一页如果当前页的size小于你的每一页设置的条数为true,那么就是是当前页,否则就+1;
/tr
/table
/body
/html
售后响应及时
7×24小时客服热线数据备份
更安全、更高效、更稳定价格公道精准
项目经理精准报价不弄虚作假合作无风险
重合同讲信誉,无效全额退款