Struts2 addActionError時(shí) 使用action標(biāo)簽的注意問(wèn)題
struts.xml配置文件?????? < 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
@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
@Scope( " prototype " ) // 同上
public ? class ?Sxt_DaoHang_XianShiAction? extends ?ActionSupport?? implements ?Preparable{
sxt_DengLu_XianShi.ftl
??<@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"> </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
??<#list?listDaoHangLieBiao?as?nav>
??<li>
????${nav}
??</li>
??</#list>
</ul>
Sxt_DengLu_CaoZuoAction.java 執(zhí)行execute
?? public ?String?execute()? throws ?Exception?{
???? this .addActionError( " 系統(tǒng)錯(cuò)誤 " );
???? return ?INPUT;
??}
Sxt_DaoHang_XianShiAction.java 執(zhí)行execute
?? 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
@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í)候是這樣寫的
注意和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