開発者ドキュメント

Struts – MappingDispatchActionの例

Struts MappingDispatchActionクラスは、類似の機能を単一のアクションクラスにグループ化し、対応するActionMappingのパラメータ属性に応じて関数を実行するために使用されます。

次に、MappingDispatchActionの使用例を示します。

この例をダウンロードする –

Struts-MappingDispatchAction-Example.zip

1. MappingDispatchActionクラス

MappingDispatchActionクラスを拡張し、

generateXML()



generateExcel()

の2つのメソッドを宣言します。

package com.mkyong.common.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

public class MyCustomDispatchAction extends MappingDispatchAction{

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

        request.setAttribute("method", "generateXML is called");

            return mapping.findForward("success");
    }

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

        request.setAttribute("method", "generateExcel is called");

        return mapping.findForward("success");
    }
}

2.ストラットの構成

2つのアクションマッピングを宣言し、それぞれ異なるMyCustomDispatchActionクラスを指します。

  • struts-config.xml **

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://jakarta.apache.org/struts/dtds/struts-config__1__3.dtd">

<struts-config>

    <action-mappings>

        <action
            path="/CustomDispatchActionXML"
            type="com.mkyong.common.action.MyCustomDispatchAction"
            parameter="generateXML"
            >

            <forward name="success" path="/pages/DispatchExample.jsp"/>

        </action>

        <action
            path="/CustomDispatchActionExcel"
            type="com.mkyong.common.action.MyCustomDispatchAction"
            parameter="generateExcel"
            >

            <forward name="success" path="/pages/DispatchExample.jsp"/>

        </action>


        <action
            path="/Test"
            type="org.apache.struts.actions.ForwardAction"
            parameter="/pages/TestForm.jsp"
            >
        </action>

    </action-mappings>

</struts-config>

3.ビューページ

JSPページでは、リンクは次のように動作します。

{空} 1。

/CustomDispatchActionXML

はgenerateXML()

メソッドを実行します。

/CustomDispatchActionExcel



generateExcel()** メソッドを実行します。

  • TestForm.jsp **

Struts - DispatchAction Example

html:link


   |



a href


           Generate XML File

   |

           Generate Excel File
  • DispatchExample.jsp **

Struts - DispatchAction Example

4.テストする





XMLファイルの生成

“リンクをクリックすると、


http://localhost:8080/StrutsExample/CustomDispatchActionXML.do


に転送されます。





Excelファイルを生成

“リンクをクリックすると、


http://localhost:8080/StrutsExample/CustomDispatchActionExcel.do


に転送されます。



モバイルバージョンを終了