硬盤的BLOG

          引導(dǎo)分區(qū)

          Struts2 addActionError時(shí) 使用action標(biāo)簽的注意問(wèn)題

          struts.xml配置文件
          ???? < action? name ="*_*_*" ?class ="{0}Action" >
          ??????
          < result? name ="input" ?type ="freemarker" > /WEB-INF/template/strong/{1}/{1}_{2}_XianShi.ftl </ result >
          ??????
          < result? name ="success" ?type ="freemarker" > /WEB-INF/template/strong/{1}/{0}.ftl </ result >
          ????
          </ action >


          登錄操作Action 對(duì)應(yīng)Action名為sxt_DengLu_CaoZuo 對(duì)應(yīng)的input result為/WEB-INF/template/strong/sxt/sxt_DengLu_XianShi.ftl

          Sxt_DengLu_CaoZuoAction.java
          @Component( " sxt_DengLu_CaoZuoAction " ) // 使用了Spring?這里的意思是將本Action裝配為一個(gè)ID為sxt_DengLu_CaoZuoAction的Bean
          @Scope( " prototype " ) // 作用域?yàn)閜rototype?即每次都使用新的對(duì)象
          public ? class ?Sxt_DengLu_CaoZuoAction? extends ?ActionSupport? implements ??ModelDriven < MYh > ?{


          導(dǎo)航列表Action 對(duì)應(yīng)Action名為sxt_DaoHang_XianShi 對(duì)應(yīng)的success result為/WEB-INF/template/strong/sxt/sxt_DaoHang_XianShi.ftl

          Sxt_DaoHang_XianShiAction.java
          @Component( " sxt_DaoHang_XianShiAction " ) // ?同上
          @Scope( " prototype " ) // 同上
          public ? class ?Sxt_DaoHang_XianShiAction? extends ?ActionSupport?? implements ?Preparable{


          sxt_DengLu_XianShi.ftl
          ??//?使用action標(biāo)簽將導(dǎo)航列表導(dǎo)入到本頁(yè)面
          ??<@s.action?name="sxt_DaoHang_XianShi"?namespace="/ht"?executeResult=true?ignoreContextParams=true?/>
          ??
          <@s.form?action="sxt_DengLu_CaoZuo"?id="form_login"?theme="simple">
          ????
          <table?align="CENTER"?id="table_login">
          ??????
          <tr>
          ????????
          <td?class="td_label">登錄名</td>
          ????????
          <td?class="td_input"><@s.textfield?label="${action.getText('TYH_YHDLM')}"?id="yhDlm"?name="yhDlm"?/></td>
          ??????
          </tr>
          ??????
          <tr>
          ????????
          <td?class="td_label">密碼</td>
          ????????
          <td?class="td_input"><@s.password?label="${action.getText('TYH_YHMM')}"?id="yhMm"?name="yhMm"?/></td>
          ??????
          </tr>
          ??????
          <tr>
          ????????
          <td?class="td_label">&nbsp;</td>
          ????????
          <td?class="td_input"><p?id="p_submit">
          ??????????
          <@s.submit?value="${action.getText('PAGE_DENG_LU')}"?/>
          ??????????
          <input?type="button"?value="${action.getText('PAGE_TUI_CHU')}">
          ????????
          </p></td>
          ??????
          </tr>
          ????
          </table>
          ??
          </@s.form>


          sxt_DaoHang_XianShi.ftl
          <ul>
          ??
          <#list?listDaoHangLieBiao?as?nav>
          ??
          <li>
          ????${nav}
          ??
          </li>
          ??
          </#list>
          </ul>


          Sxt_DengLu_CaoZuoAction.java 執(zhí)行execute
          ??@Override
          ??
          public ?String?execute()? throws ?Exception?{
          ????
          this .addActionError( " 系統(tǒng)錯(cuò)誤 " );
          ????
          return ?INPUT;
          ??}


          Sxt_DaoHang_XianShiAction.java 執(zhí)行execute
          ??@Override
          ??
          public ?String?execute()? throws ?Exception?{
          ????ActionContext?ctx?
          = ?ActionContext.getContext();
          ????ctx.put(
          " listDaoHangLieBiao " ,? new ?ArrayList());
          ????
          return ?SUCCESS;
          ??}

          這個(gè)時(shí)候執(zhí)行代碼 freemarker會(huì)提示“<pre>Expression listDaoHangLieBiao is undefined</pre>”為什么會(huì)這樣了?
          仔細(xì)查找了相關(guān)資料 發(fā)現(xiàn)apache的文檔 http://struts.apache.org/2.x/docs/how-do-we-repopulate-controls-when-validation-fails.html中寫道

          When validation fails, we typically forward back to the same server page, so that the errors can be presented, and so that the client can fix the problem and resubmit the form. Of course, aside from the errors, we may also need to present rich controls, like drop down lists.

          If we try to populate rich controls in an Action method, like input or execute, and validation fails, the method will not be invoked, and the controls are not populated. Two alternative ways to populate controls are the Preparable interface and the action tag.

          大致的意思是但出現(xiàn)validation錯(cuò)誤的時(shí)候會(huì)影響到Action的正常執(zhí)行,這個(gè)時(shí)候應(yīng)該實(shí)現(xiàn)Preparable接口中的prepare()方法,這個(gè)方法中的操作不會(huì)因?yàn)関alidation錯(cuò)誤而不執(zhí)行。

          聯(lián)想到上面的錯(cuò),會(huì)不會(huì)也是因?yàn)閍ddActionError而導(dǎo)致不能正常使用action標(biāo)簽了。為此在Sxt_DaoHang_XianShiAction中實(shí)現(xiàn)Preparable接口并在prepare()方法中put listDaoHangLieBiao。修改代碼如下

          Sxt_DaoHang_XianShiAction.java

          @Component( " sxt_DaoHang_XianShiAction " )
          @Scope(
          " prototype " )
          public ? class ?Sxt_DaoHang_XianShiAction? extends ?ActionSupport?? implements ?Preparable{

          ??@Override
          ??
          public ?String?execute()? throws ?Exception?{
          ????
          return ?SUCCESS;
          ??}

          ??
          public ? void ?prepare()? throws ?Exception?{
          ????ActionContext?ctx?
          = ?ActionContext.getContext();
          ????ctx.put(
          " listDaoHangLieBiao " ,?Constants.DAO_HANG_LIE_BIAO);
          ??}
          }

          再執(zhí)行==成功。

          總結(jié) Struts2目前的資料相對(duì)Struts1來(lái)說(shuō)是非常少的,尤其是研究的很深的資料,看來(lái)現(xiàn)在想學(xué)好Struts2還必須從Apache的原始資料中尋找。

          另外上面使用action標(biāo)簽的時(shí)候是這樣寫的

          < @s.action?name = " sxt_DaoHang_XianShi " ?namespace = " /ht " ?executeResult = true ?ignoreContextParams = true ? />

          注意和Struts2的標(biāo)簽寫法略有不同,因?yàn)檫@里使用了Freemarker做模板,所以使用的freemarker的寫法,特別的是executeResult=true?ignoreContextParams=true而按照Struts2的標(biāo)簽應(yīng)該是executeResult="true" ignoreContextParams="true"

          哈哈 小小兩個(gè)引號(hào) 害我折騰N個(gè)小時(shí)

          posted on 2008-07-16 06:43 我是一塊硬盤 閱讀(4281) 評(píng)論(1)  編輯  收藏 所屬分類: Struts2

          評(píng)論

          # re: Struts2 addActionError時(shí) 使用action標(biāo)簽的注意問(wèn)題 2009-03-10 14:05 李明旸

          希望以后可以一起研究·我要多多學(xué)習(xí)·嘿嘿··我剛工作不久·還不夠成熟··希望能夠和您多學(xué)習(xí)···Q515352000
            回復(fù)  更多評(píng)論   


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 河北区| 都兰县| 廉江市| 宁都县| 安化县| 崇阳县| 新营市| 四子王旗| 中宁县| 寿光市| 敦煌市| 车致| 濉溪县| 四会市| 嵊州市| 邵阳县| 东平县| 永城市| 泰和县| 多伦县| 尚志市| 肇东市| 繁峙县| 堆龙德庆县| 缙云县| 固安县| 广灵县| 景谷| 阿坝县| 柳河县| 兰溪市| 自治县| 南通市| 德钦县| 七台河市| 鄢陵县| 新疆| 嘉鱼县| 基隆市| 永德县| 小金县|