Struts2+mysql实现简单分页

1.index.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href='myaction?page=<%=2%>'>分页显示</a>
</body>
</html>


2.struts.xml


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true" />
<constant name="struts.ui.theme" value="simple"/>
<package name="default" namespace="/" extends="struts-default">
<action name="myaction" class="com.wxh.action.FenyeAction">
<result>/show.jsp</result>
</action>
</package>
</struts>

3.web.xml


<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

4.FenyeAction.java


package com.wxh.action;

import java.util.*;
import com.wxh.bean.*;
import com.wxh.dao.*;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class FenyeAction extends ActionSupport{
private int page;

public int getPage() {
return page;
}

public void setPage(int page) {
this.page = page;
}
public String execute() throws Exception{
int current_page=getPage();
ActionContext context=ActionContext.getContext();
BooksDao booksDao=new BooksDao();
List<Books> list=booksDao.find(current_page);
int pages=booksDao.findCount();
StringBuffer s=new StringBuffer();
for(int i=1;i<pages;i++){
if(i==current_page){
s.append("["+i+"]");
}
else{
s.append("<a href='myaction?page="+i+"'>"+i+"</a>");
}
}
context.put("list", list);
context.put("s",s);
return "success";
}
}


5.Books.java


package com.wxh.bean;

public class Books {
public static final int PAGE_SIZE=2;
private int id;
private String name;
private double price;
private int bookCount;
private String author;
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 double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getBookCount() {
return bookCount;
}
public void setBookCount(int bookCount) {
this.bookCount = bookCount;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}

}

6.BooksDao.java


package com.wxh.dao;

import java.sql.*;
import java.util.*;
import com.wxh.bean.*;

public class BooksDao {
public Connection getConnection(){
Connection conn=null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://127.0.0.1:3306/db_books";
String username="root";
String password="111";
conn=DriverManager.getConnection(url,username,password);

} catch (Exception e) {
e.printStackTrace();
}
return conn;
}


//分页查询方法

public List<Books> find(int page){
List<Books> list=new ArrayList<Books>();
Connection conn=getConnection();
String sql="select * from tb_books order by id limit ?,?";
try {
PreparedStatement ps= conn.prepareStatement(sql);
ps.setInt(1, (page-1)*Books.PAGE_SIZE);
ps.setInt(2,Books.PAGE_SIZE );
ResultSet rs=ps.executeQuery();
while(rs.next()){
Books p=new Books();
p.setId(rs.getInt("id"));
p.setName(rs.getString("name"));
p.setPrice(rs.getDouble("price"));
p.setBookCount(rs.getInt("bookCount"));
p.setAuthor(rs.getString("author"));
list.add(p);
}
rs.close();
ps.close();
conn.close();

} catch (Exception e) {
e.printStackTrace();
}
return list;
}
//获取商品信息的总记录数,用于计算商品信息的总页数
public int findCount(){
int count=0;
Connection conn=getConnection();
String sql="select count(*) from tb_books";
try {
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(sql);
if(rs.next()){
count=rs.getInt(1);
}
rs.close();
conn.close();

} catch (Exception e) {
// TODO: handle exception
}
return count;
}
}

7.show.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.*,com.wxh.bean.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table width="500" border="1" align="center" cellpadding="5" cellspacing="0">
<tr>
<td colspan="5" align="center" valign="middle"><strong><font color="#FF00CC" size="5">所有商品信息</font></strong></td>
</tr>
<tr align="center" valign="middle">
<td><strong>ID</strong></td>
<td><strong>书名</strong></td>
<td><strong>价格</strong></td>
<td><strong>数量</strong></td>
<td><strong>出版社</strong></td>
</tr>
<%
List<Books> list=(List<Books>)request.getAttribute("list");
for(Books p:list){
%>
<tr align="center" valign="middle">
<td><%=p.getId() %></td>
<td><%=p.getName() %></td>
<td><%=p.getPrice() %></td>
<td><%=p.getBookCount()%></td>
<td><%=p.getAuthor() %></td>
</tr>
<%} %>

<tr align="center" valign="middle">
<td colspan="5">
<%=request.getAttribute("s") %>
</td>
</tr>
</table>
</body>
</html>


开发工具:eclipse+mysql+tomcat8