学习Struts已经有个多月了前几天群里的朋友问我Struts分页显示的问题觉得好像与在jsp中的差不多但还是遇到了这样那样的问题好不容易花了几天时间把问题都搞清楚觉得还是写点东西跟大家分享一下的好!
至于Struts的语法这里就不多介绍了不懂的朋友可以先看网上的其他文章
一 开发环境
Elicpse+Struts Studio+SqlServer+Tomcat
二 开发思路
既然讲的是Struts那自然离不了MVC分页显示也是如此
建立适当的模型组件对应你要查询数据库中的表这部分由我们熟悉的javaBean来充当并在其中建立数据库查询方法该方法需要一个javasqlConntection类型的参数并返回一个ArrayList在本例中为 Bookjava
建立分页所需要的模型组件也是由javaBean来充当通过由Book中提供的ArrayList来构造本例中为 PageBeanjava
建立控制器组件这部分由Struts 中的Action来实现主要负责将实例化Book并利用返回的ArrayList对象构造PageBean以及接收由视图传递而来的action参数从而在PageBean对象中调用不同的方法该方法返回Book[] 对象最后将 Book[]和PageBean放入request中本例中为PageListActionjava
建立视图组件这部分由jsp来充当为了不出现java 代码我们使用Struts提供的标签库主要负责从request中取出刚刚放入的对象通过反复调用PageListAction以及action参数而实现分页显示本例中为pagetestjsp
建立并配置strutsconfigxml
建立数据库
三 实例代码
Bookjava
package bean;
import javasql*;
import javautilArrayList;
/**
* Struts分页显示数据Bean对应数据库中Book表
*/
public class Book {
private String bookname; //书名
private String author; //作者
private String price;//价格
public Book(String nameString authorString price){
thisbookname=name;
thisauthor=author;
thisprice=price;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
thisauthor = author;
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
thisbookname = bookname;
}
public String getPrice(){
return thisprice;
}
public void setPrice(String price){
thisprice=price;
}
public static ArrayList getAllBook(Connection connection){
String sql=select * from book;
ArrayList arrayList = new ArrayList();
try{
Statement statement = connectioncreateStatement(ResultSetTYPE_SCROLL_INSENSITIVEResultSetCONCUR_READ_ONLY);
ResultSet resultSet = statementexecuteQuery(sql);
Systemoutprintln(BookBean 数据查询已完成!);
while(resultSetnext())
{
String name = resultSetgetString(name);
String author = resultSetgetString(author);
String price = resultSetgetString(price);
Systemoutprintln(开始数据封装name=+name+author=+author+price=+price);
Book book = new Book(nameauthorprice);
arrayListadd(book);
}
connectionclose();
resultSetclose();
}catch(SQLException e)
{
Systemoutprintln(数据库异常+etoString());
}
return arrayList;
}
}
PageBeanjava
package page;
import beanBook;
import javautil*;
/**
* Struts分页显示逻辑Bean
*/
public class PageBean {
int currentPage=;//当前页
public int totalPages=;//总页数
int pageRecorders=;//每页条数据
int totalRows=;//总数据数
int pageStartRow=;//每页的起始数
int pageEndRow=;//每页显示数据的终止数
boolean hasNextPage=false; //是否有下一页
boolean hasPreviousPage=false; //是否有前一页
ArrayList arrayList;
Iterator it;
public PageBean(){}
public PageBean(ArrayList arrayList){
thisarrayList=arrayList;
totalRows=arrayListsize();
it=erator();
hasPreviousPage=false;
currentPage=;
if((totalRows%pageRecorders)==)
{
totalPages=totalRows/pageRecorders;
}
else
{
totalPages=totalRows/pageRecorders+;
}
if(currentPage>=totalPages)
{
hasNextPage=false;
}
else
{
hasNextPage=true;
}
if(totalRows<pageRecorders)
{
thispageStartRow=;
thispageEndRow=totalRows;
}
else
{
thispageStartRow=;
thispageEndRow=pageRecorders;
}
}
/**
* @return Returns the currentPage
*/
public String getCurrentPage() {
return thistoString(currentPage);
}
/**
* @param currentPage The currentPage to set
*/
public void setCurrentPage(int currentPage) {
thiscurrentPage = currentPage;
}
/**
* @return Returns the pageRecorders
*/
public int getPageRecorders() {
return pageRecorders;
}
/**
* @param pageRecorders The pageRecorders to set
*/
public void setPageRecorders(int pageRecorders) {
thispageRecorders = pageRecorders;
}
/**
* @return Returns the pageEndRow
*/
public int getPageEndRow() {
return pageEndRow;
}
/**
* @return Returns the pageStartRow
*/
public int getPageStartRow() {
return pageStartRow;
}
/**
* @return Returns the totalPages
*/
public String getTotalPages() {
return thistoString(totalPages);
}
/**
* @return Returns the totalRows
*/
public String getTotalRows() {
return thistoString(totalRows);
}
/**
* @return Returns the hasNextPage
*/
public boolean isHasNextPage() {
return hasNextPage;
}
/**
* @param hasNextPage The hasNextPage to set
*/
public void setHasNextPage(boolean hasNextPage) {
thishasNextPage = hasNextPage;
}
/**
* @return Returns the hasPreviousPage
*/
public boolean isHasPreviousPage() {
return hasPreviousPage;
}
/**
* @param hasPreviousPage The hasPreviousPage to set
*/
public void setHasPreviousPage(boolean hasPreviousPage) {
thishasPreviousPage = hasPreviousPage;
}
public Book[] getNextPage(){
currentPage=currentPage+;
Systemoutprintln(PageBeangetNextPage()正在执行);
Systemoutprintln(参数currentPage=+currentPage);
if((currentPage)>)
{
hasPreviousPage=true;
}
else
{
hasPreviousPage=false;
}
if(currentPage>=totalPages)
{
hasNextPage=false;
}
else
{
hasNextPage=true;
}
Systemoutprintln(参数hasNextPage=+hasNextPage);
Systemoutprintln(准备执行PageBeangetBooks());
Book[] books=getBooks();
thisdescription();
return books;
}
public Book[] getPreviouspage(){
currentPage=currentPage;
if(currentPage==){currentPage=;}
if(currentPage>=totalPages)
{
hasNextPage=false;
}
else
{
hasNextPage=true;
}
if((currentPage)>)
{
hasPreviousPage=true;
}
else
{
hasPreviousPage=false;
}
Book[] books=getBooks();
thisdescription();
return books;
}
public Book[] getBooks(){
Systemoutprintln(pageBeangetBooks()开始执行);