學習Struts已經有
至於Struts的語法這裡就不多介紹了
一 開發環境
Elicpse+Struts Studio+SqlServer
二 開發思路
既然講的是Struts
三 實例代碼
package bean;
import java
import java
/**
* Struts分頁顯示數據Bean
*/
public class Book {
private String bookname; //書名
private String author; //作者
private String price; //價格
public Book(String name
this
this
this
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this
}
public String getPrice(){
return this
}
public void setPrice(String price){
this
}
public static ArrayList getAllBook(Connection connection){
String sql=
ArrayList arrayList = new ArrayList();
try{
Statement statement = connection
ResultSet resultSet = statement
System
while(resultSet
{
String name = resultSet
String author = resultSet
String price = resultSet
System
Book book = new Book(name
arrayList
}
connection
resultSet
}catch(SQLException e)
{
System
}
return arrayList;
}
}
package page;
import bean
import java
/**
* 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){
this
totalRows=arrayList
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)
{
this
this
}
else
{
this
this
}
}
/**
* @return Returns the currentPage
*/
public String getCurrentPage() {
return this
}
/**
* @param currentPage The currentPage to set
*/
public void setCurrentPage(int currentPage) {
this
}
/**
* @return Returns the pageRecorders
*/
public int getPageRecorders() {
return pageRecorders;
}
/**
* @param pageRecorders The pageRecorders to set
*/
public void setPageRecorders(int pageRecorders) {
this
}
/**
* @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
}
/**
* @return Returns the totalRows
*/
public String getTotalRows() {
return this
}
/**
* @return Returns the hasNextPage
*/
public boolean isHasNextPage() {
return hasNextPage;
}
/**
* @param hasNextPage The hasNextPage to set
*/
public void setHasNextPage(boolean hasNextPage) {
this
}
/**
* @return Returns the hasPreviousPage
*/
public boolean isHasPreviousPage() {
return hasPreviousPage;
}
/**
* @param hasPreviousPage The hasPreviousPage to set
*/
public void setHasPreviousPage(boolean hasPreviousPage) {
this
}
public Book[] getNextPage(){
currentPage=currentPage+
System
System
if((currentPage
{
hasPreviousPage=true;
}
else
{
hasPreviousPage=false;
}
if(currentPage>=totalPages)
{
hasNextPage=false;
}
else
{
hasNextPage=true;
}
System
System
Book[] books=getBooks();
this
return books;
}
public Book[] getPreviouspage(){
currentPage=currentPage
if(currentPage==
if(currentPage>=totalPages)
{
hasNextPage=false;
}
else
{
hasNextPage=true;
}
if((currentPage
{
hasPreviousPage=true;
}
else
{
hasPreviousPage=false;
}
Book[] books=getBooks();
this
return books;
}
public Book[] getBooks(){
System
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28690.html