Struts page display of resolve: LI Min-qiang learning Struts has been more than two months, the group of friends a few days ago I asked questions Struts page displayed, and almost feel like in the jsp, but still encounter such problems, It's hard to spend a few days to make questions, I feel that I still write something 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. An development environment Elicpse Struts Studio SQLServer2000 Tomcat. Second Development Ideas Since Struts, then naturally cannot be causing MVC, and the page display is also true. 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. The model components required for Book.java2 are built in this example, and JavaBean is used as a configuration, constructed by ArrayList provided by the 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. . In this example, StrutS-Config.xml is established for PageTest.jsp.5. 6 Set up the database.
Three instance code 1 book.javaPackage bean; import java.sql. *; Import java.util.arraylist; / ** * @ Author Li Minqiang * Struts page display data bean, corresponding database Book table * / public class book {private string BookName; // Top 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 = result set.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 PageBe; Import bean.book; import java.util. *; / ** * @ Author Li Minqiang * Struts page display logic bean * / public class pagebean {int currentPage = 1; // Current page public int int int INT Totalpages = 0; // Total Page Int PageRecorders = 5; // 5 data per page INT TOTALROWS = 0; // Total data INT PageStRow = 0; // The start number INT PageEndRow = 0 per page; / / Number of termination of data in each page boolean hasnextpage = false; // Is there a next page boolean Haspreviouspage = false; // Is there a previous page arraylist arrayList; item 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 . 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.toString (totalPages);}. / ** * @return Returns the totalRows * / public String getTotalRows () {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 isHasPreviousPage () {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 ("PA GeBean.getNextPage () is being executed; "); system.out.println (" Parameter currentpage = " currentpage); if ((currentpage-1)> 0) {HaspReviOUSPAGE = true;} else {HaspReviOUSPAGE = false;} (currentPage> = totalPages) {hasNextPage = false;} else {hasNextPage = true;} System.out.println ( "parameter hasNextPage =" hasNextPage); System.out.println ( "ready to execute 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 () started;"); if (currentPage * pageRecorders author LI Min-qiang * Struts pagination Action * / public class PageListAction extends Action {public PageListAction () {} ArrayList arrayList = new ArrayList (); PageBean pb; 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")) {// 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 connection 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.SetAttribute ("Page", PB); Request.setttribute ("Result", Books);} if (action == "previouspage" || action.equals ("previouspage")) {system.out.println ("parameter action =" action; system.out .println ("Function Pb.GetPreviouspage () Prepare"); book [] books = pb.getpreviouS.SetAttribute ("page", pb); request.settribute ("result", books);} return (mapping.findforward " "));}} 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 "%>