1、建立yqdb数据库,以及表book(name,author,price)
2、 Struts分页显示数据Bean,对应数据库中Book表
public class Book { /**
*
*/
private String bookname;
private String author;
private String price;
public Book(String bookname,String author,String price) {
super();
this.bookname = bookname;
this.author = author;
this.price = price;
// TODO Auto-generated constructor stub
} /**
* @return Returns the author.
*/
public String getAuthor() {
return author;
}
/**
* @param author The author to set.
*/
public void setAuthor(String author) {
this.author = author;
}
/**
* @return Returns the bookname.
*/
public String getBookname() {
return bookname;
}
/**
* @param bookname The bookname to set.
*/
public void setBookname(String bookname) {
this.bookname = bookname;
}
/**
* @return Returns the price.
*/
public String getPrice() {
return price;
}
/**
* @param price The price to set.
*/
public void setPrice(String price) {
this.price = price;
}
/*
* 把查询的结果放入数组
*/
public static ArrayList getAllBook(Connection connection) throws SQLException{
String sql="select * from book";
ArrayList arrayList = new ArrayList();
Statement statement = null;
ResultSet resultSet = null;
try{
statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
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);
}
}catch(SQLException e)
{
System.out.println("数据库异常"+e.toString());
}finally{
resultSet.close();
statement.close();
connection.close();
} return arrayList;
}
}
2、 Struts分页显示数据Bean,对应数据库中Book表
public class Book { /**
*
*/
private String bookname;
private String author;
private String price;
public Book(String bookname,String author,String price) {
super();
this.bookname = bookname;
this.author = author;
this.price = price;
// TODO Auto-generated constructor stub
} /**
* @return Returns the author.
*/
public String getAuthor() {
return author;
}
/**
* @param author The author to set.
*/
public void setAuthor(String author) {
this.author = author;
}
/**
* @return Returns the bookname.
*/
public String getBookname() {
return bookname;
}
/**
* @param bookname The bookname to set.
*/
public void setBookname(String bookname) {
this.bookname = bookname;
}
/**
* @return Returns the price.
*/
public String getPrice() {
return price;
}
/**
* @param price The price to set.
*/
public void setPrice(String price) {
this.price = price;
}
/*
* 把查询的结果放入数组
*/
public static ArrayList getAllBook(Connection connection) throws SQLException{
String sql="select * from book";
ArrayList arrayList = new ArrayList();
Statement statement = null;
ResultSet resultSet = null;
try{
statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
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);
}
}catch(SQLException e)
{
System.out.println("数据库异常"+e.toString());
}finally{
resultSet.close();
statement.close();
connection.close();
} return arrayList;
}
}
*Struts分页显示逻辑Bean
*/
public class PageBean {
public static final String PERSON_KEY = "abc"; int currentPage=1; //当前页
public int totalPages=0; //总页数
int pageRecorders=2;//每页2条数据
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 String.valueOf(currentPage);
}
/**
* @param currentPage The currentPage to set.
*/
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
/**
* @return Returns the pageRecorders.
*/
public int getPageRecorders() {
return pageRecorders;
}
/**
* @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;
}
/**
* @return Returns the pageEndRow.
*/
public int getPageEndRow() {
return pageEndRow;
}
/**
* @param pageEndRow The pageEndRow to set.
*/
public void setPageEndRow(int pageEndRow) {
this.pageEndRow = pageEndRow;
}
/**
* @return Returns the pageStartRow.
*/
public int getPageStartRow() {
return pageStartRow;
}
/**
* @param pageStartRow The pageStartRow to set.
*/
public void setPageStartRow(int pageStartRow) {
this.pageStartRow = pageStartRow;
}
/**
* @return Returns the totalPages.
*/
public int getTotalPages() {
return totalPages;
}
/**
* @param totalPages The totalPages to set.
*/
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
/**
* @return Returns the totalRows.
*/
public int getTotalRows() {
return totalRows;
}
/**
* @param totalRows The totalRows to set.
*/
public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
}
/**
* @param pageRecorders The pageRecorders to set.
*/
public void setPageRecorders(int pageRecorders) {
this.pageRecorders = pageRecorders;
}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()开始执行;");
if(currentPage*pageRecorders<totalRows){//判断是否为最后一页
pageEndRow=currentPage*pageRecorders;
pageStartRow=pageEndRow-pageRecorders;
}
else{
pageEndRow=totalRows;
pageStartRow=pageRecorders*(totalPages-1);
}
Book[] books=new Book[pageEndRow-pageStartRow+1];
System.out.println("pageStartRow="+pageStartRow);
System.out.println("pageEndRow="+pageEndRow);
int j=0;
for(int i=pageStartRow;i<pageEndRow;i++)
{
Book book=(Book)arrayList.get(i);
books[j++]=book;
}
System.out.println("要显示的页面数据已经封装,具体信息如下:");
this.description();
return books;
} public String toString(int temp)
{
String str=Integer.toString(temp);
return str;
} public void description()
{ String description="共有数据数:"+this.getTotalRows()+ "共有页数: "+this.getTotalPages() + "当前页数为:"+this.getCurrentPage()+
" 是否有前一页: "+this.isHasPreviousPage() + " 是否有下一页:"+this.isHasNextPage()+ " 开始行数:"+this.getPageStartRow()+ " 终止行数:"+this.getPageEndRow(); System.out.println(description); } }
public class PageListAction extends Action { // --------------------------------------------------------- Instance Variables // --------------------------------------------------------- Methods
public PageListAction(){}
ArrayList arrayList=new ArrayList();
PageBean pb;
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
String action;
action=request.getParameter("action");
if(action==null || action.equals("null")){ //第一次读取数据
try{
DataSource datasource=this.getDataSource(request,Constants.DATASOURCE_KEY);
arrayList=Book.getAllBook(datasource.getConnection());
System.out.println("第一步,数据已经成功传递到Action,action="+action);
}catch(Exception e){
e.printStackTrace();
System.out.println("数据库连接出现异常");
}
pb=new PageBean(arrayList);
Book[] books=pb.getBooks();
pb.description();
request.setAttribute("result",books);
request.setAttribute("page",pb);
}
else
{
if(action=="nextPage" || action.equals("nextPage"))
{
System.out.println("参数action="+action);
System.out.println("函数pb.getNextPage()准备执行");
Book[]books=pb.getNextPage();
request.setAttribute("page",pb);
request.setAttribute("result",books);
}
if(action=="previousPage" || action.equals("previousPage"))
{
System.out.println("参数action="+action);
System.out.println("函数pb.getPreviouspage()准备执行");
Book[] books=pb.getPreviouspage();
request.setAttribute("page",pb);
request.setAttribute("result",books);
}
}
return (mapping.findForward("success"));
}
} 5、测试的JSP
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ page contentType="text/html; charset=gb2312" language="java"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html:html locale="true">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body> <table border="1">
<tr><th>书名</th><th>作者</th><th>价格</th></tr>
<logic:present name="result" scope="request">
<logic:iterate id="book" name="result" type="app.Book" >
<logic:present name="book">
<tr>
<td><bean:write name="book" property="bookname" /></td>
<td> <bean:write name="book" property="author" /></td>
<td><bean:write name="book" property="price" /></td>
</tr>
</logic:present>
</logic:iterate>
</logic:present>
</table> <logic:present name="page" scope="request"><logic:equal name="page" property="hasNextPage" value="true">
<html:link page="/page.do?action=nextPage">nextPage</html:link>
</logic:equal>
<logic:equal name="page" property="hasPreviousPage" value="true">
<html:link page="/page.do?action=previousPage">PreviousPage</html:link>
</logic:equal>
</logic:present>
共有数据总数<bean:write name="page" property="totalRows"/>;
共分<bean:write name="page" property="totalPages"/>页,当前是第
<bean:write name="page" property="currentPage"/>页 </body>
</html:html>6、STRUTS-CONFIG。XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<data-sources>
<data-source>
<set-property property="autoCommit" value="true" />
<set-property property="driverClass" value="com.microsoft.jdbc.sqlserver.SQLServerDriver" />
<set-property property="url" value="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=yqdb" />
<set-property property="user" value="sa" />
<set-property property="password" value="" />
<set-property property="maxActive" value="10" />
<set-property property="maxWait" value="5000" />
</data-source>
</data-sources>
<form-beans>
</form-beans> <global-exceptions />
<global-forwards />
<action-mappings >
<action path="/page" type="app.PageListAction" scope="request">
<forward name="success" path="/pagetest.jsp"/>
</action>
</action-mappings> <message-resources parameter="app.ApplicationResources" />
</struts-config>运行后出错:javax.servlet.ServletException: "Cannot find bean page"请问:如何解决?