Struts解決ActionForm、Action過多問題

來源:互聯網
上載者:User

1、解決Action過多問題

通過DispatchAction的方式建立一個繼承自DispatchAction的Action此類Action允許我們在表單中帶有參數,根據參數執行Action中的不同方法,以此實現Action的多用

步驟

① 建立一個繼承自DispatchAction的Action規定方法名稱

② 在表單頁面中添加一個隱藏欄位值,假設為codi value=”insert”

③ 在Struts-config.xml的相應Form中action項目中設定parameter參數值為codi

那麼當表單提交的時候,Action中的insert()方法就會被執行,可以通過改隱藏欄位中的值的方式來改變Action要執行的方法

2、解決ActionForm過多的問題

通過動態ActionForm的方式,即DynaActionForm

步驟

① 在Struts-config.xml

<form-beans>
<form-bean name=”abcForm” type=”org.apache.struts.action.DynaActionForm”>
<form-property name=”userid” type=”java.long.String”></form-property>
</form-bean>
</form-beans>

② 在Struts-config.xml

更改相應的action中的attribute和name為abcForm

這樣就配置好了一個動態ActionForm

3、解決動態ActionForm的驗證問題

通過動態驗證ActonForm的方式,即DynaValidatorForm

步驟

① 在Struts-config.xml

同2的第一步,但是type需要改成org.apache.struts.action.DynaValidatorForm

② 配置一個validation.xml檔案

<form-validation>
<formset>
<form name="abcForm">
<field property="userid" depends="required">
<arg key="err.userid" resource="true"/>
</field>
</form>
</formset>
</form-validation>

③ 在Struts資源檔中配置err.userid、以及errors.required

④ 在Struts-config.xml配置相應的action,添加validate=”true”添加驗證支援

⑤ 添加struts Plugin外掛程式,設定plugin class為org.apache.struts.validator.ValidatorPlugIn

添加propertys

Property->pathnames

Value->/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml

這樣一個驗證架構就配置好了!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.