[Original] completely solve the Struts page display

xiaoxiao2021-03-06  53

Thoroughly solve Struts page display

Author: LI Min-qiang eMail: mill_lmq@tom.com

Learning Struts has been more than 2 months. Friends in the first few days asked me that Struts page displayed, I feel like it is almost in JSP, but I still have a problem like this, it takes a few days. I don't know the questions. I feel that I still write some things to share with you!

As for Struts, you will not introduce it here, and friends who don't understand can first look at other articles on the Internet.

A development environment

Elicpse Struts Studio SQLServer2000 Tomcat.

Two development ideas

Since Struts, it is natural that the MVC is inseparable from MVC.

1 Establish an appropriate model assembly, and correspond to the table in the database. This part is served by JavaBean we are familiar with. And establish a database query method, which requires a parameter for a java.sql.contection type and returns an ArrayList. In this example, Book.java

2 Creating the model components required for paging, but also served as JavaBean, constructed by ArrayList provided in Book. This example is PageBean.java..

3 Set up the controller component, which is implemented by the action in Struts. Mainly responsible for instantiation book, and use the return ArrayList object to construct the PageBean. And receive the Action parameters that are passed by the view. Thereby different methods in the PageBean object, the method returns the BOOK [] object. Finally, Book [] and PageBean are placed in Request. This example is PagelistAction.java.

4 Establish a view component, this part is acting as JSP, in order not to appear Java code, we use the Struts provided by the label library, which is mainly responsible for taking out the object that just put into the object, and implements paging display by repeated calling the PageListAction and the Action parameters. . This example is PageTest.jsp.

5 Establish and configure struts-config.xml.

6 Set up the database.

Three instance code

1 book.java

Package bean;

Import java.sql. *;

Import java.util.arraylist;

/ **

* @ Author Li Minqiang

* Struts page display data bean, the BOOK table in the corresponding database

* /

Public class book {

Private string bookname; // Title

Private string author; // author

Private string price; // 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 data query has been completed!");

While (resultSet.next ())

{

String name = resultSet.getstring ("name");

String author = resultSet.getstring ("author");

String price = resultSet.getstring ("price");

System.out.Println ("Start Data Package: 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 ("Database Exception" E.TOString ());

}

Return arraylist;

}

}

2 PageBean.java

Package page;

IMPORT bean.book;

Import java.util. *;

/ **

* @ Author Li Minqiang

* Struts Page Display Logic Bean

* /

Public class pagebean {

INT CurrentPage = 1; // Current page

Public int totalpages = 0; // Total number

INT pageRecorders = 5; // 5 data per page

INT TOTALROWS = 0; // Total data

INT PageStartrow = 0; // The starting number of each page

INT pageEndrow = 0; / / The number of termination of the data per page

Boolean HasnextPage = false; // Is there a next page

Boolean Haspreviouspage = false; // Is there a previous page

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 / PageRecorder 1;

}

IF (CurrentPage> = Totalpages)

{

HasnextPage = false;

}

Else

{

HasnextPage = True;

}

IF (TotalRows

{

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 pageRecorder to set.

* /

Public void setpageRecorders (int pagerecorders) {

THIS.PAGERECORDERS = PageRecorders;

}

/ **

* @Return Returns the pageeendrow.

* /

Public int getpagendrow () {

Return PageEndRow;

}

/ **

* @Return Returns the Pagestartrow.

* /

Public int getpagestRow () {

Return PagestartRow;

}

/ **

* @Return Returns The Totalpages.

* /

Public string gettotalpages () {

Return this.toTOString (Totalpages);

}

/ **

* @Return Returns The TotalRows.

* /

Public string gettotaocks () {

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 ishasisPreviouspage () {

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 () is being executed;");

System.out.println ("Parameter CurrentPage =" CURRENTPAGE);

IF (CurrentPage-1)> 0)

{

Haspreviouspage = true;

}

Else

{

Haspreviouspage = false;

}

IF (CurrentPage> = Totalpages)

{

HasnextPage = false;

}

Else

{

HasnextPage = True;

}

System.out.println ("Parameters HasnextPage =" HasnextPage);

System.out.println ("Preparing to perform 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 () begins;");

IF (CurrentPage * PageRecorder

Judgment is the last page

PageEndrow = CURRENTPAGE * PAGERECORDERS;

Pagestartrow = PageEndrow-PageRecorders;

}

Else {

PageEndrow = TotalRows;

Pagestartrow = PageRecorders * (Totalpages-1);

}

Book [] Books = New Book [PageEndrow-Pagestartrow 1];

System.out.println ("PagestRow =" PagestRrow);

System.out.println ("PageEndrow =" PageEndrow);

INT j = 0;

For (int I = PagestRow; i

{

Book book = (book) arraylist.get (i); books [j ] = book;

}

System.out.println ("The page data to be displayed has been packaged, the specific information is as follows:");

THIS.DESCRIPTION ();

Return Books;

}

Public String Tostring (int Temp)

{

String str = integer.toString (TEMP);

Return Str;

}

Public void description ()

{

String description = "A total of data:" this.gettotalrows ()

"Candid page number:" this.gettotalpages ()

"The current number of pages is:" this.getCurrentPage ()

"Is there a previous page:" this.ishaspreviouspage ()

"Is there a next page:" this.ishasnextpage ()

"Start the number of lines:" this.getpagestRow ()

Termination of the number of lines: " this.getPageEndrow ();

System.out.println (Description); DESCRIPTION;

}

}

3 Pagelistion.java

Package page;

Import org.apache.struts.action. *;

Import javax.servlet.http. *;

Import comm.constants;

IMPORT bean.book;

Import java.util. *;

Import javax.sql.datasource;

/ **

* @Author Li Minqiang

* Struts page Displays Action

* /

Public class pagelistAction extends action {

Public PagelistAction () {}

ArrayList arraylist = new arraylist ();

Pagebean PB;

Public ActionForward Execute (ActionMapping Mapping, ActionMM Form, HttpservletRequest Request, httpservletResponse response) throws exception {

String action;

Action = Request.getParameter ("action");

IF (action == null || action.equals ("null")) {// First read data

Try {

DataSource Datasource = this.getDataSource (Request, Constants.DataSource_Key);

ArrayList = book.getallbook (DataSource.getConnection ());

System.out.println ("first step, data has been successfully passed to action, action =" action);

} catch (exception e) {

E.PrintStackTrace ();

System.out.println ("Database Connecting Exception");

}

PB = New Pagebean (arraylist);

Book [] books = pb.getbooks ();

Pb.description ();

Request.setattribute ("Result", Books;

Request.setttribute ("Page", PB);

}

Else

{

IF (action == "NextPage" || action.equals ("nextpage"))

{

System.out.println ("Parameter Action =" Action;

System.out.println ("Function PB.GetNextPage () Prepare");

Book [] books = pb.getnextpage ();

Request.setttribute ("Page", PB);

Request.setattribute ("Result", Books;

}

IF (action == "previouspage" || action.equals ("previouspage)))))

{

System.out.println ("Parameter Action =" Action;

System.out.println ("Function PB.GetPreviouspage () Prepare");

Book [] books = pb.getpreviouspage ();

Request.setttribute ("Page", PB);

Request.setattribute ("Result", Books;

}

}

Mapping.Findforward ("Success"));

}

}

4 PageTest.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"%>

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

Title Author Price

NextPage previouspage>

A total of total data ; copies page, currently the current No. BEAN: WRITE Name = " Page "Property =" CurrentPage "/> Page

5 Struts-Config.xml

6 Create an eBookStore database, as well as Table Book (name, author, parce); where data can be configured in Struts-Config.xml according to your different situations.

7 Constants.java

Package CommM;

/ ** * this interface provides the constant string for applicator constant * / public class Constants {/ ** * name of the User Object in HttpSession * / public static String USER_KEY = "user"; / ** * dataSource name * / public Static string DataSource_key = "Datasource"; ! Mill_lmq@tom.com

转载请注明原文地址:https://www.9cbs.com/read-118042.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.034, SQL: 9