如果A網頁進入Action時可能會有三個動作:新增資料、刪除資料或到查詢頁,
在進入一般的Action(org.apache.struts.action.Action)中會進入execute方法,
然後寫一些if-else去判斷如何執行這三個動作...在程式的可讀性很差。
而使用DispatchAction後,就可以自行定義三個方法當做進入點,而不必再使用execute,
設定方式如下:
1.在Struts-Config.xml中設定action tag的parameter="method"
2. 在jsp中設定一個hidden參數為"method"對應上一步的設定,並設定goSearchPage方法準備讓網頁移動到查詢頁,
3.讓Action繼承DispatchAction並設定goSearchPage方法,對應JSP中的form.method.value
public class CSDP01 extends DispatchAction {
/**
* 移動至查詢頁
*/
public ActionForward goSearchPage(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception
{ return (mapping.findForward("search"));
}
}
2008年5月5日 星期一
2008年4月28日 星期一
訂閱:
文章 (Atom)