免费高清特黄a大片,九一h片在线免费看,a免费国产一级特黄aa大,国产精品国产主播在线观看,成人精品一区久久久久,一级特黄aa大片,俄罗斯无遮挡一级毛片

分享

Java in Action - Struts分頁(yè)顯示

 duduwolf 2005-11-13

    說(shuō)道Struts自然是不能離開(kāi)MVC模式,分頁(yè)顯示也是如此.我不知道我的算法是不是算好的,也希望看過(guò)的朋友能發(fā)表一下自己的看法,下面簡(jiǎn)單闡述一下主要的開(kāi)發(fā)思路:

1)建立適當(dāng)?shù)哪P徒M件,對(duì)應(yīng)你要查詢(xún)數(shù)據(jù)庫(kù)中的表,這部分由熟悉的JavaBean來(lái)充當(dāng).并在其中建立數(shù)據(jù)庫(kù)查詢(xún)方法,該方法需要一個(gè)java.sql.Conntection類(lèi)型的參數(shù),并返回一個(gè)ArrayList,在本例中為Book.java,另外還有一個(gè)數(shù)據(jù)庫(kù)連接的Bean是SqlBean.java.

2)建立分頁(yè)所需要的模型組件,也是用JavaBean,通過(guò)Book提供的ArrayList來(lái)構(gòu)造,這里用的是PageBean.java.

3)建立控制器組件,這部分由Struts的Action來(lái)實(shí)現(xiàn),主要負(fù)責(zé)實(shí)例化Book,并利用返回的ArrayList對(duì)象,構(gòu)造PageBean,以及接收由視圖傳遞來(lái)的action參數(shù),從而在PageBean對(duì)象中調(diào)用不同的方法,該方法返回Book[]對(duì)象,最后將Book[]和PageBean放入到request中.本Action為PageListAction.java.

4)建立視圖組件,這部分JSP來(lái)實(shí)現(xiàn),為了不出現(xiàn)JAVA代碼,使用Struts提供的標(biāo)簽庫(kù),主要負(fù)責(zé)從Request中取出剛剛放入的對(duì)象,通過(guò)反復(fù)調(diào)用Action以及action參數(shù),而實(shí)現(xiàn)分頁(yè)顯示,是pagetest.jsp.

5)建立并配置struts-config.xml和web.xml文件.

6)建立數(shù)據(jù)庫(kù).

我對(duì)代碼進(jìn)行了實(shí)現(xiàn),調(diào)試通過(guò).如果大家有更好的用Struts實(shí)現(xiàn)分頁(yè)顯示的算法可以給我留言,代碼如下:

struts-config.xml文件如下,這里配置了一個(gè)接收haha的請(qǐng)求提交給PageListAction

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts./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./xml/ns/j2ee" xmlns:xsi="http://www./2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java./xml/ns/j2ee   http://java./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>

…………………………………………………………………………

這個(gè)是控制器Action類(lèi)

 

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");

    }

}

………………………………………………………………………………….

這個(gè)是pagebean類(lèi),主要負(fù)責(zé)分頁(yè)算法和邏輯處理

 

package page;

 

import bean.Book;

import java.util.*;

 

public class PageBean {

    int currentPage=1;//當(dāng)前頁(yè)數(shù)

    public int totalPages=0;//總頁(yè)數(shù)

    int pageRecorders=2;//每頁(yè)顯示數(shù)

    int totalRows=0;//總數(shù)據(jù)數(shù)

    int pageStartRow=0;//每頁(yè)的起始數(shù)

    int pageEndRow;//每頁(yè)的終止數(shù)

    boolean hasNextPage=false;//是否有下一頁(yè)

    boolean hasPreviousPage=false;//是否有前一頁(yè)

    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類(lèi),負(fù)責(zé)查詢(xún)數(shù)據(jù)庫(kù),把結(jié)果放到一個(gè)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("數(shù)據(jù)庫(kù)錯(cuò)誤"+e.toString());

        }

        return arrayList;

    }

}

………………………………………………………………………………..

這個(gè)是SqlBook,負(fù)責(zé)和數(shù)據(jù)庫(kù)建立一個(gè)連接的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("連接錯(cuò)誤"+e.toString());

        }

    }

   

    public ResultSet select(String selects) throws Exception

    {

        return sta.executeQuery(selects);

    }

}

…………………………………………………………………………

這個(gè)是負(fù)責(zé)顯示分頁(yè)的JSP頁(yè).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>書(shū)名</th><th>作者</th><th>價(jià)格</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"/>頁(yè)顯示,當(dāng)前是

                 <bean:write name="page" property="currentPage"/>頁(yè).

    </logic:present>

  </body>

</html:html>

…………………………………………………………………………………

這個(gè)是首頁(yè)的JSP頁(yè)面,只有一個(gè)連接.提交一個(gè)haha.do的請(qǐng)求

 

<%@ 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>

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多