私はStrutsとSpringの両方にかなり慣れていません。Struts ActionForm で Spring Service にアクセスする方法を知る必要があります。正しい方向へのポインタでさえ感謝されます。
3 に答える
struts 1 ActionForm クラスから、次のものが必要になります。
WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext).getBean("yourService");
通常、Spring contextloader リスナーを Web xml に追加します。
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
次に、追加します
<constant name="struts.objectFactory" value="spring"/>
あなたのstruts.xmlに。
次に、アクション クラスで次のように言うことができます。
class MyAction {
@Autowired MyService service;
....
}
struts2 については以上です。
Struts 1 または 2 を使用していますか?
Struts 1 を使用している場合は、いくつかの方法があります。私は org.springframework.web.struts.DelegatingActionProxy を使用してそれを行うことを好みます。クラスパスに spring-webmvc-struts.jar が必要です。
struts-config.xml:
<action path="/faq" type="org.springframework.web.struts.DelegatingActionProxy" name="faqForm" parameter="method">
<forward name="List" path="faq.list" />
</action>
applicationContext.xml:
<bean name="/faq" class="com.mypackage.FAQAction" autowire="byType" />
この手法が最もエレガントであることがわかりました。Spring を使用しない古いコードには影響しません。
ストラット 1 をスプリングと統合するには、少なくともあと 2 つの方法があります。さまざまなソリューションの長所と短所を説明する ibm developerworks の記事があります。「Spring を使用して Struts アクションをより適切に処理する」を検索してください (私のような初心者はリンクを含めることはできません)。