JSP MVC模式下 分页显示有问题
请教论坛各位大神~~~~~~如题所示,我JSP页面一开始能正常显示表头和表中数据,如图:
但是,一旦按了下一页或者上一页,就会出问题了,问题就是表头会不断增加、表中数据只会显示当前页的第一行数据。
如图所示:
具体代码如下
--------------------编程问答-------------------- 这个要晒晒你的前台代码 --------------------编程问答-------------------- 楼主真不容易啊,写了这么复杂的分页。换一种分页思路试试吧 --------------------编程问答-------------------- 你把展现层的代码都塞到业务逻辑层去了。不建议这么干。分页最好用分页模板类的方法,网上很多例子的 --------------------编程问答-------------------- 肯定是有模块的重复调用,嵌套了
package user.yourservlet;
import user.yourbean.Main;
import com.sun.rowset.*;
import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HandleMainPage extends HttpServlet
{
int fieldNum;
CachedRowSetImpl rowSet=null;
public void init(ServletConfig config)throws ServletException
{
super.init(config);
//加载MySQL的Java数据库驱动程序
try
{
Class.forName("com.mysql.jdbc.Drvier");
}
catch(Exception e){}
}
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
Connection con;
StringBuffer presentPageResult=new StringBuffer();
Main queryBean=null;
HttpSession session=request.getSession(true);
try
{
queryBean=(Main)session.getAttribute("query");
if(queryBean==null)
{
queryBean=new Main();
session.setAttribute("query",queryBean);
}
}
catch(Exception exp)
{
queryBean=new Main();
session.setAttribute("query",queryBean);
}
//String ps=request.getParameter("pageSize");
//if(ps!=null)
//{
// try{
// int mm=Integer.parseInt(request.getParameter("pageSize"));
// queryBean.setPageSize(mm);
// }
// catch(NumberFormatException exp){
queryBean.setPageSize(2);
// }
////}
int showPage=queryBean.getShowPage();
int pageSize=queryBean.getPageSize();
String uri="jdbc:mysql://localhost/jim";
try{
con=DriverManager.getConnection(uri,"root","asd1234");
DatabaseMetaData metadata=con.getMetaData();
ResultSet rs1=metadata.getColumns(null,null,"book",null);
int k=0;
String field[]=new String[100];
while(rs1.next()){
fieldNum++;
field[k]=rs1.getString(4);
k++;
}
StringBuffer str=new StringBuffer();
str.append("<tr>");
for(int i=0;i<fieldNum;i++)
str.append("<th>"+field[i]+"</th>");
str.append("</tr>");
queryBean.setFormTitle(str);
String condition="select * from book";
PreparedStatement pre=con.prepareStatement(condition);
ResultSet rs=pre.executeQuery();
rowSet=new CachedRowSetImpl();
rowSet.populate(rs);
con.close();
queryBean.setRowSet(rowSet);
rowSet.last();
int m=rowSet.getRow();
int n=pageSize;
int pageAllCount=((m%n)==0)?(m/n):(m/n+1);
queryBean.setPageAllCount(pageAllCount);
}
catch(SQLException exp){
}
String whichPage=request.getParameter("whichPage");
if(whichPage==null||whichPage.length()==0){
showPage=1;
queryBean.setShowPage(showPage);
CachedRowSetImpl rowSet=queryBean.getRowSet();
if(rowSet!=null){
presentPageResult=show(showPage,pageSize,rowSet);
queryBean.setPresentPageResult(presentPageResult);
}
}
else if(whichPage.equals("nextPage")){
showPage++;
if(showPage>queryBean.getPageAllCount())
showPage=1;
queryBean.setShowPage(showPage);
CachedRowSetImpl rowSet=queryBean.getRowSet();
if(rowSet!=null){
presentPageResult=show(showPage,pageSize,rowSet);
queryBean.setPresentPageResult(presentPageResult);
}
}
else if(whichPage.equals("previousPage")){
showPage--;
if(showPage<=0)
showPage=queryBean.getPageAllCount();
queryBean.setShowPage(showPage);
CachedRowSetImpl rowSet=queryBean.getRowSet();
if(rowSet!=null){
presentPageResult=show(showPage,pageSize,rowSet);
queryBean.setPresentPageResult(presentPageResult);
}
}
queryBean.setPresentPageResult(presentPageResult);
RequestDispatcher dispatcher=request.getRequestDispatcher("showMain.jsp");
dispatcher.forward(request,response);
}
public StringBuffer show(int page,int pageSize,CachedRowSetImpl rowSet){
StringBuffer str=new StringBuffer();
try{
rowSet.absolute((page-1)*pageSize+1);
//rowSet.relative(-1);
for(int i=1;i<=pageSize;i++){
str.append("<tr>");
for(int k=1;k<=fieldNum;k++)
str.append("<td>"+rowSet.getString(k)+"</td>");
str.append("</tr>");
rowSet.next();
}
}
catch(SQLException exp){}
return str;
}
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
doPost(request,response);
}
}
看看你前台的代码吧 --------------------编程问答-------------------- 你的页面肯定有问题。是用jsp去显示的吧。 --------------------编程问答-------------------- 因为我还只是刚初学简单的jsp+servlet+javabean的MVC,而且最近时间有点紧,暂时还看不懂网上用其他方法的例子T T --------------------编程问答--------------------
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="user.yourbean.Main" %>
<html>
<center>
<body>
<jsp:useBean id="query" class="user.yourbean.Main" scope="session"/>
<table border="2">
分页后总页数<jsp:getProperty name="query" property="pageAllCount"/>
当前页码<jsp:getProperty name="query" property="showPage"/>
每页显示记录数<jsp:getProperty name="query" property="pageSize"/>
<jsp:getProperty name="query" property="presentPageResult"/>
</table>
<table>
<tr>
<td>
<form action="helpQuery" method="post">
<input type="hidden" value="<%=query.getShowPage()-1%>" name="showPage">
<input type="submit" value="上一页" name="submit">
</form>
</td>
<td>
<form action="helpQuery" method="post">
<input type="hidden" value="<%=query.getShowPage()+1%>" name="showPage">
<input type="submit" value="下一页" name="whichPage">
</form>
</td>
</tr>
</table>
</body>
</center>
</html>
就是这个jsp
补充:Java , Web 开发