说道Struts自然是不能离开MVC模式,分页显示也是如此.我不知道我的算法是不是算好的,也希望看过的朋友能发表一下自己的看法,下面简单阐述一下主要的开发思路:
1)建立适当的模型组件,对应你要查询数据库中的表,这部分由熟悉的JavaBean来充当.并在其中建立数据库查询方法,该方法需要一个java.sql.Conntection类型的参数,并返回一个ArrayList,在本例中为Book.java,另外还有一个数据库连接的Bean是SqlBean.java.
2)建立分页所需要的模型组件,也是用JavaBean,通过Book提供的ArrayList来构造,这里用的是PageBean.java.
3)建立控制器组件,这部分由Struts的Action来实现,主要负责实例化Book,并利用返回的ArrayList对象,构造PageBean,以及接收由视图传递来的action参数,从而在PageBean对象中调用不同的方法,该方法返回Book[]对象,最后将Book[]和PageBean放入到request中.本Action为PageListAction.java.
4)建立视图组件,这部分JSP来实现,为了不出现JAVA代码,使用Struts提供的标签库,主要负责从Request中取出刚刚放入的对象,通过反复调用Action以及action参数,而实现分页显示,是pagetest.jsp.
5)建立并配置struts-config.xml和web.xml文件.
6)建立数据库.
我对代码进行了实现,调试通过.如果大家有更好的用Struts实现分页显示的算法可以给我留言,代码如下:
struts-config.xml文件如下,这里配置了一个接收haha的请求提交给PageListAction
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<action-mappings>
<action path="/haha"
type="page.PageListAction"
scope="request">
<forward name="success" path="/pagetest.jsp"/>
</action>
</action-mappings>
<message-resources parameter="ApplicationResources" />
</struts-config>
……………………………………………………………………………….
Web.xml文件如下,这里配置了Web的信息,基本都是由Eclipse完成的
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>application</param-name>
<param-value>ApplicationResources</param-value>
</init-param>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
…………………………………………………………………………
这个是控制器Action类
package page;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import bean.Book;
import java.util.*;
import javax.sql.DataSource;
public class PageListAction extends Action {
ArrayList arrayList=new ArrayList();
PageBean pd;
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
String action;
action=request.getParameter("action");
if(action==null||action.equals("null")) {
try {
arrayList=Book.getAllBook();
}
catch(Exception e) {
e.printStackTrace();
}
pd=new PageBean(arrayList);
Book[] books=pd.getBooks();
request.setAttribute("result",books);
request.setAttribute("page",pd);
}
else {
if(action=="nextPage"||action.equals("nextPage")) {
Book[] books=pd.getNextPage();
request.setAttribute("result",books);
request.setAttribute("page",pd);
}
if(action=="previousPage"||action.equals("previousPage")) {
Book[] books=pd.getPreviousPage();
request.setAttribute("resule",books);
request.setAttribute("page",pd);
}
}
return mapping.findForward("success");
}
}
………………………………………………………………………………….
这个是pagebean类,主要负责分页算法和逻辑处理
package page;
import bean.Book;
import java.util.*;
public class PageBean {
int currentPage=1;//当前页数
public int totalPages=0;//总页数
int pageRecorders=2;//每页显示数
int totalRows=0;//总数据数
int pageStartRow=0;//每页的起始数
int pageEndRow;//每页的终止数
boolean hasNextPage=false;//是否有下一页
boolean hasPreviousPage=false;//是否有前一页
ArrayList arrayList;
Iterator it;
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;
}
}
public void setCurrentPage(int currentPage) {
this.currentPage=currentPage;
}
public void setPageRecorders(int pageRecorders) {
this.pageRecorders=pageRecorders;
}
public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage=hasNextPage;
}
public void setHasPreviosPage(boolean hasPreviosPage) {
this.hasPreviousPage=hasPreviousPage;
}
public String getCurrentPage() {
return this.toString(currentPage);
}
public String getTotalPages() {
return this.toString(totalPages);
}
public String getTotalRow() {
return this.toString(totalRows);
}
public int getPageRecorders() {
return pageRecorders;
}
public int getPageEndRow() {
return pageEndRow;
}
public int getPageStartRow() {
return pageStartRow;
}
public boolean isHasNextPage() {
return hasNextPage;
}
public boolean isHasPreviousPage() {
return hasPreviousPage;
}
public Book[] getNextPage() {
currentPage=currentPage+1;
if((currentPage-1)>0) {
hasPreviousPage=true;
}
else {
hasPreviousPage=false;
}
if(currentPage>=totalPages) {
hasNextPage=false;
}
else {
hasNextPage=true;
}
Book[] books=getBooks();
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();
return books;
}
public Book[] 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];
int j=0;
for(int i=pageStartRow;i<pageEndRow;i++) {
Book book=(Book)arrayList.get(i);
books[j++]=book;
}
return books;
}
public String toString(int temp) {
String str=Integer.toString(temp);
return str;
}
}
……………………………………………………………………………….
Book类,负责查询数据库,把结果放到一个ArrayList中
package bean;
import java.sql.*;
import java.util.ArrayList;
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 void setBookname(String bookname) {
this.bookname=bookname;
}
public void setAuthor(String Author) {
this.author=author;
}
public void setPrice(String price) {
this.price=price;
}
public String getBookname() {
return bookname;
}
public String getAuthor() {
return author;
}
public String getPrice() {
return price;
}
public static ArrayList getAllBook() throws Exception {
String sql="select * from book";
SqlBean sq=new SqlBean();
ArrayList arrayList=new ArrayList();
try
{
ResultSet resultSet=sq.select(sql);
while(resultSet.next()) {
String name=resultSet.getString("name");
String author=resultSet.getString("author");
String price=resultSet.getString("price");
Book book=new Book(name,author,price);
arrayList.add(book);
}
resultSet.close();
}
catch(SQLException e)
{
System.out.println("数据库错误"+e.toString());
}
return arrayList;
}
}
………………………………………………………………………………..
这个是SqlBook,负责和数据库建立一个连接的Bean
package bean;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class SqlBean {
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=eBookStore";
Connection con=null;
Statement sta=null;
public SqlBean() {
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con=DriverManager.getConnection(url,"sa","");
sta=con.createStatement();
}
catch(Exception e)
{
System.out.println("连接错误"+e.toString());
}
}
public ResultSet select(String selects) throws Exception
{
return sta.executeQuery(selects);
}
}
…………………………………………………………………………
这个是负责显示分页的JSP页.pagetest.jsp
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%@ page contentType="text/html;charset=gb2312"%>
<html:html locale="true">
<head>
</head>
<body>
<table border="1">
<tr><th>书名</th><th>作者</th><th>价格</th></tr>
<logic:present name="result">
<logic:iterate id="book" name="result" type="bean.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">
<logic:equal name="page" property="hasNextPage" value="true">
<html:link page="/haha.do?action=nextPage">nextPage</html:link>
</logic:equal>
<logic:equal name="page" property="hasPreviousPage" value="true">
<html:link page="/haha.do?action=previousPage">previousPage</html:link>
</logic:equal>
共分<bean:write name="page" property="totalPages"/>页显示,当前是
<bean:write name="page" property="currentPage"/>页.
</logic:present>
</body>
</html:html>
…………………………………………………………………………………
这个是首页的JSP页面,只有一个连接.提交一个haha.do的请求
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ page contentType="text/html;charset=gb2312" language="java"%>
<html:html>
<head>
</head>
<body>
<html:link action="haha.do">GotoPage</html:link>
</body>
</html:html>
posted on 2005-11-12 09:13
我心依旧 阅读(15116)
评论(25) 编辑 收藏