代码之家  ›  专栏  ›  技术社区  ›  Anonymous

支柱和弹簧在一起?

  •  2
  • Anonymous  · 技术社区  · 17 年前

    我对Struts和Spring都很陌生。我需要知道如何在struts actionform中访问spring服务。即使是一个指向正确方向的指针也值得赞赏。

    3 回复  |  直到 16 年前
        1
  •  2
  •   Gareth Davis    17 年前

    从Struts 1 ActionForm类中,您将需要:

    WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext).getBean("yourService");
    
        2
  •  2
  •   D. Wroblewski    17 年前

    你用的是Struts1还是Struts2?

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

    至少还有两种将Struts1与Spring集成的方法。ibm developerworks上有一篇文章解释了不同解决方案的利弊,google“用spring更好地处理struts操作”(像我这样的新手不允许包含链接)。

        3
  •  1
  •   krosenvold    17 年前

    通常,您会将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的全部功能。