漫谈解决Struts分页显示

王朝other·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

学习Struts已经有2个多月了,前几天群里的朋友问我Struts分页显示的问题,觉得似乎与在jsp中的差不多,但还是碰到了这样那样的问题,好不轻易花了几天时间把问题都搞清楚,觉得还是写点东西跟大家分享一下的好!

至于Struts的语法这里就不多介绍了,不懂的朋友可以先看网上的其他文章。

一 开发环境

Elicpse+Struts Studio+SqlServer2000+Tomcat。

二 开发思路

既然讲的是Struts,那自然离不了MVC,分页显示也是如此。

1 建立适当的模型组件,对应你要查询数据库中的表。这部分由我们熟悉的JavaBean来充当。并在其中建立数据库查询方法,该方法需要一个java.sql.Conntection类型的参数,并返回一个ArrayList。在本例中为 Book.java

2 建立分页所需要的模型组件,也是由javaBean来充当,通过由Book中提供的ArrayList来构造。本例中为 PageBean.java.。

3建立控制器组件,这部分由Struts 中的Action来实现。主要负责将实例化Book,并利用返回的ArrayList对象,构造PageBean。以及接收由视图传递而来的action参数。从而在PageBean对象中调用不同的方法,该方法返回Book[] 对象。最后将 Book[]和PageBean放入request中。本例中为PageListAction.java。

4建立视图组件,这部分由jsp来充当,为了不出现java 代码,我们使用Struts提供的标签库,主要负责从request中取出刚刚放入的对象,通过反复调用PageListAction以及action参数,而实现分页显示。本例中为pagetest.jsp.

5 建立并配置struts-config.XML。

6 建立数据库。

三 实例代码

1 Book.java

package bean;

import java.sql.*;

import java.util.ArrayList;

/**

* Struts分页显示数据Bean,对应数据库中Book表

*/

public class Book {

private String bookname; //书名

private String author; //作者

private String price;//价格

public Book(String name,String author,String price){

this.bookname=name;

this.author=author;

this.price=price;

}

public String getAuthor() {

return author;

}

public void setAuthor(String author) {

this.author = author;

}

public String getBookname() {

return bookname;

}

public void setBookname(String bookname) {

this.bookname = bookname;

}

public String getPrice(){

return this.price;

}

public void setPrice(String price){

this.price=price;

}

public static ArrayList getAllBook(Connection connection){

String sql="select * from book";

ArrayList arrayList = new ArrayList();

try{

Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);

ResultSet resultSet = statement.executeQuery(sql);

System.out.println("BookBean 数据查询已完成!");

while(resultSet.next())

{

String name = resultSet.getString("name");

String author = resultSet.getString("author");

String price = resultSet.getString("price");

System.out.println("开始数据封装:name="+name+"author="+author+"price="+price);

Book book = new Book(name,author,price);

arrayList.add(book);

}

connection.close();

resultSet.close();

}catch(SQLException e)

{

System.out.println("数据库异常"+e.toString());

}

return arrayList;

}

}

2 PageBean.java

package page;

import bean.Book;

import java.util.*;

/**

* Struts分页显示逻辑Bean

*/

public class PageBean {

int currentPage=1;//当前页

public int totalPages=0;//总页数

int pageRecorders=5;//每页5条数据

int totalRows=0;//总数据数

int pageStartRow=0;//每页的起始数

int pageEndRow=0;//每页显示数据的终止数

boolean hasNextPage=false; //是否有下一页

boolean hASPreviousPage=false; //是否有前一页

ArrayList arrayList;

Iterator it;

public PageBean(){}

public PageBean(ArrayList arrayList){

this.arrayList=arrayList;

totalRows=arrayList.size();

it=arrayList.iterator();

hasPreviousPage=false;

currentPage=1;

if((totalRows%pageRecorders)==0)

{

totalPages=totalRows/pageRecorders;

}

else

{

totalPages=totalRows/pageRecorders+1;

}

if(currentPage=totalPages)

{

hasNextPage=false;

}

else

{

hasNextPage=true;

}

if(totalRows<pageRecorders)

{

this.pageStartRow=0;

this.pageEndRow=totalRows;

}

else

{

this.pageStartRow=0;

this.pageEndRow=pageRecorders;

}

}

/**

* @return Returns the currentPage.

*/

public String getCurrentPage() {

return this.toString(currentPage);

}

/**

* @param currentPage The currentPage to set.

*/

public void setCurrentPage(int currentPage) {

this.currentPage = currentPage;

}

/**

* @return Returns the pageRecorders.

*/

public int getPageRecorders() {

return pageRecorders;

}

/**

* @param pageRecorders The pageRecorders to set.

*/

public void setPageRecorders(int pageRecorders) {

this.pageRecorders = 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 this.toString(totalPages);

}

/**

* @return Returns the totalRows.

*/

public String getTotalRows() {

return this.toString(totalRows);

}

/**

* @return Returns the hasNextPage.

*/

public boolean isHasNextPage() {

return hasNextPage;

}

/**

* @param hasNextPage The hasNextPage to set.

*/

public void setHasNextPage(boolean hasNextPage) {

this.hasNextPage = hasNextPage;

}

/**

* @return Returns the hasPreviousPage.

*/

public boolean isHasPreviousPage() {

return hasPreviousPage;

}

/**

* @param hasPreviousPage The hasPreviousPage to set.

*/

public void setHasPreviousPage(boolean hasPreviousPage) {

this.hasPreviousPage = hasPreviousPage;

}

public Book[] getNextPage(){

currentPage=currentPage+1;

System.out.println("PageBean.getNextPage()正在执行;");

System.out.println("参数currentPage="+currentPage);

if((currentPage-1)0)

{

hasPreviousPage=true;

}

else

{

hasPreviousPage=false;

}

if(currentPage=totalPages)

{

hasNextPage=false;

}

else

{

hasNextPage=true;

}

System.out.println("参数hasNextPage="+hasNextPage);

System.out.println("预备执行PageBean.getBooks()");

Book[] books=getBooks();

this.description();

return books;

}

public Book[] getPreviouspage(){

currentPage=currentPage-1;

if(currentPage==0){currentPage=1;}

if(currentPage=totalPages)

{

hasNextPage=false;

}

else

{

hasNextPage=true;

}

if((currentPage-1)0)

{

hasPreviousPage=true;

}

else

{

hasPreviousPage=false;

}

Book[] books=getBooks();

this.description();

return books;

}

public Book[] getBooks(){

System.out.println("pageBean.getBooks()开始执行;");

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航