java web--页面分页技术

  在设计信息浏览页面时,如果信息量很大,则经常需要分页显示信息。在这种情况下,分页技术就可以很好的帮助到我们。

目录

  1.分页技术的设计思想

  2.分页的具体实现

  3.分页实例  


  1.分页技术的设计思想

  按照“表示层--控制层--DAO层--数据层”的分层设计思想:首先DAO对象提供分页查询的方法;在控制层调用DAO中的方法完成指定页数据的查询;然后在表示层通过EL表达式和JSTL将该页数据显示出来。

  2.分页的具体实现

  ①在DAO对象中提供两个方法用来计算总页数和查询指定页数据。

  ②控制层提供一个servlet类,调用DAO对象查询数据并指派页面显示数据。

  ③输出页面使用EL和JSTL显示查询结果

  3.分页实例  

    User.java--暂时记录信息

package bean2;

public class User {
	private String id;
	private String name;
	private String sex;
	public User() {
		super();
		// TODO Auto-generated constructor stub
	}
	public User(String id, String name, String sex) {
		super();
		this.id = id;
		this.name = name;
		this.sex = sex;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	
}

      JdbcUtil.java--连接数据库

package bean2;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JdbcUtil {
	private static String driver;
	private static String url;
	private static String user;
	private static String password;
	private static Properties pr=new Properties();
	public JdbcUtil(){
		super();
	}
	static 
	{
		try {
			pr.load(JdbcUtil.class.getClassLoader().getResourceAsStream("db.properties"));
		    driver=pr.getProperty("driver");
		    url=pr.getProperty("url");
		    user=pr.getProperty("user");
		    password=pr.getProperty("password");
		    try {
				Class.forName(driver);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static Connection getConnection() throws SQLException
	{
		return DriverManager.getConnection(url,user,password);
	}
	public static void free(Connection conn,Statement pstmt,ResultSet rs) throws Exception
	{
		if(conn!=null){conn.close();}
		if(pstmt!=null){pstmt.close();}
		if(rs!=null){rs.close();}
	}
}

    db.properties--记录数据库信息

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ccc?useUnicode=true&characterEncoding=UTF-8
user=root
password=2411030483

    UserDao.java--定义功能

package bean2;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class UserDao {
public int getPageCount() throws Exception{
	int recordCount=0,t1=0,t2=0;
	Connection conn=JdbcUtil.getConnection();
	String sql="select count(*) from student";
	PreparedStatement pstmt=conn.prepareStatement(sql);
	ResultSet rs=pstmt.executeQuery();
	rs.next();
	recordCount=rs.getInt(1);
	t1=recordCount%5;
	t2=recordCount/5;
	JdbcUtil.free(conn, pstmt, rs);
	return t1==0?t2:t2+1;
}
public List listUser(int pageNo) throws Exception{
	int pageSize=5;
	int startRecno=(pageNo-1)*pageSize;
	ArrayList userList=new ArrayList();
	Connection conn=JdbcUtil.getConnection();
	String sql="select * from student order by id limit ?,?";
	PreparedStatement pstmt=conn.prepareStatement(sql);
	pstmt.setInt(1,startRecno);
	pstmt.setInt(2, pageSize);
	ResultSet rs=pstmt.executeQuery();
	while(rs.next()){
		User user=new User();
		user.setId(rs.getString(1));
		user.setName(rs.getString(2));
		user.setSex(rs.getString(3));
		userList.add(user);
	}
	JdbcUtil.free(conn, pstmt, rs);
	return userList;
}
}

      page.java--servlet类,用以完成最终的功能调用

package bean2;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class page
 */
@WebServlet("/page")
public class page extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public page() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int pageNo=1;
		int pagesize=1;
		String strPageNo=request.getParameter("pageNo");
		if(strPageNo!=null){
			pageNo=Integer.parseInt(strPageNo);
		}
		UserDao userDao=new UserDao();
		try {
			List userlist=userDao.listUser(pageNo);
			request.setAttribute("userlist", userlist);
			Integer pageCount=new Integer(userDao.getPageCount());
			request.setAttribute("pageCount", pageCount);
			request.setAttribute("pageNo", pageNo);
			request.getRequestDispatcher("userlist.jsp").forward(request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

    tiaozhuan.jsp--跳转页面,可以不使用,如果不使用的话,需要直接运行servlet类,来获取页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




跳转页面


跳转页面


    userlist.jsp--显示页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
        <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>




用户列表




所有用户信息

共有${pageCount}页,这是第${pageNo}页 第一页 上一页 下一页 最后一页
用户编号用户名性别
${user.id }${user.name }${user.sex }

目前没有记录

java web--页面分页技术_第1张图片

你可能感兴趣的:(java)