您的位置:首页 > 编程语言 > Java开发

JSP+Servlet+JavaBean实现分页

2014-05-23 22:29 627 查看
自己从网上看了很多分页的例子,有些写的不是很明白,这两天做项目的时候用到了分页,特地把它写下来,供以后学习。。。

项目:个人简历

在简历列表(list.jsp)中用到了分页,因为经过增删改查之后都会跳到list.jsp页面,所以将分页写在了过滤器中。过滤list.jsp

web.xml中过滤器的配置

<filter>

<description>This is the description of my J2EE component</description>

<display-name>This is the display name of my J2EE component</display-name>

<filter-name>FenyeFilter</filter-name>

<filter-class>com.filter.FenyeFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>FenyeFilter</filter-name>

<url-pattern>/list.jsp</url-pattern>

</filter-mapping>

下面是JavaBean中的代码:

package com.entity;

import java.util.List;

public class PageBean {

private int page=1;//当前页

private int size=4;//每页显示的行数

private int total;//总页数

private List data;//每页的数据

public int getPage() {

return page;

}

public void setPage(int page) {

this.page = page;

}

public int getSize() {

return size;

}

public void setSize(int size) {

this.size = size;

}

public int getTotal() {

return total;

}

public void setTotal(int total) {

this.total = total;

}

public List getData() {

return data;

}

public void setData(List data) {

this.data = data;

}

}

dao中的代码:

DBHelper:

package com.dao;

import java.io.IOException;

import java.io.InputStream;

import java.lang.reflect.Field;

import java.lang.reflect.Method;

import java.sql.*;

import java.util.ArrayList;

import java.util.List;

import java.util.Properties;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.naming.NamingException;

import javax.sql.DataSource;

import com.sun.org.apache.commons.beanutils.ConvertUtils;

/**

* 此类为数据库操作的工具类

* @author Administrator

*

*/

public class DBHelper {

private String driver;

private String url;

private String user;

private String pwd;

public DBHelper(){

InputStream is=this.getClass().getResourceAsStream("/db.properties");

*******************************************************************

*//db.properties中存放的是

*driver=com.mysql.jdbc.Driver

*url=jdbc:mysql://localhost:3306/cfgjianli?useUnicode=true&characterEncoding=utf-8

*user=root

*pwd=root

*******************************************************************

Properties p=new Properties();

try {

p.load(is);

driver=p.getProperty("driver");

url=p.getProperty("url");

user=p.getProperty("user");

pwd=p.getProperty("pwd");

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

private Connection conn;

private Statement stmt;

private ResultSet rs;

// Java纯连接方式(注意:必须引入驱动Jar包)

public Connection getConnByJdbc(){

try {

Class.forName(driver);

return DriverManager.getConnection(url,user,pwd);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

System.out.println("数据库连接失败!");

}

return null;

}

//检查数据库是否连接成功

public static void main(String[] args) {

System.out.println(new DBHelper().getConnByJdbc());

}

//此方法用于数据库的增、删、改

public int execUpdate(String sql){

conn=this.getConnByJdbc();

try {

stmt=conn.createStatement();

return stmt.executeUpdate(sql);

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

return 0;

}finally{

this.close();

}

}

//通用的查询方法(1,实体类的属性==列名;2,如何为聚合查询,则cls传null)

public List query(String sql,Class cls){

List list=new ArrayList();

conn=this.getConnByJdbc();

try {

stmt=conn.createStatement();

ResultSet rs=stmt.executeQuery(sql);

if(cls!=null){

while(rs.next()){

Object obj=cls.newInstance();

Field[]fs=cls.getDeclaredFields();

for(Field f:fs){

String fname=f.getName();

Method method=cls.getMethod("set"+fname.substring(0,1).toUpperCase()+fname.substring(1),f.getType());

method.invoke(obj, ConvertUtils.convert(rs.getString(fname), f.getType()));

}

list.add(obj);

}

}else{

if(rs.next())

list.add(rs.getInt(1));

}

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

close();

}

return list;

}

//关闭资源

public void close(){

try {

if(rs!=null)

rs.close();

if(stmt!=null)

stmt.close();

if(conn!=null)

conn.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

JlDao继承DBHelper

//得到简历的总记录数

public int getCount(){

String sql="select count(*) from gerenjianli";

List list=super.query(sql, null);

return (Integer)list.get(0);

}

//得到每页的数据

public List getList(int page,int size){

String sql="select * from gerenjianli limit "+size*(page-1)+","+size;

List list=super.query(sql, JianLiInfo.class);

return list;

}

分页过滤器中的代码:

public void doFilter(ServletRequest arg0, ServletResponse arg1,

FilterChain arg2) throws IOException, ServletException {

JlDao jdao=new JlDao();

PageBean pb=new PageBean();

if(arg0.getParameter("page")!=null){

pb.setPage(Integer.parseInt(arg0.getParameter("page")));

}

int count = 0;

List list=null;

count=jdao.getCount();

list=jdao.getList(pb.getPage(), pb.getSize());

//核心代码

pb.setTotal(count%pb.getSize()==0?count/pb.getSize():count/pb.getSize()+1);

pb.setData(list);

arg0.setAttribute("pb", pb);//将每页的数据存在pb中

arg2.doFilter(arg0, arg1);

}

list.jsp页面分页代码:

共 <span>${pb.total}</span> 页 | 第 <span>${pb.page }</span> 页

[<a href="list.jsp?page=1">首页</a>

<c:if test="${pb.page>1}">

| <a href="list.jsp?page=${pb.page-1}">上一页</a>

</c:if>

<c:if test="${pb.page<pb.total}">

|<a href="list.jsp?page=${pb.page+1}">下一页</a>

</c:if>

| <a href="list.jsp?page=${pb.total}">末页</a>] 转至:

<input id="num" type="text" size="1" value="${pb.page}"/>

<input type="button" value="GO" onclick="fenye();"/>

javascript中fenye()的写法:

function fenye(){

var page=document.getElementById("num").value;

if(isNaN(page)){

alert("请输入正确的页数!");

return;

}

location.href="list.jsp?page="+page;

}

****************至此,分页完成******************************************
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: