在开发web程序时,从数据库中提取的数据通常显示在页面上。有很多方法可以显示分页。有些人写自定义标签,用自己的标签显示分页。就我个人而言,我认为这是一个更好的方法。以下是我用Struts2做的一个分页显示实例。基本思路是将数据库表中的每一行数据包装成一个对象,然后通过返回类型为List返回这些对象 Struts2的action定义了一个List属性,用这个List接收从数据库中查询并包装的对象,然后通过Struts2的标签遍布List中的每个对象,并在页面上显示这些对象中包含的属性。具体步骤如下: Xml代码
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="https://www.tulingxueyuan.cn/d/file/p/20230508/v5xlvq5yc4f" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee https://www.tulingxueyuan.cn/d/file/p/20230508/gercllsezy3 <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="https://www.tulingxueyuan.cn/d/file/p/20230508/v5xlvq5yc4f" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee https://www.tulingxueyuan.cn/d/file/p/20230508/gercllsezy3 <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
3、用于保存学生信息的数据库表: Sql代码
drop database if exists page; create database page ; use page; create table student ( stu_id integer auto_increment, stuName varchar(255) not null, address varchar(255) not null, stuPhone varchar(255)not null, primary key(stu_id) ); insert into student(stuName,address,stuPhone) values(杨华林,长沙,13787825190; insert into student(stuName,address,stuPhone) values"李明清","天津","13787525190"; insert into student(stuName,address,stuPhone) values"李小华","大连","137845190"; insert into student(stuName,address,stuPhone) values苏州,郑小明,'13787052188'); insert into student(stuName,address,stuPhone) values"杨一新","西安","1378785190"; insert into student(stuName,address,stuPhone) values"王新任","广州","137870560"; insert into student(stuName,address,stuPhone) values"谢小华","深圳"'13787075550'); insert into student(stuName,address,stuPhone) values"王建明","厦门","13785390"; insert into student(stuName,address,stuPhone) values"秦一生","北京","1378889236"; insert into student(stuName,address,stuPhone) values"田翠林","杭州",'13787655150'); insert into student(stuName,address,stuPhone) values"陈小明","广州",'13787623668'); drop database if exists page; create database page ; use page; create table student ( stu_id integer auto_increment, stuName varchar(255) not null, address varchar(255) not null, stuPhone varchar(255)not null, primary key(stu_id) ); insert into student(stuName,address,stuPhone) values"杨华林","长沙","13787825190"; insert into student(stuName,address,stuPhone) values"李明清","天津",'13787525190'); insert into student(stuName,address,stuPhone) values"李小华","大连","137845190"; insert into student(stuName,address,stuPhone) values"郑小明","苏州","1378705218"; insert into student(stuName,address,stuPhone) values"杨一新","西安",'13787851190'); insert into student(stuName,address,stuPhone) values"王新任","广州","137870560"; insert into student(stuName,address,stuPhone) values"谢小华","深圳","13787550"; insert into student(stuName,address,stuPhone) values"王建明","厦门",'13788853690'); insert into student(stuName,address,stuPhone) values"秦一生","北京","1378889236"; insert into student(stuName,address,stuPhone) values"田翠林","杭州","137875150"; insert into student(stuName,address,stuPhone) values"陈小明","广州","137876268";4、编写Pagedao类,这类用于从数据库中取出所有学生信息,并将这些学生信息封装成Student 对象并将 放入List中。 Java代码 package org.hnylj.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; public class PageDAO { private Connection conn ; private PreparedStatement pstmt ; private ResultSet rs ; private static final String DRIVER = "com.mysql.jdbc.Driver" ; private static final String URL = "jdbc:mysql://localhost:3306/page" ; private static final String USERNAME = "root" ; private static final String PASSWORD = "123" ; private Student student ; ///数据库连接 public synchronized Connection getConnection () { try { Class.forName (DRIVER) ; conn = DriverManager.getConnection (URL,USERNAME,PASSWORD) ; } catch (ClassNotFoundException e) { e.printStackTrace () ; return null ; } catch (SQLException e) { e.printStackTrace () ; return null ; } return conn ; } ///分页查询 public List<Student> queryByPage (int pageSize, int pageNow) { List<Student> list = new ArrayList<Student> () ; try { if (this.getConnection()!=null && pageSize>0 && pageNow>0) { pstmt = this.getConnection().prepareStatement( "select * from student order by stu_id limit "+(pageNow*pageSize-pageSize)+","+pageSize ); rs = pstmt.executeQuery () ; while (rs.next()) { student = new Student () ; student.setStu_id (rs.getInt(1)) ; student.setStuName (rs.getString(2)) ; student.setAddress (rs.getString(3)) ; student.setStuPhone (rs.getString(4)) ; list.add (student) ; } } } catch(SQLException e) { e.printStackTrace() ; } return list ; } } package org.hnylj.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; public class PageDAO { private Connection conn ; private PreparedStatement pstmt ; private ResultSet rs ; private static final String DRIVER = "com.mysql.jdbc.Driver" ; private static final String URL = "jdbc:mysql://localhost:3306/page" ; private static final String USERNAME = "root" ; private static final String PASSWORD = "123" ; private Student student ; ///数据库连接 public synchronized Connection getConnection () { try { Class.forName (DRIVER) ; conn = DriverManager.getConnection (URL,USERNAME,PASSWORD) ; } catch (ClassNotFoundException e) { e.printStackTrace () ; return null ; } catch (SQLException e) { e.printStackTrace () ; return null ; } return conn ; } ///分页查询 public List<Student> queryByPage (int pageSize, int pageNow) { List<Student> list = new ArrayList<Student> () ; try { if (this.getConnection()!=null && pageSize>0 && pageNow>0) { pstmt = this.getConnection().prepareStatement( "select * from student order by stu_id limit "+(pageNow*pageSize-pageSize)+","+pageSize ); rs = pstmt.executeQuery () ; while (rs.next()) { student = new Student () ; student.setStu_id (rs.getInt(1)) ; student.setStuName (rs.getString(2)) ; student.setAddress (rs.getString(3)) ; student.setStuPhone (rs.getString(4)) ; list.add (student) ; } } } catch(SQLException e) { e.printStackTrace() ; } return list ; } } 5、因为要包装数据库中的学生信息,所以要写JavaBean,即Student类: Java代码 package org.hnylj.util; public class Student { private int stu_id ; private String stuName ; private String address ; private String stuPhone ; public Student () { } public int getStu_id () { return stu_id ; } public void setStu_id (int stu_id) { this.stu_id = stu_id ; } public String getStuName () { return stuName ; } public void setStuName (String stuName) { this.stuName = stuName ; } public String getAddress () { return address ; } public void setAddress (String address) { this.address = address ; } public String getStuPhone () { return stuPhone ; } public void setStuPhone (String stuPhone) { this.stuPhone = stuPhone ; } } package org.hnylj.util; public class Student { private int stu_id ; private String stuName ; private String address ; private String stuPhone ; public Student () { } public int getStu_id () { return stu_id ; } public void setStu_id (int stu_id) { this.stu_id = stu_id ; } public String getStuName () { return stuName ; } public void setStuName (String stuName) { this.stuName = stuName ; } public String getAddress () { return address ; } public void setAddress (String address) { this.address = address ; } public String getStuPhone () { return stuPhone ; } public void setStuPhone (String stuPhone) { this.stuPhone = stuPhone ; } }
到目前为止,底层的代码已经写好了。接下来,我们需要编写web层的代码。 6、用index编写web层的jsp文件.jsp,show.jsp和error.jsp,其代码分别如下: Java代码
<%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Struts2实现分页显示</title> </head> <body> <p align="center"> <s:a href="show.action">查看学生列表</s:a> </p> </body> </html> <%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Struts2实现分页显示</title> </head> <body> <p align="center"> <s:a href="show.action">查看学生列表</s:a> </p> </body> </html>Java代码 <%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Struts2实现分页显示</title> </head> <body> <p align="center"> <table border="1"> <tr> <th>学号</th> <th>姓名</th> <th>地址</th> <th>电话</th> </tr> <s:iterator value="students"> <tr> <td><s:property value="stu_id"/></td> <td><s:property value="stuName"/></td> <td><s:property value="address"/></td> <td><s:property value="stuPhone"/></td> </tr> </s:iterator> </table> <s:url id="url_pre" value="show.action"> <s:param name="pageNow" value="pageNow-1"></s:param> </s:url> <s:url id="url_next" value="show.action"> <s:param name="pageNow" value="pageNow+1"></s:param> </s:url> <s:a href="%{url_pre}">上一页</s:a> <s:iterator value="students" status="status"> <s:url id="url" value="show.action"> <s:param name="pageNow" value="pageNow"/> </s:url> </s:iterator> <s:a href="%{url_next}">下一页</s:a> </p> </body> </html> <%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Struts2实现分页显示</title> </head> <body> <p align="center"> <table border="1"> <tr> <th>学号</th> <th>姓名</th> <th>地址</th> <th>电话</th> </tr> <s:iterator value="students"> <tr> <td><s:property value="stu_id"/></td> <td><s:property value="stuName"/></td> <td><s:property value="address"/></td> <td><s:property value="stuPhone"/></td> </tr> </s:iterator> </table> <s:url id="url_pre" value="show.action"> <s:param name="pageNow" value="pageNow-1"></s:param> </s:url> <s:url id="url_next" value="show.action"> <s:param name="pageNow" value="pageNow+1"></s:param> </s:url> <s:a href="%{url_pre}">上一页</s:a> <s:iterator value="students" status="status"> <s:url id="url" value="show.action"> <s:param name="pageNow" value="pageNow"/> </s:url> </s:iterator> <s:a href="%{url_next}">下一页</s:a> </p> </body> </html>Java代码 <%@ page language="java" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>发生错误</title> </head> <body> 业务逻辑异常,请稍后再试试。。。。。。。 </body> </html> <%@ page language="java" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>发生错误</title> </head> <body> 业务逻辑异常,请稍后再试试。。。。。。。 </body> </html>
7、编写struts2的action拦截用户请求:Java代码
package org.hnylj.web; import java.util.List; import org.hnylj.util.PageDAO; import org.hnylj.util.Student; import com.opensymphony.xwork2.ActionSupport; public class ShowAction extends ActionSupport { private List<Student> students ; private int pageNow = 1 ; ///初始化为1,默认从第一页开始 private int pageSize = 5 ; ///每页显示5个记录 private PageDAO pageDAO = new PageDAO () ; public List<Student> getStudents() { return students; } public void setStudents(List<Student> students) { this.students = students; } public int getPageNow() { return pageNow; } public void setPageNow(int pageNow) { this.pageNow = pageNow; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public String execute() throws Exception { students = pageDAO.queryByPage(pageSize, pageNow) ; return SUCCESS ; } } package org.hnylj.web; import java.util.List; import org.hnylj.util.PageDAO; import org.hnylj.util.Student; import com.opensymphony.xwork2.ActionSupport; public class ShowAction extends ActionSupport { private List<Student> students ; private int pageNow = 1 ; ///初始化为1,默认从第一页开始 private int pageSize = 5 ; ///每页显示5个记录 private PageDAO pageDAO = new PageDAO () ; public List<Student> getStudents() { return students; } public void setStudents(List<Student> students) { this.students = students; } public int getPageNow() { return pageNow; } public void setPageNow(int pageNow) { this.pageNow = pageNow; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public String execute() throws Exception { students = pageDAO.queryByPage(pageSize, pageNow) ; return SUCCESS ; } }
8、配置struts.xml文件,即action与响应结果页面之间的映射: Java代码
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name=“struts2” namespace="/" extends="struts-default"> <action name="show" class="org.hnylj.web.ShowAction"> <result name="success">show.jsp</result> <result name="error">error.jsp</result> </action> </package> </struts> <?xml version="1.0" encoding="UTF-8" ?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name=“struts2” namespace="/" extends="struts-default"> <action name="show" class="org.hnylj.web.ShowAction"> <result name="success">show.jsp</result> <result name="error">error.jsp</result> </action> </package> </struts>
到目前为止,所有的工作都已经完成。接下来,将整个web项目部署到tomcat的webaps目录中,启动tomcat,并在浏览器地址栏中输入 http://localhost:8080/项目名称/index.jsp,回车后可以看到一个链接,点击这个链接就可以看到一个学生信息列表,可以进行上一页和下一页。