(1)、实体类:
1package com.domain;
2
3import org.apache.struts.action.ActionForm;
4
5public class PeopleForm extends ActionForm {
6 private int id;
7 private String name;
8 private String sex ;
9 private int age;
10 private String job;
11 public int getId() {
12 return id;
13 }
14 public void setId(int id) {
15 this.id = id;
16 }
17 public String getName() {
18 return name;
19 }
20 public void setName(String name) {
21 this.name = name;
22 }
23 public String getSex() {
24 return sex;
25 }
26 public void setSex(String sex) {
27 this.sex = sex;
28 }
29 public int getAge() {
30 return age;
31 }
32 public void setAge(int age) {
33 this.age = age;
34 }
35 public String getJob() {
36 return job;
37 }
38 public void setJob(String job) {
39 this.job = job;
40 }
41
42 }
(2)、Dao:
数据库操作:
1package com.dao;
2import java.sql.*;
3public class JDBConnection {
4Connection connection = null;
5static {
6 try {
7 Class.forName(\"com.mysql.jdbc.Driver\"); // 静态块中实现加载数据库驱动
8 } catch (ClassNotFoundException e) {
9 e.printStackTrace();
10 }
11}
12public Connection creatConnection(){
13 //创建数据库连接对象
14 String url = \"jdbc:mysql://localhost:3306/db_database20\"; //指定数据库连接URL
15 String userName = \"root\"; //连接数据库用户名
16 String passWord = \"111\"; //连接数据库密码
17 try {
18 connection = DriverManager.getConnection(url,userName, passWord); //获取数据库连接
19 } catch (SQLException e) {
20 e.printStackTrace();
21 }
22 return connection;
23}
24 //对数据库的查询操作
25public ResultSet executeQuery(String sql) {
26 ResultSet rs; //定义查询结果集
27 try {
28 if (connection == null) {
29 creatConnection(); //创建数据库连接
30 }
31 Statement stmt = connection.createStatement(); //创建Statement对象
32 rs = stmt.executeQuery(sql); //执行查询SQL语句
33 } catch (SQLException e) {
34 System.out.println(e.getMessage());
35 return null; //有异常发生返回null
36 }
37 return rs; //返回查询结果集对象
38}
39//关闭数据库连接
40public void closeConnection() {
41 if (connection != null) { //如果Connection对象
42 try {
43 connection.close(); //关闭连接
44 } catch (SQLException e) {
45 e.printStackTrace();
46 } finally {
47 connection = null;
48 }
49 }
50}
51
52}
业务逻辑:
1package com.dao;
2import java.util.*;
3import com.domain.PeopleForm;
4import java.sql.ResultSet;
5import java.sql.*;
6public class PeopleDao {
7 private JDBConnection connection = null;
8 public PeopleDao() {
9 connection = new JDBConnection();
10 }
11 //查询所有员工信息方法
12public List selectPeople() {
13 List list = new ArrayList(); //创建保存查询结果集集合对象
14 PeopleForm form = null;
15 String sql = \"select * from tb_emp\"; //定义查询tb_emp表中全部数据SQL语句
16 ResultSet rs = connection.executeQuery(sql); //执行查询
17 try {
18 while (rs.next()) { //循环遍历查询结果集
19 form = new PeopleForm(); //创建ActionForm实例
20 form.setId(Integer.valueOf(rs.getString(1))); //获取查询结果
21 form.setName(rs.getString(2));
22 form.setSex(rs.getString(3));
23 form.setAge(rs.getInt(4));
24 form.setJob(rs.getString(5));
25 list.add(form); //向集合中添加对象
26 }
27 } catch (SQLException ex) {
28 }
29 connection.closeConnection(); //关闭数据连接
30 return list; //返回查询结果
31}
32}
(3)Action:
1package com.action;
2
3import org.apache.struts.action.*;
4import javax.servlet.http.*;
5import com.dao.PeopleDao;
6import java.util.List;
7public class PeopleAction extends Action {
8private PeopleDao dao = null;
9public ActionForward execute(ActionMapping mapping, ActionForm form,
10 HttpServletRequest request, HttpServletResponse response) {
11 dao = new PeopleDao(); // 创建保存有数据查询类对象
12 List list = dao.selectPeople(); // 调用数据查询方法
13 int pageNumber = list.size(); // 计算出有多少条记录
14 int maxPage = pageNumber; // 计算有多少页数
15 变量
16 17 18 19 20 21 22 23 24 String number = request.getParameter(\"i\"); // 获取保存在request对象中if (maxPage % 4 == 0) { // “4”代表每页显示有4条记录
maxPage = maxPage / 4; // 计算总页数
} else { // 如果总页数除以4不整除
maxPage = maxPage / 4 + 1; // 将总页数加1
}
if (number == null) { // 如果保存在request范围内的当前页数为null
number = \"0\"; // 将number为0
}
request.setAttribute(\"number\将number
保存在request范围内
25 request.setAttribute(\"maxPage\String.valueOf(maxPage)); // 将分的总页数保存在request范围内
26 int nonce = Integer.parseInt(number) + 1;
27 request.setAttribute(\"nonce\
28 request.setAttribute(\"pageNumber\
29 request.setAttribute(\"list\
30 return mapping.findForward(\"peopleAction\"); // 请求转发地址
31}
32}
33
(4)、页面代码:
index.jsp:
<%@ page language=\"java\" import=\"java.util.*\" pageEncoding=\"gbk\"%>
<%
String path = request.getContextPath();
String basePath =
request.getScheme()+\"://\"+request.getServerName()+\":\"+request.getServerPort()+path+\"/\";
%>
===============================================
pagenation.jsp
<%@ page contentType=\"text/html; charset=gbk\" %>
<%@page import=\"java.sql.*\"%>
<%@page import=\"java.util.*\"%>
<%@page import=\"com.domain.PeopleForm\"%>
<%@ taglib uri=\"/WEB-INF/struts-bean.tld\" prefix=\"bean\"%>
<%@ taglib uri=\"/WEB-INF/struts-html.tld\" prefix=\"html\"%>
<%@ taglib uri=\"/WEB-INF/struts-logic.tld\" prefix=\"logic\"%>
<%
List list = (List)request.getAttribute(\"list\"); //获取保存在request范围内的数据
int number = Integer.parseInt((String)request.getAttribute(\"number\"));
int maxPage = Integer.parseInt((String)request.getAttribute(\"maxPage\"));
int pageNumber =
Integer.parseInt((String)request.getAttribute(\"pageNumber\"));
int start = number*4;//开始条数
int over = (number+1)*4;//结束条数
int count=pageNumber-over;//还剩多少条记录
if(count<=0){
over=pageNumber;
}
%>
| ||||||||||
(5)、Struts-config.xml
1
2Struts Configuration 1.2//EN\"
3
4\"http://struts.apache.org/dtds/struts-config_1_2.dtd\">
5
6 7 8 9 10 11 12 13 14 16 17 18 19 20
因篇幅问题不能全部显示,请点此查看更多更全内容