在JSP中实现MVC分页

上传人:ji****72 文档编号:37679873 上传时间:2018-04-20 格式:DOC 页数:6 大小:80.50KB
返回 下载 相关 举报
在JSP中实现MVC分页_第1页
第1页 / 共6页
在JSP中实现MVC分页_第2页
第2页 / 共6页
在JSP中实现MVC分页_第3页
第3页 / 共6页
在JSP中实现MVC分页_第4页
第4页 / 共6页
在JSP中实现MVC分页_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《在JSP中实现MVC分页》由会员分享,可在线阅读,更多相关《在JSP中实现MVC分页(6页珍藏版)》请在金锄头文库上搜索。

1、首先实现 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

2、;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.DriverM

3、anager; 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.ser

4、vlet.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 Ser

5、vletException, 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.setContentT

6、ype(“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

7、); 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.getIn

8、t(“num“); /获取总页数。也要注意这里的位置,不然可能会出错PageCount=IntCount%PAGE_SIZE=0?IntCount/PAGE_SIZE:IntCount/PAGE_SIZE+1; stmt=conn.createStatement(); /* 当前页小于 1 时,固定为 1;当前页大于总页数时,固定为最大页数* 千万要注意这个 if 语句的位置,只能写在这里,不然不会起作用,同时控 制台报错*/ if(currentPagePageCount) currentPage=PageCount; /查询要显示的记录数 rs=stmt.executeQuery(“sel

9、ect * 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.

10、setContent(content); coll.add(article); /以下这行代码仅用作测试,之所以没删,当作分享给大家的一个经验吧。因 为比起把结果给大家,还不如把过程给大家 System.out.print(“当前页码:“+currentPage+“,“+str); /下面依次是打包数据、页码和总页数 request.setAttribute(“colls“, coll); request.setAttribute(“currentPage“, currentPage); request.setAttribute(“PageCount“, PageCount); /跳转到 ar

11、ticle.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 Servl

12、etException, 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(); 最后实现“试图” ,代码如下:作品列表标题作者内容“上一页“下一页共有页  当前第页跳转至第页效果图:没有实现首页、尾页,很好做的。

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号