Struts DispatchActionの例

Struts DispatchActionの例

DispatchActionクラス(org.apache.struts.actions.DispatchAction)は、関連するすべての関数を単一のアクションクラスにグループ化する方法を提供します。 これは、関数ごとに別々のアクションクラスを作成しないようにするための便利なメカニズムです。

このStrutsDispatchActionの例をダウンロード–Struts-DispatchAction-Example.zip

このメカニズムを実装するには、アクションクラスがorg.apache.struts.actions.DispatchActionクラスを拡張する必要があります。このアクションクラスは、通常のアクションクラスのようにexecute()メソッドを実装する必要はありません。 代わりに、DispatchActionクラスは、着信要求パラメーターmethodに基づいてメソッドを実行します。 たとえば、パラメータが「method = chinese」の場合、chinese()メソッドが実行されます。

アクションクラスはDispatchActionを拡張し、ローカライズ用のStrutsセッション属性にロケールを設定する4つのメソッドを含みます。

public class LanguageSelectAction extends DispatchAction{

    public ActionForward chinese(ActionMapping mapping,ActionForm form,
        HttpServletRequest request,HttpServletResponse response)
    throws Exception {

        request.getSession().setAttribute(
                Globals.LOCALE_KEY, Locale.SIMPLIFIED_CHINESE);

        return mapping.findForward("success");
    }

    public ActionForward english(ActionMapping mapping,ActionForm form,
        HttpServletRequest request,HttpServletResponse response)
    throws Exception {

        request.getSession().setAttribute(
                Globals.LOCALE_KEY, Locale.ENGLISH);

        return mapping.findForward("success");
    }

    public ActionForward german(ActionMapping mapping,ActionForm form,
        HttpServletRequest request,HttpServletResponse response)
    throws Exception {

        request.getSession().setAttribute(
                Globals.LOCALE_KEY, Locale.GERMAN);

        return mapping.findForward("success");
    }

    public ActionForward france(ActionMapping mapping,ActionForm form,
        HttpServletRequest request,HttpServletResponse response)
    throws Exception {

        request.getSession().setAttribute(
                Globals.LOCALE_KEY, Locale.FRANCE);

        return mapping.findForward("success");
    }

}

このStruts htmlタグはchinese()メソッドを実行します。


このStruts htmlタグはenglish()メソッドを実行します。


このStruts htmlタグはgerman()メソッドを実行します。


このStruts htmlタグはfrance()メソッドを実行します。