2

私はStrutsとSpringの両方にかなり慣れていません。Struts ActionForm で Spring Service にアクセスする方法を知る必要があります。正しい方向へのポインタでさえ感謝されます。

4

3 に答える 3

2

struts 1 ActionForm クラスから、次のものが必要になります。

WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext).getBean("yourService");
于 2008-12-09T22:03:36.413 に答える
1

通常、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 については以上です。

于 2008-12-09T20:54:48.500 に答える
1

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 アクションをより適切に処理する」を検索してください (私のような初心者はリンクを含めることはできません)。

于 2009-05-25T14:01:02.253 に答える