posts - 310, comments - 6939, trackbacks - 0, articles - 3
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          Struts2+Spring2+Hibernate3 web應(yīng)用示例(四)

          Posted on 2007-09-30 09:56 詩特林 閱讀(13755) 評(píng)論(5)  編輯  收藏 所屬分類: Struts
                                                        Struts2+Spring2+Hibernate3 web應(yīng)用示例(四)

        1. Struts2+Spring2+Hibernate3 web應(yīng)用示例------源代碼
        2. Struts2+Spring2+Hibernate3 web應(yīng)用示例(七)
        3. Struts2+Spring2+Hibernate3 web應(yīng)用示例(六)
        4. Struts2+Spring2+Hibernate3 web應(yīng)用示例(五)
        5. Struts2+Spring2+Hibernate3 web應(yīng)用示例(四)
        6. Struts2+Spring2+Hibernate3 web應(yīng)用示例(三)
        7. Struts2+Spring2+Hibernate3 web應(yīng)用示例(二)
        8. Struts2+Spring2+Hibernate3 web應(yīng)用示例(一)

        9.  

          六、        創(chuàng)建Action類:BookAction 

          Struts 1.x經(jīng)驗(yàn)的朋友都知道ActionStruts的核心內(nèi)容,當(dāng)然Struts 2.0也不例外。不過,Struts 1.xStruts 2.0Action模型很大的區(qū)別。

           

          Struts 1.x

          Stuts 2.0

          接口

          必須繼承org.apache.struts.action.Action或者其子類

          無須繼承任何類型或?qū)崿F(xiàn)任何接口

          表單數(shù)據(jù)

          表單數(shù)據(jù)封裝在FormBean

          表單數(shù)據(jù)包含在Action中,通過GetterSetter獲取

          1、建立BookAction

          package com.sterning.books.web.actions;

          import java.util.Collection;

          import com.sterning.books.model.Books;
          import com.sterning.books.services.iface.IBooksService;
          import com.sterning.commons.AbstractAction;
          import com.sterning.commons.Pager;
          import com.sterning.commons.PagerService;

          public class BooksAction extends AbstractAction {
              
              
          private IBooksService booksService;
              
          private PagerService pagerService;
              
              
          private Books book;
              
          private Pager pager;
              
              
          protected Collection availableItems;
              
          protected String currentPage;
              
          protected String pagerMethod;
              
          protected String totalRows;
              
          protected String bookId;
              
          protected String queryName;
              
          protected String queryValue;
              
          protected String searchName;
              
          protected String searchValue;
              
          protected String queryMap;
              
              
          public String list() throws Exception {
                  
          if(queryMap ==null||queryMap.equals("")){
                      
                  }
          else{
                      String[] str
          =queryMap.split("~");
                      
          this.setQueryName(str[0]);
                      
          this.setQueryValue(str[1]);
                  }

                  
                  System.out.println("
          asd"+this.getQueryValue());
                  
          int totalRow=booksService.getRows(this.getQueryName(),this.getQueryValue());
                  pager
          =pagerService.getPager(this.getCurrentPage(), this.getPagerMethod(), totalRow);
                  
          this.setCurrentPage(String.valueOf(pager.getCurrentPage()));
                  
          this.setTotalRows(String.valueOf(totalRow));
                  availableItems
          =booksService.getBooks(this.getQueryName(),this.getQueryValue(),pager.getPageSize(), pager.getStartRow());
                  
                  
          this.setQueryName(this.getQueryName());
                  
          this.setQueryValue(this.getQueryValue());
                  
                  
          this.setSearchName(this.getQueryName());
                  
          this.setSearchValue(this.getQueryValue());
                  
                  
          return SUCCESS;         
              }

              
              public
           String load() throws Exception {
                  
          if(bookId!=null)
                      book 
          = booksService.getBook(bookId);
                  
          else
                      bookId
          =booksService.getMaxID();
                  
          return SUCCESS;
              }

              
              public
           String save() throws Exception {
                  
          if(this.getBook().getBookPrice().equals("")){
                      
          this.getBook().setBookPrice("0.0");
                  }

                  
                  String id=
          this.getBook().getBookId();
                  Books book
          =booksService.getBook(id);
                  
                  
                  
                  
          if(book == null)
                      booksService.addBook(
          this.getBook());
                  
          else
                      booksService.updateBook(
          this.getBook());
                  
                  
          this.setQueryName(this.getQueryName());
                  
          this.setQueryValue(this.getQueryValue());
                  
                  
          if(this.getQueryName()==null||this.getQueryValue()==null||this.getQueryName().equals("")||this.getQueryValue().equals("")){
                      
                  }
          else{
                      queryMap
          =this.getQueryName()+"~"+this.getQueryValue();
                  }
                  
                  
                  
          return SUCCESS;
              }

              
              public
           String delete() throws Exception {
                  booksService.deleteBook(
          this.getBookId());
                  
                  
          if(this.getQueryName()==null||this.getQueryValue()==null||this.getQueryName().equals("")||this.getQueryValue().equals("")){
                      
                  }
          else{
                      queryMap
          =this.getQueryName()+"~"+this.getQueryValue();
                  }

                  
          return SUCCESS;
              }
              
              
              
          public Books getBook() {
                  
          return book;
              }


              public
           void setBook(Books book) {
                  
          this.book = book;
              }


              public
           IBooksService getBooksService() {
                  
          return booksService;
              }


              public
           void setBooksService(IBooksService booksService) {
                  
          this.booksService = booksService;
              }


              public
           Collection getAvailableItems() {
                  
          return availableItems;
              }


              public
           String getCurrentPage() {
                  
          return currentPage;
              }


              public
           void setCurrentPage(String currentPage) {
                  
          this.currentPage = currentPage;
              }


              public
           String getPagerMethod() {
                  
          return pagerMethod;
              }


              public
           void setPagerMethod(String pagerMethod) {
                  
          this.pagerMethod = pagerMethod;
              }


              public
           Pager getPager() {
                  
          return pager;
              }


              public
           void setPager(Pager pager) {
                  
          this.pager = pager;
              }


              public
           String getTotalRows() {
                  
          return totalRows;
              }


              public
           void setTotalRows(String totalRows) {
                  
          this.totalRows = totalRows;
              }

                  
              public
           String getBookId() {
                  
          return bookId;
              }


              public
           void setBookId(String bookId) {
                  
          this.bookId = bookId;
              }


              public
           String getQueryName() {
                  
          return queryName;
              }


              public
           void setQueryName(String queryName) {
                  
          this.queryName = queryName;
              }


              public
           String getQueryValue() {
                  
          return queryValue;
              }


              public
           void setQueryValue(String queryValue) {
                  
          this.queryValue = queryValue;
              }

              
              public
           String getSearchName() {
                  
          return searchName;
              }


              public
           void setSearchName(String searchName) {
                  
          this.searchName = searchName;
              }


              public
           String getSearchValue() {
                  
          return searchValue;
              }


              public
           void setSearchValue(String searchValue) {
                  
          this.searchValue = searchValue;
              }

              
              public
           String getQueryMap() {
                  
          return queryMap;
              }


              public
           void setQueryMap(String queryMap) {
                  
          this.queryMap = queryMap;
              }

              
              public
           PagerService getPagerService() {
                  
          return pagerService;
              }



              public
           void setPagerService(PagerService pagerService) {
                  
          this.pagerService = pagerService;
              }
              
          }

          com.sterning.books.web.actions.BookAction.java

          1)、默認(rèn)情況下,當(dāng)請(qǐng)求bookAction.action發(fā)生時(shí)(這個(gè)會(huì)在后面的Spring配置文件中見到的)Struts運(yùn)行時(shí)(Runtime)根據(jù)struts.xml里的Action映射集(Mapping),實(shí)例化com.sterning.books.web.actions.BookAction類,并調(diào)用其execute方法。當(dāng)然,我們可以通過以下兩種方法改變這種默認(rèn)調(diào)用。這個(gè)功能(Feature)有點(diǎn)類似Struts 1.x中的LookupDispathAction

          classes/sturts.xml中新建Action,并指明其調(diào)用的方法;

          訪問Action時(shí),在Action名后加上“!xxx”xxx為方法名)。

          2)、細(xì)心的朋友應(yīng)該可能會(huì)發(fā)現(xiàn)com.sterning.books.web.actions.BookAction.javaAction方法(execute)返回都是SUCCESS。這個(gè)屬性變量我并沒有定義,所以大家應(yīng)該會(huì)猜到它在ActionSupport或其父類中定義。沒錯(cuò),SUCCESS在接口com.opensymphony.xwork2.Action中定義,另外同時(shí)定義的還有ERROR, INPUT, LOGIN, NONE

          此外,我在配置Action時(shí)都沒有為result定義名字(name),所以它們默認(rèn)都為success。值得一提的是Struts 2.0中的result不僅僅是Struts 1.xforward的別名,它可以實(shí)現(xiàn)除forward外的很激動(dòng)人心的功能,如將Action輸出到FreeMaker模板、Velocity模板、JasperReports和使用XSL轉(zhuǎn)換等。這些都過result里的type(類型)屬性(Attribute)定義的。另外,您還可以自定義result類型。

          3)、使用Struts 2.0,表單數(shù)據(jù)的輸入將變得非常方便,和普通的POJO一樣在Action編寫GetterSetter,然后在JSPUI標(biāo)志的name與其對(duì)應(yīng),在提交表單到Action時(shí),我們就可以取得其值。

          4)、Struts 2.0更厲害的是支持更高級(jí)的POJO訪問,如this.getBook().getBookPrice()private Books book所引用的是一個(gè)關(guān)于書的對(duì)象類,它可以做為一個(gè)屬性而出現(xiàn)在BookActoin.java類中。這樣對(duì)我們開發(fā)多層系統(tǒng)尤其有用。它可以使系統(tǒng)結(jié)構(gòu)更清晰。

          5)、有朋友可能會(huì)這樣問:“如果我要取得Servlet API中的一些對(duì)象,如requestresponsesession等,應(yīng)該怎么做?這里的execute不像Struts 1.x的那樣在參數(shù)中引入。”開發(fā)Web應(yīng)用程序當(dāng)然免不了跟這些對(duì)象打交道。在Strutx 2.0中可以有兩種方式獲得這些對(duì)象:非IoC(控制反轉(zhuǎn)Inversion of Control)方式和IoC方式。

          非IoC方式

          要獲得上述對(duì)象,關(guān)鍵是Struts 2.0com.opensymphony.xwork2.ActionContext類。我們可以通過它的靜態(tài)方法getContext()獲取當(dāng)前Action的上下文對(duì)象。另外,org.apache.struts2.ServletActionContext作為輔助類(Helper Class),可以幫助您快捷地獲得這幾個(gè)對(duì)象。

          HttpServletRequest request = ServletActionContext.getRequest();

          HttpServletResponse response = ServletActionContext.getResponse();

          HttpSession session = request.getSession();

          如果你只是想訪問session的屬性(Attribute),你也可以通過ActionContext.getContext().getSession()獲取或添加session范圍(Scoped)的對(duì)象。

          IoC方式

          要使用IoC方式,我們首先要告訴IoC容器(Container)想取得某個(gè)對(duì)象的意愿,通過實(shí)現(xiàn)相應(yīng)的接口做到這點(diǎn)。如實(shí)現(xiàn)SessionAware, ServletRequestAware, ServletResponseAware接口,從而得到上面的對(duì)象

          1、對(duì)BookAction類的Save方法進(jìn)行驗(yàn)證

          正如《Writing Secure Code》文中所寫的名言All input is evil所有的輸入都是罪惡的,所以我們應(yīng)該對(duì)所有的外部輸入進(jìn)行校驗(yàn)。而表單是應(yīng)用程序最簡(jiǎn)單的入口,對(duì)其傳進(jìn)來的數(shù)據(jù),我們必須進(jìn)行校驗(yàn)。Struts2的校驗(yàn)框架十分簡(jiǎn)單方便,只在如下兩步:

          Xxx-validation.xml文件中的<message>元素中加入key屬性;

          在相應(yīng)的jsp文件中的<s:form>標(biāo)志中加入validate="true"屬性,就可以在用Javascript在客戶端校驗(yàn)數(shù)據(jù)。

          其驗(yàn)證文件為:BooksAction-save-validation.xml

          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">
          <validators>
              
          <!-- Field-Validator Syntax -->
              
          <field name="book.bookName">
                  
          <field-validator type="requiredstring">
                      
          <message key="book.bookName.required"/>
                  
          </field-validator>
              
          </field>
              
          <field name="book.bookAuthor">
                  
          <field-validator type="requiredstring">
                      
          <message key="book.bookAuthor.required"/>
                  
          </field-validator>
              
          </field>
              
          <field name="book.bookPublish">
                  
          <field-validator type="requiredstring">
                      
          <message key="book.bookPublish.required"/>
                  
          </field-validator>
              
          </field>
          </validators>
           com.sterning.books.web.actions.BooksAction-save-validation.xml

          1、對(duì)BookAction類的Save方法進(jìn)行驗(yàn)證的資源文件

                 注意配置文件的名字應(yīng)該是:配置文件(類名-validation.xml)的格式。BooksAction類的驗(yàn)證資源文件為:BooksAction.properties

          book=Books
          book.bookName.required=\u8bf7\u8f93\u5165\u4e66\u540d
          book.bookAuthor.required=\u8bf7\u8f93\u5165\u4f5c\u8005
          book.bookPublish.required=\u8bf7\u8f93\u5165\u51fa\u7248\u793e
          format.date={0,date,yyyy-MM-dd}

          com.sterning.books.web.actions.BooksAction.properties

                 資源文件的查找順序是有一定規(guī)則的。之所以說Struts 2.0的國際化更靈活是因?yàn)樗梢愿鶕?jù)不同需要配置和獲取資源(properties)文件。在Struts 2.0中有下面幾種方法:

          1)、使用全局的資源文件。這適用于遍布于整個(gè)應(yīng)用程序的國際化字符串,它們?cè)诓煌陌?/span>package)中被引用,如一些比較共用的出錯(cuò)提示;

          2)、使用包范圍內(nèi)的資源文件。做法是在包的根目錄下新建名的package.propertiespackage_xx_XX.properties文件。這就適用于在包中不同類訪問的資源;

          3)、使用Action范圍的資源文件。做法為Action的包下新建文件名(除文件擴(kuò)展名外)與Action類名同樣的資源文件。它只能在該Action中訪問。如此一來,我們就可以在不同的Action里使用相同的properties名表示不同的值。例如,在ActonOnetitle動(dòng)作一,而同樣用titleActionTwo表示動(dòng)作二,節(jié)省一些命名工夫;

          4)、使用<s:i18n>標(biāo)志訪問特定路徑的properties文件。在使用這一方法時(shí),請(qǐng)注意<s:i18n>標(biāo)志的范圍。在<s:i18n name="xxxxx"></s:i18n>之間,所有的國際化字符串都會(huì)在名為xxxxx資源文件查找,如果找不到,Struts 2.0就會(huì)輸出默認(rèn)值(國際化字符串的名字)。

          例如:某個(gè)ChildAction中調(diào)用了getText("user.title")Struts 2.0的將會(huì)執(zhí)行以下的操作:

          查找ChildAction_xx_XX.properties文件或ChildAction.properties

          查找ChildAction實(shí)現(xiàn)的接口,查找與接口同名的資源文件MyInterface.properties

          查找ChildAction的父類ParentActionproperties文件,文件名為ParentAction.properties

          判斷當(dāng)前ChildAction是否實(shí)現(xiàn)接口ModelDriven。如果是,調(diào)用getModel()獲得對(duì)象,查找與其同名的資源文件;

          查找當(dāng)前包下的package.properties文件;

          查找當(dāng)前包的父包,直到最頂層包;

          在值棧(Value Stack)中,查找名為user的屬性,轉(zhuǎn)到user類型同名的資源文件,查找鍵為title的資源;

          查找在struts.properties配置的默認(rèn)的資源文件,參考例1;

          輸出user.title


           未完待續(xù) 。。。。。。

          評(píng)論

          # re: Struts2+Spring2+Hibernate3 web應(yīng)用示例(四)  回復(fù)  更多評(píng)論   

          2007-10-01 11:35 by yk103
          頂一個(gè)~

          # re: Struts2+Spring2+Hibernate3 web應(yīng)用示例(四)  回復(fù)  更多評(píng)論   

          2007-10-08 15:19 by yhk
          辛苦了 寫的非常好

          # re: Struts2+Spring2+Hibernate3 web應(yīng)用示例(四)  回復(fù)  更多評(píng)論   

          2007-10-21 21:38 by apple0668
          想問一個(gè)問題:在struts2如果是一個(gè)注冊(cè)頁面,注冊(cè)提交是由于數(shù)據(jù)類型不一致,又返回到register.jsp,怎樣保存之前注冊(cè)是的信息?是用你所說的“非IoC(控制反轉(zhuǎn)Inversion of Control)方式和IoC方式”?有這方面的例子可以給一個(gè)學(xué)習(xí)學(xué)習(xí)?

          # re: Struts2+Spring2+Hibernate3 web應(yīng)用示例(四)  回復(fù)  更多評(píng)論   

          2008-10-09 14:51 by liyan2008_2009
          henhao

          # re: Struts2+Spring2+Hibernate3 web應(yīng)用示例(四)  回復(fù)  更多評(píng)論   

          2009-04-22 07:37 by 我是初學(xué)者
          我的email dqzj_000@126.com 能把整個(gè)jar 發(fā)給我嗎
          主站蜘蛛池模板: 浪卡子县| 东阳市| 上犹县| 平果县| 嘉黎县| 肃宁县| 简阳市| 龙井市| 枣强县| 玉环县| 浦县| 潞城市| 湘乡市| 眉山市| 和平县| 南皮县| 澄江县| 高淳县| 黄骅市| 南靖县| 琼海市| 南丰县| 太和县| 淳安县| 吴忠市| 云浮市| 微山县| 枞阳县| 阿克| 平顶山市| 永安市| 岗巴县| 岳阳市| 保德县| 常宁市| 潮安县| 富宁县| 铁岭县| 阳新县| 上杭县| 沾化县|