`
black_angle
  • 浏览: 48192 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

整合 Struts 和 Spring

阅读更多

整合 Struts Spring三种方式:<o:p></o:p>

1:使用 SpringActionSupport 整合 Structs2: Spring DelegatingRequestProcessor 覆盖 Struts RequestProcessor3: Struts Action 管理委托 Spring 框架 <o:p></o:p>

您使用哪都需要使用 Spring ContextLoaderPlugin Struts ActionServlet Spring 用程序境。就像添加任何其他插件一简单地向您的 struts-config.xml 文件添加插件通过加载吧spring的配置文件加载进来:如下所示<o:p></o:p>

<plug-in className=<o:p></o:p>

  "org.springframework.web.struts.ContextLoaderPlugIn"><o:p></o:p>

    <set-property property="contextConfigLocation" value="/WEB-INF/beans.xml"/><o:p></o:p>

 </plug-in><o:p></o:p>

窍门 1. 使用 Spring ActionSupport(strutsspring耦合在一起了)<o:p></o:p>

org.springframework.web.struts.ActionSupport类提供了一个 getWebApplicationContext() 方法。您所做的只是从 Spring ActionSupport 而不是 Struts Action 类扩展您的public class SearchSubmit extends ActionSupport {  //下面execute方法里的内容,strutsspring耦合在一起了<o:p></o:p>

ApplicationContext ctx = getWebApplicationContext();  <o:p></o:p>
    BookService bookService =  (BookService) ctx.getBean("bookService");<o:p></o:p>
}//不继承ActionSupport;这里换成直接自己通过各种各种ApplicationContext来读取springbean定义//文件(这种情况不需要上面的< plug-in>标签也可以)<o:p></o:p>

窍门 2. 覆盖 RequestProcessor<o:p></o:p>

org.springframework.web.struts.DelegatingRequestProcessor 来覆盖 Struts  RequestProcessor 理程序<controller processorClass="org.springframework.web.struts. DelegatingRequestProcessor"/>下一是在我的 Spring 配置文件中注册该动<o:p></o:p>
<beans><o:p></o:p>
  <bean id="bookService" class="ca.nexcel.books.business.BookServiceImpl"/><o:p></o:p>
  <bean name="/searchSubmit" class="ca.nexcel.books.actions.SearchSubmit"> |(1)<o:p></o:p>
     <property name="bookService"><o:p></o:p>
        <ref bean="bookService"/><o:p></o:p>
     </property><o:p></o:p>
  </bean><o:p></o:p>
</beans><o:p></o:p>
注意:在 (1) ,我使用名称属性注册了一个 bean,以匹配 struts-config 作映射名称。SearchSubmit 示了一个 JavaBean 属性,允 Spring 在运行填充属性SearchSubmit类就是Action里面定义了 private BookService bookServiceget/set方法.<o:p></o:p>
缺点: 如果您使用一个不同的 RequestProcessortilesprocessor需要手整合 Spring  DelegatingRequestProcessor<o:p></o:p>

窍门 3. 作管理委托 Spring<o:p></o:p>

 Strut 作管理委托 Spring。您可以通 struts-config 作映射中注册一个代理来实现。代理负责 Spring 境中 Struts 作。由于作在 Spring 的控制之下,所以它可以填充作的 JavaBean 属性,并为应 Spring  AOP 截器之的特性来了可能。Action类和上面那种情况的类一样<o:p></o:p>
<action    path="/searchSubmit" <o:p></o:p>
             type="org.springframework.web.struts.DelegatingActionProxy" |(1)<o:p></o:p>
             input="/searchEntry.do"<o:p></o:p>
             validate="true"<o:p></o:p>
             name="searchForm"><o:p></o:p>
             <forward name="success" path="/WEB-INF/pages/detail.jsp"/><o:p></o:p>
             <forward name="failure" path="/WEB-INF/pages/search.jsp"/><o:p></o:p>
    </action>  <o:p></o:p>
是一个典型的 struts-config.xml 文件,只有一个小小的差。它注册 Spring 代理的名称,而不是声明作的名,如(1所示。DelegatingActionProxy 使用作映射名称 Spring 境中的作。就是我使用 ContextLoaderPlugIn 声明的<o:p></o:p>
<beans><o:p></o:p>
  <bean id="bookService" class="ca.nexcel.books.business.BookServiceImpl"/><o:p></o:p>
  <bean name="/searchSubmit"   class="ca.nexcel.books.actions.SearchSubmit"><o:p></o:p>
     <property name="bookService"><o:p></o:p>
        <ref bean="bookService"/><o:p></o:p>
     </property><o:p></o:p>
  </bean> <o:p></o:p>
*Spring Bean NameStruts Action Path关联,当Struts载对应ActionDelegatingActionProxy就根据入的path属性,在Spring Context对应bean,并将其例返回Struts<o:p></o:p>

作委托的点不止如此。一旦 Spring 控制您的 Struts

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics