在JSP中实现MVC分页
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
首先实现MVC模式中的“模型”,用于封装数据,代码如下:package program;
public class Article {
String title;
String author;
String content;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getContent() {
return content;
}
public void setContent(String content) { this.content = content;
}
public String toString(){
return title+""+author+""+content;
}
}
然后实现“控制”,代码如下:
package program;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DevidePage extends HttpServlet {
Statement stmt;
ResultSet rs;
Connection conn = null;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Collection coll=new ArrayList(); //初始化ArrayList集合
final int PAGE_SIZE=2; //每页显示2个记录
int PageCount=0; //总页数
int currentPage=1; //当前页
int IntCount=0; //总记录数
request.setCharacterEncoding("GBK"); //必需写上这几段语句,否则浏览器上会显示乱码
response.setCharacterEncoding("GBK");
response.setContentType("text/html;charset=GBK");
PrintWriter out=response.getWriter();
//获取article.jsp页面上的Page值,因为getParameter()接收的是字符串,因此要用到String类
String str=request.getParameter("Page");
//判断接收到的Page是否为null。
第一次运行的时候,它是一个空值
if(str!=null){
// 如果str不为null,则将String类型转换为int类型
currentPage=Integer.parseInt(str);
}
try {
/*
* 获取总记录数
*/
stmt=conn.createStatement();
rs=stmt.executeQuery("select num from article");
while(rs.next()){
/*
* 获取总记录数。
原来数据库中没有int类型的num,于是我使用select
count(*) from article
* 接收title的数量,在用Intege.parseInt将之转换成为int类型。
* 之前之转换成功的的,后来或许是因为搞忘记了某个步骤导致没能成功
* 于是只好专门设置了一个自增的num
*/
IntCount=rs.getInt("num");
//获取总页数。
也要注意这里的位置,不然可能会出错
PageCount=IntCount%PAGE_SIZE==0?IntCount/PAGE_SIZE:IntCount/PAGE_SIZE+1;
}
stmt=conn.createStatement();
/*
* 当前页小于1时,固定为1;当前页大于总页数时,固定为最大页数
* 千万要注意这个if语句的位置,只能写在这里,不然不会起作用,同时控制台报错
*/
if(currentPage<=0){
currentPage=1;
}else if(currentPage>PageCount){
currentPage=PageCount;
}
// 查询要显示的记录数
rs=stmt.executeQuery("select * from article limit "+(currentPage-1)*PAGE_SIZE+",2");
while(rs.next()){
String title=rs.getString("title");
String author=rs.getString("author");
String content=rs.getString("content");
// 将数据封装到Article类
Article article=new Article();
article.setTitle(title);
article.setAuthor(author);
article.setContent(content);
coll.add(article);
}
// 以下这行代码仅用作测试,之所以没删,当作分享给大家的一个经验吧。
因为比起把结果给大家,还不如把过程给大家
System.out.print("当前页码:"+currentPage+","+str);
//下面依次是打包数据、页码和总页数
request.setAttribute("colls", coll);
request.setAttribute("currentPage", currentPage);
request.setAttribute("PageCount", PageCount);
// 跳转到article.jsp页面
RequestDispatcher requestdispatcher=request.getRequestDispatcher("article.jsp");
requestdispatcher.forward(request, response);
// 最好写上它。
曾经因为没写,导致跳转失败
return;
} catch (SQLException e) {
e.printStackTrace();
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public DevidePage() throws SQLException{
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/yunmeng","root","root");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
最后实现“试图”,代码如下:
<%@ page language="java" import="java.util.*,program.*,java.sql.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServ erPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>作品列表</title>
</head>
<body>
<%
//获取从DevidePage接收到的页码,currentPage的初始值已在DevidePage类中设好,为1。
用于实时监听你所操作的页码
int Page=(Integer)request.getAttribute("currentPage");
//获取总页数
int pageCount=(Integer)request.getAttribute("PageCount");
//为了防止浏览器上出现乱码
request.setCharacterEncoding("GBK");
response.setContentType("text/html;charset=GBK");
//获取封装的数据
Collection colls=(Collection)request.getAttribute("colls");
//遍历数据
Iterator it=colls.iterator();
while(it.hasNext()){
Article article =(Article)it.next();
%>
<form action="DevidePage" method="post">
<table>
<tr>
<td>标题</td><td><%=article.getTitle() %></td>
</tr>
<tr>
<td>作者</td><td><%=article.getAuthor() %></td>
</tr>
<tr>
<td>内容</td><td><%=article.getContent() %></td>
</tr>
</table>
</form>
<%
}
%>
<!-- 操作Page,然后让currentPage接收 -->
<a href="DevidePage?Page=<%=Page-1 %>">上一页</a><a href="DevidePage?Page=<%=Page+1 %>">下一页</a>
<a>共有<%=pageCount%>页</a>  <a>当前第<%=Page%>页</a>
<form action="DevidePage" method="post">
跳转至第<input type="text" width=20px name="Page"/>页
<input type="submit" value="跳转至该页"/>
</form>
</body>
</html>
效果图:
没有实现首页、尾页,很好做的。