S2Strutsを使うと、S2とStrutsが簡単に連動するようになります。
Actionクラスにサービスコンポーネントが自動的に設定されるので、
Actionクラスの役割は適切なサービスコンポーネントを呼び出すだけになります。
Actionクラスはコントローラに徹することで、プレゼンテーション層とサービス層の役割分担も明確になります。
また、Actionに対してAOPを適用することもできます。
S2と同様にJDK1.4以上が必要です。
S2StrutsVx.x.x.jarを解凍してできたs2strutsディレクトリをEclipseのJavaプロジェクトとして丸ごとインポートしてください。
.classpathを上書きするか聞かれるので、全て、はいのボタンをクリックして、全てを取り込みます。
これで、私と全く同じ開発環境になります。
サンプルは、
Tomcatと
Tomcat Pluginを使うことを前提にしています。
あらかじめインストールして置いてください。
サンプルは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を起動するためにS2StrutsServletをweb.xmlに登録する必要があります。
web.xml
<web-app>
<display-name>Struts Application</display-name>
<servlet>
<servlet-name>s2container</servlet-name>
<servlet-class>org.seasar.struts.S2StrutsServlet</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の、RequestProcessorとTilesRequestProcessorに相当します。
struts-config.xml
<struts-config>
...
<controller processorClass="org.seasar.struts.S2RequestProcessor"/>
...
</struts-config>
上記のRequestProcessorから生成される全てのActionクラスがS2と連動するようになります。
S2と連動するActionを、明示的にコンポーネント定義に登録しておく必要はありません(Add.dicon)。
インターフェースに対するセッターメソッドを定義しておけば、
自動的にセッター・インジェクションが行われます。
また、インターフェースのみが引数の、コンストラクタを定義している場合は、
自動的に、コンストラクタ・インジェクションが行われます。
Add.dicon
<components>
<component class="org.seasar.struts.examples.AddServiceImpl"/>
</components>
ただし、Actionに対して、AOPの適用や、
メソッド・インジェクションを行いたい場合、
また、コンポーネントの依存関係が、型によって自動的に解決されない場合などには、
他のコンポーネントと同様に、コンポーネント定義にActionクラスを登録しておく必要があります(Multiply.dicon)。
Multiply.dicon
<components>
<component class="org.seasar.struts.examples.MultiplyAction" instance="prototype">
<initMethod>#out.println("inited MultiplyAction")</initMethod>
<aspect pointcut="execute">
<component class="org.seasar.framework.aop.interceptors.TraceInterceptor">
</aspect>
</component>
<component class="org.seasar.struts.examples.MultiplyServiceImpl"/>
</components>
インスタンス属性は、任意です。このコンポーネント定義ファイルでは、では、他の演算サンプルとの比較のために、prototypeを指定しています。
singletonとするのであれば、明示的な指定の必要はありません。instance属性に、prototypeを指定をしていすると、
Actionクラスを、実装する時に、インスタンスフィールドを使用できるという利点があります。
また、これらの定義ファイルは、アプリケーション全体の定義であるapp.diconに登録する必要があります。
app.dicon
<components>
<include path="org/seasar/struts/examples/Add.dicon"/>
<include path="org/seasar/struts/examples/Subtract.dicon"/>
<include path="org/seasar/struts/examples/Multiply.dicon"/>
<include path="org/seasar/struts/examples/Divide.dicon"/>
</components>
サービスコンポーネントを受け取るためにActionクラスはコンストラクタもしくはセッターメソッドを定義しておきます。
execute()メソッドでは、サービスに処理を委譲するのみとなるので、非常にすっきりしたコードになっていることが分かると思います。
AddAction
package org.seasar.struts.examples;
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 calForm = (CalculationForm) form;
int result = addService_.add(calForm.getArg1(), calForm.getArg2());
calForm.setResult(result);
return (mapping.findForward("success"));
}
}
また、ProxyActionクラスを使用して、コンポーネント定義ファイルに記述したActionを呼び出し、処理を委譲する事もできます。
これにより、struts-configとコンポーネント定義ファイルに同じクラス名を定義/同期する必要がなくなります。
struts-config内に記述しているactionのpath属性と、コンポーネント定義ファイルに定義したcomponentのname属性が一致している必要があります。
詳細は、「path属性とActionクラスのマッピングについて」を参照してください。
また、ProxyActionクラスのみに関しては、struts-configに、S2用のRequestProcessorを登録しなくとも、S2と連動する事ができます。
ただし、他のActionクラスもS2と連動させたい場合には、前述のように、S2用のRequestProcessorをstruts-config.xmlに登録する必要があります。
これは、特定のActionクラスのみを、S2と連動する為に有効である事を意味します。
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="org.seasar.struts.examples.SubtractAction"/>
<component class="org.seasar.struts.examples.SubtractServiceImpl"/>
</components>
ProxyActionクラスの使い方と同様に、struts-config内に記述しているactionのpath属性と、
コンポーネント定義ファイルに定義したcomponentのname属性を一致させる事によりtype属性を記述せずにActionクラスを呼び出す事が出来ます。
詳細は、「path属性とActionクラスのマッピングについて」を参照してください。
この手法を利用する為には条件があります。それは、type属性、forward属性、include属性の3つの属性が無い事です。
これにより、struts-configとコンポーネント定義ファイルに同じクラス名を定義/同期する必要がなくなります。
struts-config.xml
<struts-config>
...
<action-mappings>
...
<action
path="/divide"
name="calcForm"
scope="request"
validate="false"
input="/pages/divideInput.jsp">
<forward name="success" path="/pages/result.jsp" />
</action>
...
<action-mappings>
...
</struts-config>
Divide.dicon
<components>
<component name="/divide" class="org.seasar.struts.examples.DivideAction"/>
<component class="org.seasar.struts.examples.DivideServiceImpl"/>
</components>
ここでは、前述の「ProxyActionクラスの使い方」と、
「struts-config内に記述しているactionの、type属性を記述せずにActionクラスを指定する」で行った、
path属性とActionクラスのマッピングについての、詳細情報を記述します。
以下の、web.xml、struts-config、そして、コンポーネント定義ファイルが存在するとします。
web.xml
<web-app>
...
<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>config/foo</param-name>
<param-value>/WEB-INF/struts-config-foo.xml</param-value>
</init-param>
...
</servlet>
...
</web-app>
struts-config.xml
<struts-config>
...
<action-mappings>
...
<action path="/bar"
...
</action>
...
<action-mappings>
...
</struts-config>
struts-config-foo.xml
<struts-config>
...
<action-mappings>
...
<action path="/baz"
...
</action>
...
<action-mappings>
...
</struts-config>
コンポーネント定義ファイル
<components>
<component name="/bar" class="BarAction"/>
<component name="/foo/baz" class="BazAction"/>
</components>
デフォルトのモジュール(struts-config.xmlで動作するモジュール)であれば、struts-config内に記述しているactionタグのpath属性と、
コンポーネント定義ファイル内に記述しているcomponentタグのname属性でマッピングを行います。下記のサブモジュールの(2)のパターンになります。
サブモジュール(この例では、struts-config-foo.xmlで動作するモジュール)であれば、マッピング方法が2つ存在します。
(1)モジュール(prefix)名(/foo)+path属性(/baz)=name属性(/foo/baz)
(2)path属性(/baz)=name属性(/foo/baz)
コンポーネントを取得する優先順位は、(1)、(2)の順になります。つまり、名前が/foo/bazであるコンポーネントが見つからなければ、名前が/bazであるコンポーネントを取得します。
|