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 瀵瑰簲Action鍚嶄負sxt_DengLu_CaoZuo 瀵瑰簲鐨?span style="color: rgb(0, 0, 0);">input result涓?span style="color: rgb(0, 0, 0);">/WEB-INF/template/strong/sxt/sxt_DengLu_XianShi.ftl
Sxt_DengLu_CaoZuoAction.java
@Component(
"
sxt_DengLu_CaoZuoAction
"
)
//
浣跨敤浜哠pring聽榪欓噷鐨勬剰鎬濇槸灝嗘湰Action瑁呴厤涓轟竴涓狪D涓簊xt_DengLu_CaoZuoAction鐨凚ean
@Scope(
"
prototype
"
)
//
浣滅敤鍩熶負prototype聽鍗蟲瘡嬈¢兘浣跨敤鏂扮殑瀵硅薄
public
聽
class
聽Sxt_DengLu_CaoZuoAction聽
extends
聽ActionSupport聽
implements
聽聽ModelDriven
<
MYh
>
聽{
瀵艱埅鍒楄〃Action 瀵瑰簲Action鍚嶄負sxt_DaoHang_XianShi 瀵瑰簲鐨?span style="color: rgb(0, 0, 0);">success result涓?span style="color: rgb(0, 0, 0);">/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鏍囩灝嗗鑸垪琛ㄥ鍏ュ埌鏈〉闈?/span>
聽聽<@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">鐧誨綍鍚?/span></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
<ul>
聽聽<#list聽listDaoHangLieBiao聽as聽nav>
聽聽<li>
聽聽聽聽${nav}
聽聽</li>
聽聽</#list>
</ul>
Sxt_DengLu_CaoZuoAction.java 鎵цexecute
聽聽@Override
聽聽
public
聽String聽execute()聽
throws
聽Exception聽{
聽聽聽聽
this
.addActionError(
"
緋葷粺閿欒
"
);
聽聽聽聽
return
聽INPUT;
聽聽}
Sxt_DaoHang_XianShiAction.java 鎵цexecute
聽聽@Override
聽聽
public
聽String聽execute()聽
throws
聽Exception聽{
聽聽聽聽ActionContext聽ctx聽
=
聽ActionContext.getContext();
聽聽聽聽ctx.put(
"
listDaoHangLieBiao
"
,聽
new
聽ArrayList());
聽聽聽聽
return
聽SUCCESS;
聽聽}
榪欎釜鏃跺欐墽琛屼唬鐮?freemarker浼氭彁紺衡?lt;pre>Expression listDaoHangLieBiao is undefined</pre>鈥濅負浠涔堜細榪欐牱浜嗭紵
浠旂粏鏌ユ壘浜嗙浉鍏寵祫鏂?鍙戠幇apache鐨勬枃妗?http://struts.apache.org/2.x/docs/how-do-we-repopulate-controls-when-validation-fails.html涓啓閬?br />
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.
澶ц嚧鐨勬剰鎬濇槸浣嗗嚭鐜皏alidation閿欒鐨勬椂鍊欎細褰卞搷鍒癆ction鐨勬甯告墽琛岋紝榪欎釜鏃跺欏簲璇ュ疄鐜?tt>Preparable鎺ュ彛涓殑prepare()鏂規硶錛岃繖涓柟娉曚腑鐨勬搷浣滀笉浼氬洜涓簐alidation閿欒鑰屼笉鎵ц銆?/font>
鑱旀兂鍒頒笂闈㈢殑閿欙紝浼氫笉浼氫篃鏄洜涓篴ddActionError鑰屽鑷翠笉鑳芥甯鎬嬌鐢╝ction鏍囩浜嗐備負姝ゅ湪Sxt_DaoHang_XianShiAction涓疄鐜癙reparable鎺ュ彛騫跺湪prepare()鏂規硶涓璸ut listDaoHangLieBiao銆備慨鏀逛唬鐮佸涓?/span>
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);
聽聽}
}
鍐嶆墽琛?=鎴愬姛銆?/font>
鎬葷粨 Struts2鐩墠鐨勮祫鏂欑浉瀵筍truts1鏉ヨ鏄潪甯稿皯鐨勶紝灝ゅ叾鏄爺絀剁殑寰堟繁鐨勮祫鏂欙紝鐪嬫潵鐜板湪鎯沖濂絊truts2榪樺繀欏諱粠Apache鐨勫師濮嬭祫鏂欎腑瀵繪壘銆?/font>
鍙﹀涓婇潰浣跨敤action鏍囩鐨勬椂鍊欐槸榪欐牱鍐欑殑
<
@s.action聽name
=
"
sxt_DaoHang_XianShi
"
聽namespace
=
"
/ht
"
聽executeResult
=
true
聽ignoreContextParams
=
true
聽
/>
娉ㄦ剰鍜孲truts2鐨勬爣絳懼啓娉曠暐鏈変笉鍚岋紝鍥犱負榪欓噷浣跨敤浜咶reemarker鍋氭ā鏉匡紝鎵浠ヤ嬌鐢ㄧ殑freemarker鐨勫啓娉曪紝鐗瑰埆鐨勬槸executeResult=true聽ignoreContextParams=true鑰屾寜鐓truts2鐨勬爣絳懼簲璇ユ槸executeResult="true" ignoreContextParams="true"
鍝堝搱 灝忓皬涓や釜寮曞彿 瀹蟲垜鎶樿吘N涓皬鏃?br />

]]>