【问题标题】:Struts and Spring together?Struts 和 Spring 在一起?
【发布时间】:2009-10-09 20:06:22
【问题描述】:

我对 Struts 和 Spring 都很陌生。我需要知道如何在 Struts ActionForm 中访问 Spring Service。即使是指向正确方向的指针也会受到赞赏。

【问题讨论】:

    标签: spring struts service actionform


    【解决方案1】:

    来自您将需要的 struts 1 ActionForm 类:

    WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext).getBean("yourService");
    

    【讨论】:

      【解决方案2】:

      通常你将 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 的全部内容。

      【讨论】:

        【解决方案3】:

        您使用的是 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 的旧代码。

        至少还有两种方法可以将 struts 1 与 spring 集成。 ibm developerworks 上有一篇文章解释了不同解决方案的优缺点,谷歌“Get a better handle on Struts actions, with Spring”(像我这样的新手不允许包含链接)。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-10-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多