java动态新闻分页项目
页面格式布局:
示例代码:
package com.exam.model;
public class PageBean {
private int currentPage; //当前页
private int pageSize; //一页多少条
private int start; //起始页
public PageBean(int currentPage, int pageSize) {
super();
this.currentPage = currentPage;
this.pageSize = pageSize;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getStart() {
return (currentPage-1)*pageSize;
}
}
package com.exam.web; import java.io.IOException; import java.sql.Connection; import java.util.ArrayList; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.exam.dao.NewsDao; import com.exam.model.News; import com.exam.model.PageBean; import com.exam.util.DbUtil; @WebServlet(name="NewsServlet",urlPatterns="/newsList") public class NewsServlet extends HttpServlet{ /** * */ private static final long serialVersionUID = 1L; NewsDao newsDao = new NewsDao(); DbUtil dbUtil = new DbUtil(); @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //System.out.println("hello world"); String currentPage = req.getParameter("page"); if(currentPage==null || currentPage.equals("")) { currentPage = "1"; } PageBean pageBean = new PageBean(Integer.parseInt(currentPage),3); Connection conn = null; try { conn = dbUtil.getConn(); ArrayList<News> list = newsDao.newsList(conn,pageBean); req.setAttribute("list", list); int total = newsDao.newsCount(conn,pageBean); int totalPage = 0; if(total%pageBean.getPageSize()==0) { totalPage = total/pageBean.getPageSize(); }else { totalPage = total/pageBean.getPageSize() + 1; } req.setAttribute("currentPage",currentPage); req.setAttribute("total", totalPage); req.getRequestDispatcher("news_list.jsp").forward(req, resp); //resp.sendRedirect("news_list.jsp"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { dbUtil.closeConn(conn); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * sendRedirect()是请求重定向,和超连接是一个意思,比如你在A页面中写一个request.setAtribute,sendRedirect到B页面, * 就是说服务器从A页面中给你一个response,然后你的浏览器再去request到B页面,由于有两次request和response, * 是不能在B页面取到request.setAtribute里的值,能从地址栏看到url的改变。 * request.getRequestDispatcher().forward(request,response)是请求分发器, * 比如你在A页面中写一个request.setAtribute,request.getRequestDispatcher().forward(request,response)到B页面, * 那就是说服务器给你的response是B页面的,并且只有一次request和response,所以是能在B页面取到request.setAtribute里的值, * 地址栏的url仍然是A页面的。 */ }
package com.exam.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import com.exam.model.News;
import com.exam.model.PageBean;
import com.exam.util.DateUtil;
public class NewsDao {
public ArrayList<News> newsList(Connection conn,PageBean pageBean) throws Exception{
String sql = "select ids,title,memo,time,type from news limit ?,?";
PreparedStatement pst = conn.prepareStatement(sql); //准备执行sql语句
pst.setInt(1,pageBean.getStart());
pst.setInt(2,pageBean.getPageSize());
ResultSet rs = pst.executeQuery(); //执行sql语句
ArrayList<News> newsList = new ArrayList<>();
while(rs.next()) {
News news = new News();
news.setIds(rs.getInt("ids"));
news.setTitle(rs.getString("title"));
news.setMemo(rs.getString("memo"));
news.setTime(DateUtil.formatString(rs.getString("time"),"yyyy-MM-dd"));
news.setType(rs.getString("type"));
newsList.add(news);
}
return newsList;
}
public int newsCount(Connection conn,PageBean pageBean) throws SQLException {
String sql = "select count(*) as total from news";
PreparedStatement pst = conn.prepareStatement(sql);
ResultSet rs = pst.executeQuery();
int i = 0;
if(rs.next()) {
i = rs.getInt("total");
}
return i;
}
}
news_list.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
<style type="text/css">
*{font-size:12px;}
</style>
</head>
<body>
<table>
<c:forEach var="news" items="${list}">
<tr>
<td>
<p>
<span style="font-weight:bold">${news.title }</span>
- <fmt:formatDate value="${news.time }" pattern="yyyy-MM-dd"/>
</p>
<p>${fn:substring(news.memo,0,100) }</p>
<p><a href="">详情</a></p>
</td>
</tr>
</c:forEach>
</table>
<div>
<a href="newsList?page=1" >首页</a>
<a href="newsList?page=${currentPage-1<=0? 1:currentPage-1}">上一页</a>
<a href="newsList?page=${currentPage+1> total? total:currentPage+1}">下一页</a>
<a href="newsList?page=${total}">尾页</a>
<select id="choose" onchange="func()">
<c:forEach varStatus="x" begin="1" end="${total}">
<option value="${x.count }" ${currentPage==x.count? "selected":""}>第${x.count}页</option>
</c:forEach>
</select>
<span>当前是第${currentPage}页 —共${total }页</span>
</div>
</body>
<script type="text/javascript">
function func(){
var chs = document.getElementById("choose");
//alert(chs.value); //验证
window.location.href="newsList?page="+chs.value;
}
</script>
</html>