二、建立公共类1、AbstractAction类Struts2和Struts1.x的差别,最明显的就是Struts2是一个pull-MVC架构。Struts1.x必须继承org.apache.struts.action.Action或者其子类,表单数据封装在FormBean中。Struts2无须继承任何类型或实现任何接口,表单数据包含在Action中,通过Getter和Setter获取。虽然,在理论上Struts2的Action无须实现任何接口或者是继承任何的类,但是,在实际编程过程中,为了更加方便的实现Action,大多数情况下都会继承com.opensymphony.xwork2.ActionSupport类,并且重载(Override)com.sterning.commons.AbstractAction.java参考JavaDoc,可知ActionSupport类实现了接口:com.opensymphony.xwork2.Actioncom.opensymphony.xwork2.LoaleProvidercom.opensymphony.xwork2.TextProvidercom.opensymphony.xwork2.Validateablecom.opensymphony.xwork2.ValidationAwarecom.uwyn.rife.continuations.ContinuableObjectjava.io.Searializablejava.lang.Cloneable2、Pager分页类为了增加程序的分页功能,特意建立共用的分页类。packagecom.sterning.commons;importjava.math.*;publicclassPager{privateinttotalRows;//总行数privateintpageSize=5;//每页显示的行数privateintcurrentPage;//当前页号privateinttotalPages;//总页数privateintstartRow;//当前页在数据库中的起始行publicPager(){}publicPager(int_totalRows){totalRows=_totalRows;totalPages=totalRows/pageSize;intmod=totalRows%pageSize;if(mod0){totalPages++;}currentPage=1;startRow=0;}publicintgetStartRow(){returnstartRow;}publicintgetTotalPages(){returntotalPages;}publicintgetCurrentPage(){returncurrentPage;}publicintgetPageSize(){returnpageSize;}publicvoidsetTotalRows(inttotalRows){this.totalRows=totalRows;}publicvoidsetStartRow(intstartRow){this.startRow=startRow;}publicvoidsetTotalPages(inttotalPages){this.totalPages=totalPages;}publicvoidsetCurrentPage(intcurrentPage){this.currentPage=currentPage;}publicvoidsetPageSize(intpageSize){this.pageSize=pageSize;}publicintgetTotalRows(){returntotalRows;}publicvoidfirst(){currentPage=1;startRow=0;}publicvoidprevious(){if(currentPage==1){return;}currentPage--;startRow=(currentPage-1)*pageSize;}publicvoidnext(){if(currentPagetotalPages){currentPage++;}startRow=(currentPage-1)*pageSize;}publicvoidlast(){currentPage=totalPages;startRow=(currentPage-1)*pageSize;}publicvoidrefresh(int_currentPage){currentPage=_currentPage;if(currentPagetotalPages){last();}}}com.sterning.commons.Pager.java同时,采用PagerService类来发布成为分页类服务PagerService,代码如下:同时,采用PagerService类来发布成为分页类服务PagerService,代码如下:packagecom.sterning.commons;publicclassPagerService{publicPagergetPager(StringcurrentPage,StringpagerMethod,inttotalRows){//定义pager对象,用于传到页面Pagerpager=newPager(totalRows);//如果当前页号为空,表示为首次查询该页//如果不为空,则刷新pager对象,输入当前页号等信息if(currentPage!=null){pager.refresh(Integer.parseInt(currentPage));}//获取当前执行的方法,首页,前一页,后一页,尾页。if(pagerMethod!=null){if(pagerMethod.equals(first)){pager.first();}elseif(pagerMethod.equals(previous)){pager.previous();}elseif(pagerMethod.equals(next)){pager.next();}elseif(pagerMethod.equals(last)){pager.last();}}returnpager;}}com.sterning.commons.PagerService.java三、建立数据持久化层1、编写实体类Books及books.hbm.xml映射文件。packagecom.sterning.books.model;importjava.util.Date;publicclassBooks{//FieldsprivateStringbookId;//编号privateStringbookName;//书名privateStringbookAuthor;//作者privateStringbookPublish;//出版社privateDatebookDate;//出版日期privateStringbookIsbn;//ISBNprivateStringbookPage;//页数privateStringbookPrice;//价格privateStringbookContent;//内容提要//ConstructorspublicBooks(){}//PropertyaccessorspublicStringgetBookId(){returnbookId;}publicvoidsetBookId(StringbookId){this.bookId=bookId;}publicStringgetBookName(){returnbookName;}publicvoidsetBookName(StringbookName){this.bookName=bookName;}publicStringgetBookAuthor(){returnbookAuthor;}publicvoidsetBookAuthor(StringbookAuthor){this.bookAuthor=bookAuthor;}publicStringgetBookContent(){returnbookContent;}publicvoidsetBookContent(StringbookContent){this.bookContent=bookContent;}publicDategetBookDate(){returnbookDate;}publicvoidsetBookDate(DatebookDate){this.bookDate=bookDate;}publicStringgetBookIsbn(){returnbookIsbn;}publicvoidsetBookIsbn(StringbookIsbn){this.bookIsbn=bookIsbn;}publicStringgetBookPage(){returnbookPage;}publicvoidsetBookPage(StringbookPage){this.bookPage=bookPage;}publicStringgetBookPrice(){returnbookPrice;}publicvoidsetBookPrice(StringbookPrice){this.bookPrice=bookPrice;}publicStringgetBookPublish(){returnbookPublish;}publicvoidsetBookPublish(StringbookPublish){this.bookPublish=bookPublish;}}com.sterning.books.model.Books.java接下来要把实体类Books的属性映射到books表,编写下面的books.hbm.xml文件:?xmlversion=1.0?!DOCTYPEhibernate-mappingPUBLIC-//Hibernate/HibernateMappingDTD3.0//EN=com.sterning.books.model.Bookstable=booksidname=bookIdtype=stringcolumnname=book_idlength=5/generatorclass=assigned//idpropertyname=bookNametype=stringcolumnname=book_namelength=100//propertypropertyname=bookAuthortype=stringcolumnname=book_authorlength=100//propertypropertyname=bookPublishtype=stringcolumnname=book_publishlength=100//propertypropertyname=bookDatetype=java.sql.Timestampcolumnname=book_datelength=7//propertypropertyname=bookIsbntype=stringcolumnname=book_isbnlength=20//propertypropertyname=bookPagetype=stringcolumnname=book_pagelength=11//propertypropertyname=bookPricetype=strin