StrutsとSpringを連携させる
Struts2はSpringに簡単にインスタンス管理をお任せすることができます。
注意すべきはActionのインスタンスモード。Struts2のアクションはステートを持つので、prototypeかrequestにしないと洒落になりません。
ライブラリの取得
必要なライブラリは、struts2-spring-plugin-2.0.6.jar。Maven2のPOMは、以下のように設定します。
<dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> <version>2.0.6</version> </dependency>
web.xmlにContextLoaderListener設定
web.xmlには、SpringのContextLoaderListenerと、読み込むapplicationContextファイルを設定します。
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>
Struts.xmlの設定
struts.xmlで、Actionインスタンスの生成をSpringに任せる設定を追記します。
<constant name="struts.objectFactory" value="spring" />
struts.xmlのActionのclass属性をSpringのbean名を指すように修正します。
<action name="BookBinder" class="bookBinderAction" method="list"> <result name="success">/WEB-INF/jsp/BookBinder.jsp</result> </action>