Seasar DI Container with AOP

S2Strutsを使うと、S2とStrutsが簡単に連動するようになります。 Actionクラスにサービスコンポーネントが自動的に設定されるので、 Actionクラスの役割は適切なサービスコンポーネントを呼び出すだけになります。 Actionクラスはコントローラに徹することで、プレゼンテーション層とサービス層の役割分担も明確になります。 Actionに対してAOPを適用することもできます。

セットアップ

S2と同様にJDK1.4以上が必要です。サンプルは、 TomcatTomcat Pluginを使うことを前提にしています。 あらかじめインストールして置いてください。 S2StrutsVx.x.x.jarを解凍してできたs2strutsディレクトリをEclipseのJavaプロジェクトとして丸ごとインポートしてください。 .classpathを上書きするか聞かれるので、すべてはいのボタンをクリックして、すべてを取り込みます。 これで、私と全く同じ環境になります。サンプルはS2StrutsExamleVx.x.x.jarとして別途用意されているので、 ダウンロードして解凍してください。Eclipseでs2struts-exampleという名前でJavaプロジェクトを作成します。 Tomcatプロジェクトではないので気をつけてください。解凍してできたs2struts-exampleディレクトリを丸ごとインポートしてください。 .classpathを上書きするか聞かれるので、すべてはいのボタンをクリックして、すべてを取り込みます。 s2struts-exampleプロジェクトを右クリックしてプロパティ->Tomcatを選びます。Tomcatプロジェクトであるをチェックし、 アプリケーションURIを/s2struts-exampleとします。これで、Tomcatを再起動して、 ブラウザからhttp://localhost:8080/s2struts-example/にアクセスすると 足し算、引き算などのサンプルを見ることができます。

使い方

S2Containerを起動するためにS2ContainerServletをweb.xmlに登録します。

web.xml

<web-app>
  <display-name>Struts Application</display-name>
  <servlet>
    <servlet-name>s2container</servlet-name>
    <servlet-class>org.seasar.framework.container.servlet.S2ContainerServlet</servlet-class>
    <load-on-startup/>
  </servlet>
  
  <!-- Standard Action Servlet Configuration (with debugging) -->
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
      <param-name>debug</param-name>
      <param-value>2</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>2</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
  </servlet>

<servlet-mapping> <servlet-name>s2container</servlet-name> <url-pattern>/s2container</url-pattern> </servlet-mapping>

<!-- Standard Action Servlet Mapping -->
<servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>

<!-- The Usual Welcome File List -->
<welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list>

<!-- Struts Tag Library Descriptors -->
<taglib> <taglib-uri>/tags/struts-bean</taglib-uri> <taglib-location>/WEB-INF/struts-bean.tld</taglib-location> </taglib>

<taglib> <taglib-uri>/tags/struts-html</taglib-uri> <taglib-location>/WEB-INF/struts-html.tld</taglib-location> </taglib>

<taglib> <taglib-uri>/tags/struts-logic</taglib-uri> <taglib-location>/WEB-INF/struts-logic.tld</taglib-location> </taglib>

<taglib> <taglib-uri>/tags/struts-nested</taglib-uri> <taglib-location>/WEB-INF/struts-nested.tld</taglib-location> </taglib>

<taglib> <taglib-uri>/tags/struts-tiles</taglib-uri> <taglib-location>/WEB-INF/struts-tiles.tld</taglib-location> </taglib>

</web-app>

S2とStrutsを連動させるためには、S2用のRequestProcessorをstruts-config.xmlに登録する必要があります。 S2RequestProcessorとS2TilesRequestProcessorを用意しています。

struts-config.xml

<struts-config>
  ...
  <controller
processorClass="org.seasar.struts.S2RequestProcessor"/> ... </struts-config>

上記のRequestProcessorから生成される全てのActionがS2と連動するようになります。 S2と連動するActionを、明示的にコンポーネント定義に登録しておく必要はありません(Add.dicon)。 ただし、Actionに対して、AOPの適用や、 メソッド・インジェクションなどを行いたい場合には、 他の、コンポーネントと同様に、コンポーネント定義に登録しておく必要があります(Multiply.dicon)。 また、この定義ファイルは、アプリケーション全体の定義であるapp.diconに登録する必要があります。

Add.dicon

<components>
  <component class="examples.struts.AddServiceImpl"/>
</components>

Multiply.dicon

<components>
  <component class="examples.struts.MultiplyAction">
    <initMethod>#out.println("inited MultiplyAction")</initMethod>
  </component>
  <component class="examples.struts.MultiplyServiceImpl"/>
</components>

app.dicon

<components>
  <include path="examples/struts/Add.dicon"/>
  <include path="examples/struts/Subtract.dicon"/>
  <include path="examples/struts/Multiply.dicon"/>
</components>

サービスコンポーネントを受け取るためにActionクラスはコンストラクタもしくはセッターメソッドを定義しておきます。 execute()では処理をサービスに委譲しているので非常にすっきりしたコードになっていることが分かると思います。

AddAction

package examples.struts;

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

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class AddAction extends Action {

    private AddService addService_;

    public AddAction(AddService addService) {
        addService_ = addService;
    }

    public ActionForward execute(
        ActionMapping mapping,
        ActionForm form,
        HttpServletRequest request,
        HttpServletResponse response) {

        CalculationForm addForm = (CalculationForm) form;
        int result = addService_.add(addForm.getArg1(), addForm.getArg2());
        addForm.setResult(result);
        return (mapping.findForward("success"));
    }
}

ProxyActionクラスの使い方

また、ProxyActionクラスを使用して、コンポーネント定義ファイルに記述したActionを呼び出し、処理を委譲する事もできます。 これにより、struts-configとコンポーネント定義ファイルに同じクラス名を定義/同期する必要がなくなります。 struts-configのactionのpath属性と、コンポーネント定義ファイルに定義したcomponentのname属性が一致している必要があります。

struts-config.xml

<struts-config>
  ...
  <action-mappings>
    ...
    <action
      path="/subtract"
      type="org.seasar.struts.ProxyAction"
      name="calcForm"
      scope="request"
      validate="false"
      input="/pages/subtractInput.jsp">
      <forward name="success" path="/pages/result.jsp" />
    </action>
    ...
  <action-mappings>
  ...
</struts-config>

Subtract.dicon

<components>
  <component name="/subtract" class="examples.struts.SubtractAction" instance="prototype"/>
  <component class="examples.struts.SubtractServiceImpl"/>
</components>
(インスタンス属性は、任意です。この引き算のサンプルでは、足し算、掛け算のサンプルとの比較のために、prototypeを指定しています。 singletonならば、明示的な指定の必要はないでしょう。)