jsp+mysql的分页问题
三层架构,逻辑写在后台:分页显示的效果出不来,DAO能测试输出数据库里的数据; 但是界面上什么数据都没有 不知道哪个参数没传好,还是语法问题。。
javabean文件:
package com.po;
public class Stu {
private int id;
private String name;
private int age;
private String addresss;
public Stu() {
// TODO Auto-generated constructor stub
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddresss() {
return addresss;
}
public void setAddresss(String addresss) {
this.addresss = addresss;
}
}
DAO的实现类:
/**
*
*/
package com.dao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import util.DbUtil;
import com.po.Stu;
/**
* @author lenovo
*
*/
public class StuDAOImpl implements StuDAO {
private int size;
/**
*
*/
public StuDAOImpl() {
// TODO Auto-generated constructor stub
}
/*
* (non-Javadoc)
*
* @see com.dao.StuDAO#findAll()
*/
public List<Stu> findAll() {
DbUtil db = new DbUtil();
ResultSet rs;
String sql = "select * from stu";
List<Stu> list = new LinkedList<Stu>();
try {
PreparedStatement ps = db.getCon().prepareStatement(sql);
rs = ps.executeQuery(sql);
while (rs.next()) {
Stu s = new Stu();
s.setId(rs.getInt(1));
s.setName(rs.getString(2));
s.setAge(rs.getInt(3));
s.setAddresss(rs.getString(4));
list.add(s);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
size = list.size();
return list;
}
/*
* (non-Javadoc)
*
* @see com.dao.StuDAO#getIntSize()
*/
public int getIntSize() {
// TODO Auto-generated method stub
findAll();
return size;
}
/*
* (non-Javadoc)
*
* @see com.dao.StuDAO#listPage(int, int)
*/
public List<Stu> listPage(int currentPage, int pageSize) {
// TODO Auto-generated method stub
List<Stu> list = new LinkedList<Stu>();
int recordIndex = (currentPage - 1) * pageSize;
DbUtil db = new DbUtil();
ResultSet rs;
String sql= "select * from stu limit ?, ?";
try {
PreparedStatement ps = db.getCon().prepareStatement(sql);
ps.setInt(1, recordIndex);
ps.setInt(2, pageSize);
//从结果集中读
rs = ps.executeQuery();
while(rs.next()){
Stu s = new Stu();
s.setId(rs.getInt(1));
s.setName(rs.getString(2));
s.setAge(rs.getInt(3));
s.setAddresss(rs.getString(4));
list.add(s);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
servlet类:
package com.servlet;
import java.io.IOException;
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;
import com.dao.StuDAOImpl;
import com.po.Stu;
/**
* @author lenovo
*
*/
public class StuServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
*
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
StuDAOImpl stuIns = new StuDAOImpl();
int total = stuIns.getIntSize();
int pageSize = 3;
int totalPages = total / pageSize;
totalPages = (total % pageSize == 0) ? totalPages : totalPages + 1;
int currentPage = 1;
if (request.getParameter("pageNo") != null) {
currentPage = Integer.parseInt(request.getParameter("pageNo"));
}
if (currentPage < 1) {
currentPage = 1;
}
if (currentPage > totalPages) {
currentPage = totalPages;
}
List<Stu> list = stuIns.listPage(currentPage, pageSize);
request.setAttribute("list", list);
request.setAttribute("currentPage", currentPage);
request.setAttribute("totalPages", totalPages);
RequestDispatcher rd = request.getRequestDispatcher("list.jsp");
rd.forward(request, response);
}
数据库连接类:
package util;
import java.sql.*;
public class DbUtil {
private PreparedStatement pstmt = null;
private Connection con = null;
public DbUtil() {
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=gbk",
"root", "root");;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public Connection getCon() {
return con;
}
public void close() {
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
jsp界面:
<%@ page language="java" import="java.util.*,com.po.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<jsp:useBean id="Stu" scope="page" class="com.po.Stu"></jsp:useBean>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'list.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h3>
学生信息表
</h3>
<table>
<tr>
<td width="20%" align="center" height="20">登录学号id</td>
<td width="20%" align="center" height="20">姓名</td>
<td width="20%" align="center" height="20">年龄</td>
<td width="20%" align="center" height="20">地址</td>
</tr>
<c:forEach items="${list}" var="listone">
<tr>
<td>${listone.id}</td>
<td>${listone.name}</td>
<td>${listone.age}</td>
<td>${listone.addresss}</td>
</tr>
</c:forEach>
<a href="list.jsp?currentPage=1"> 首页</a>
<a href="list.jsp?currentPage=${currentPage-1}"> 上一页</a>
<a href="list.jsp?currentPage=${currentPage+1}"> 下一页</a>
<a href="list.jsp?currentPage=${totalPages}"> 尾页</a>
</table><br/>
</body>
</html>
[解决办法]
楼主问错地方了