学习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()开始执行;");
闽公网安备 35060202000074号