The NoteBook of EricKong

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            611 Posts :: 1 Stories :: 190 Comments :: 0 Trackbacks

          常用鏈接

          留言簿(11)

          我參與的團隊

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          比如,當我要添加一個信用卡的時候,我需要信用卡的CardType, 這些數據是存在數據庫中的,我要先通過action 的一個 create方法,取到信用卡的類型:

          public String create(){
                  creditCardTypeList = this.creditCardTypeService.getList();
                 
                  
          return SUCCESS;
          }
           

          Struts的配置文件:

          <action name="create" method="create" class="example.CreditCardAction">     <result name="success">input.jsp</result>
          </action>

          input.jsp

          ...
          <
          s:select name="creditCard.creditCardTypeId" list="creditCardTypeList" listKey="creditCardTypeId" 
                          listValue
          ="ccType" />
          ....

          當提交input.jsp 的時候,Validate 檢查沒有通過,這時我需要回到input.jsp,此時應該下拉列表框的CreditType應該被保留,只需要在example.CreditCardAction 實現 Preparable接口,并實現prepare 方法,然后在add的 action中加上

          <interceptor-ref name="prepare"/>
          <interceptor-ref name="defaultStack"/>
          就可以了
          prepare方法:
          public void prepare(){
              creditCardTypeList = this.creditCardTypeService.getList(); 
          }

          Add Acton:
          <action name="add" method="add" class="example.CreditCardAction">
              
          <interceptor-ref name="prepare"/>
              
          <interceptor-ref name="defaultStack"/>
              
          <result name="input">input.jsp</result>            
              
          <result name="success" type="redirect-action">
                      
          <param name="namespace">/credit</param>
                  
          <param name="actionName">list</param>
              
          </result>
          </action>

            這樣,在驗證前將首先調用 prepare方法,即使失敗了回到input.jsp頁面creditCardType選擇框的值仍然存在。

          <interceptor-ref name="defaultStack"/> 中的 defaultStack 是我們在struts.xml 中配置的,其中我們注釋掉了 <interceptor-ref name="prepare"/> 這樣在example.CreditCardAction中的其他Action就不會首先執行prepare方法,只有加上了 <interceptor-ref name="prepare"/> 的才會去首先執行 prepare方法。

          <package name="project-default" abstract="true" extends="struts-default">
                   
          <interceptors>
                   
          <interceptor-stack name="defaultStack">
                          
          <interceptor-ref name="exception"/>
                          
          <interceptor-ref name="alias"/>
                          
          <interceptor-ref name="servletConfig"/>
                          
          <!--                           
                          <interceptor-ref name="prepare"/>
                           
          -->                              
                          
          <interceptor-ref name="i18n"/>
                          
          <interceptor-ref name="chain"/>
                          
          <interceptor-ref name="debugging"/>
                          
          <interceptor-ref name="profiling"/>
                          
          <interceptor-ref name="scopedModelDriven"/>
                          
          <interceptor-ref name="modelDriven"/>
                          
          <interceptor-ref name="fileUpload"/>
                          
          <!-- 
                          <interceptor-ref name="checkbox">  
                                 <param name="uncheckedValue">no</param>  
                          </interceptor-ref>
                          
          -->
                          
          <interceptor-ref name="staticParams"/>
                          
          <interceptor-ref name="params">
                            
          <param name="excludeParams">dojo..*</param>
                          
          </interceptor-ref>
                          
          <interceptor-ref name="conversionError"/>
                          
          <interceptor-ref name="validation">
                              
          <param name="excludeMethods">input,back,cancel,browse</param>
                          
          </interceptor-ref>
                          
          <interceptor-ref name="workflow">
                              
          <param name="excludeMethods">input,back,cancel,browse</param>
                          
          </interceptor-ref>
                      
          </interceptor-stack>
                 
          </interceptors>
              
          </package> 
          posted on 2010-08-03 21:56 Eric_jiang 閱讀(730) 評論(0)  編輯  收藏 所屬分類: struts2
          主站蜘蛛池模板: 象州县| 霍山县| 县级市| 礼泉县| 枣阳市| 陇川县| 辽阳市| 抚顺县| 婺源县| 延川县| 子洲县| 顺义区| 历史| 清苑县| 扬州市| 商都县| 扬中市| 萝北县| 本溪| 萨嘎县| 招远市| 财经| 武山县| 雅江县| 波密县| 中牟县| 上犹县| 淮安市| 嘉黎县| 绥滨县| 都江堰市| 汾西县| 广州市| 三门县| 太康县| 赤城县| 迁安市| 鄂托克旗| 开化县| 瑞昌市| 桑日县|