1.jsp代碼
<script>
function select() {
var val = $("#inputcount").val();
window.location.href = "${pageContext.request.contextPath}/querypage?everyCount="+val;
}
</script>
<%--頁面實現代碼--%>
<body>
<div class="container">
<div class="row">
<span style="margin-left: 20px"> 每頁顯示</span>
<input type="text" style="height:30px;width:30px;border-radius: 8px" class="text-left text-info"
id="inputcount" value="10"> <span>條</span>
<button class="btn btn-success" οnclick="select()">查詢</button>
<h3 style="text-align: center">我的所有項目</h3>
</div>
<table border="1" class="table table-bordered table-hover table-responsive text-center">
<tr class="success text-center">
<th></th>
<th>編號</th>
<th>項目名稱</th>
<th>項目ID</th>
<th>項目類型</th>
<th>創建時間</th>
<th>項目路徑</th>
<th colspan="2">操作</th>
</tr>
<c:forEach var="h" items="${pageBean.list}" varStatus="vs">
<tr>
<td><input type="checkbox"></td>
<td>${vs.count}</td>
<td>${h.pName}</td>
<td>${h.id}</td>
<td>${h.pType}</td>
<td>${h.date}</td>
<td>${h.pPath}</td>
<td>
<a class="btn btn-default btn-sm" href="#">修改</a>
<a class="btn btn-default btn-sm">刪除</a>
</td>
</tr>
</c:forEach>
<tr>
<td colspan="8" align="center">
<ul class="pagination success">
<%--此處判斷當前頁是否是第一頁,如果是第一頁,上一頁按鈕不能用--%>
<c:if test="${pageBean.currentPage==1}">
<li class="disabled">
<a href="#" aria-label="Previous"><span aria-hidden="true">«</span>
</a>
</li>
</c:if>
<c:if test="${pageBean.currentPage!=1}">
<li class="disabled">
<a href="${pageContext.request.contextPath}/querypage?currentPage=${pageBean.currentPage-1}&everyCount=${pageBean.everypagecount}"
aria-label="Previous"><span aria-hidden="true">«</span>
</a>
</li>
</c:if>
<%--此處生成頁號--%>
<c:forEach var="i" begin="1" end="${pageBean.countPage}" step="1">
<%--當前活動頁面深色顯示不能點擊--%>
<c:if test="${pageBean.currentPage==i}">
<li class="active"><a
href="${pageContext.request.contextPath}/querypage?currentPage=${i}&everyCount=${pageBean.everypagecount}">${i}</a></li>
</c:if>
<c:if test="${pageBean.currentPage!=i}">
<li><a href="${pageContext.request.contextPath}/querypage?currentPage=${i}&everyCount=${pageBean.everypagecount}">${i}</a></li>
</c:if>
</c:forEach>
<%--此處判斷是否爲最後一頁,如果是最後一頁,下一頁按鈕不能用--%>
<c:if test="${pageBean.currentPage==pageBean.countPage}">
<li><a class="disabled" href="#" aria-label="Next"><span aria-hidden="true">»</span></a>
</li>
</c:if>
<c:if test="${pageBean.currentPage!=pageBean.countPage}">
<li><a class="disabled"
href="${pageContext.request.contextPath}/querypage?currentPage=${pageBean.currentPage+1}&everyCount=${pageBean.everypagecount}"
aria-label="Next"><span aria-hidden="true">»</span></a></li>
</c:if>
</ul>
</td>
</tr>
</table>
</div>
</body>
Dao層代碼
/**
* 查詢當前頁面的數據
* @param currentPage 當前頁
* @param a 每頁記錄數
* @return 當前頁數據
*/
public List<Project> queryCurrentPageData(int currentPage, int a) {
String sql = "select * from project limit ?,? ";
int begin = (currentPage - 1) * a;//當前頁開始數據位置
List<Project> currentProject = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Project.class),begin,a);
return currentProject;
}
service層實現代碼
/**
* 獲取當前頁的數據
*
* @param currentPage 當前頁
* @param currentCountPage 當前頁要顯示的記錄數
* @return
*/
public PageBean queryPageProject(String currentPage, String currentCountPage) {
int cp = Integer.parseInt(currentPage); //當前頁
int ccp = Integer.parseInt(currentCountPage); //每頁數據數
List<Project> currentPageList = projectOperationDao.queryCurrentPageData(cp, ccp); //當前頁面用戶數據
//將項目數據封裝到PageBean
PageBean pageBean = new PageBean();
pageBean.setList(currentPageList); //當前頁數據
pageBean.setCurrentPage(cp); //當前頁
//查詢數據庫記錄總條數
int pagecount = projectOperationDao.pagecount();
int ceil = (int) Math.ceil(pagecount/ ccp);
pageBean.setCountPage(ceil); //總頁數
pageBean.setEverypagecount(ccp);
return pageBean;
}
4.web層實現代碼(servlet的實現)
@WebServlet(name = "querypage", urlPatterns = "/querypage")
public class PageQueryServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//獲取每頁顯示的條數,如果未指定每頁顯示數目,則默認顯示十條
String everyCount = request.getParameter("everyCount");
if (everyCount==null||"".equals(everyCount)){
everyCount="10";
}
//獲取當前頁
String currentPage = request.getParameter("currentPage");
if (currentPage==null||"".equals(currentPage)){
currentPage="1";
}
//調用查詢方法查詢每頁數據
ProjectOperationService projectOperationService = new ProjectOperationService();
PageBean pageBean = projectOperationService.queryPageProject(currentPage, everyCount);
request.setAttribute("pageBean",pageBean);
request.getRequestDispatcher("/page/testpage/pagelist.jsp").forward(request,response);
}
}
5.JavaBean
package com.sps.entity;
import java.util.List;
public class PageBean {
private List<Project> list; //每頁的項目數據
private int currentPage; //當前頁
private int countPage; //總頁數
private int everypagecount; //每頁顯示的記錄數
public PageBean() {
}
public PageBean(List<Project> list, int currentPage, int countPage, int everypagecount) {
this.list = list;
this.currentPage = currentPage;
this.countPage = countPage;
this.everypagecount = everypagecount;
}
public int getEverypagecount() {
return everypagecount;
}
public void setEverypagecount(int everypagecount) {
this.everypagecount = everypagecount;
}
public List<Project> getList() {
return list;
}
public void setList(List<Project> list) {
this.list = list;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getCountPage() {
return countPage;
}
public void setCountPage(int countPage) {
this.countPage = countPage;
}
@Override
public String toString() {
return "PageBean{" +
"list=" + list +
", currentPage=" + currentPage +
", countPage=" + countPage +
", everypagecount=" + everypagecount +
'}';
}
}
6.util工具類和配置文件(此Demo用的是c3p0連接池和mysql數據庫,可根據個人情況修改配置文件,此處包括util工具類和配置文件)
/**
* 目的:
* 1. 保證DataSource只有一個
* 2. 提供連接(DataSource獲得)
* 3. 釋放資源
*/
public class C3P0Utils {
//創建C3P0數據源(連接池)
private static DataSource dataSource = new ComboPooledDataSource();
/**
* 提供數據源
* @return
*/
public static DataSource getDataSource(){
return dataSource;
}
/**
* 從dataSource(連接池)獲得連接對象
*
* @return
* @throws Exception
*/
public static Connection getConnection() throws Exception {
Connection connection = dataSource.getConnection();
return connection;
}
/**
* 釋放資源
*
* @param resultSet
* @param statement
* @param connection
*/
public static void release(ResultSet resultSet, Statement statement, Connection connection) {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();//看Connection來自哪裏, 如果Connection是從連接池裏面獲得的, close()方法其實是歸還; 如果Connection是創建的, 就是銷燬
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
---------------------------------以下是配置文件----------------------------------------
<c3p0-config>
<!--配置方式一:使用默認配置(default-config)讀取連接池對象-->
<default-config>
<!--連接參數,必須要寫的-->
<property name="driverClass"> com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/mysystem </property>
<property name="user">root</property>
<property name="password">339869</property>
<!-- 連接池參數 -->
<property name="initialPoolSize">7</property>
<!--初始連接數:剛創建好連接池的時候準備的連接數量-->
<property name="maxPoolSize">10</property>
<!--最大連接數:連接池中最多可以放多少個連接-->
<property name="cheakoutTimeout">1000</property>
<!--最大等待時間:連接池中沒有連接時最長等待時間-->
<property name="maxIdleTime">2000</property>
<!--最大空閒回收時間:連接池中的空閒連接多久沒有使用就會回收-->
</default-config>
</c3p0-config>
7.實現效果圖