Struts1.整合Spring的三种方法

使用 Spring 的 ActionSupport 类整合 Structs1.X, 使用 Spring 的 DelegatingRequestProcessor 覆盖 Struts 的 RequestProcessor,将 Struts Action 管理委托给 Spring 框架

装载应用环境:

      无论您使用哪种技术,都需要使用 Spring 的 ContextLoaderPlugin 为 Struts 的 ActionServlet 装载 Spring 应用程序环境


在struts-config.xml 文件尾处添加该插件:

< plug-in className=  "org.springframework.web.struts.ContextLoaderPlugIn">
    < set-property property= "contextConfigLocation"  value="/WEB-INF/classes/applicationContext.xml"/>
 < /plug-in>

第一种:使用Spring的ActionSupport

该方法: 简单快捷,但会导致struts和spring耦合在一起,如果要移值struts应用程序要重写代码.

例如:

  public class ActionName extends ActionSupport {  //继承自actionsupport
    public ActionForward execute(
    ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response)
    throws IOException, ServletException {

    DynaActionForm searchForm = (DynaActionForm) form;//使用动态的actionForm
    String isbn = (String) searchForm.get("isbn");
       
    //the old fashion way
    //BookService bookService = new BookServiceImpl();
       
    ApplicationContext ctx = getWebApplicationContext();    //org.springframework.web.struts.ActionSupport 类提              

                                                                                   //供了一个 getWebApplicationContext() 方法

    BookService bookService =
      (BookService) ctx.getBean("bookService");   //查找名为bookService的Spring bean
       
  Book book = bookService.read(isbn.trim());

    if (null == book) {
      ActionErrors errors = new ActionErrors();
      errors.add(ActionErrors.GLOBAL_ERROR,new ActionError
        ("message.notfound"));
      saveErrors(request, errors);
      return mapping.findForward("failure") ;
  }

    request.setAttribute("book", book);
    return mapping.findForward("success");
  }
}

第二种:覆盖 RequestProcessor

 该方法使用 org.springframework.web.struts.DelegatingRequestProcessor 类来覆盖 Struts 的 RequestProcessor 处理程序,通过

Spring 的 DelegatingRequestProcessor 进行整合,看下面的struts-config.xml文件的主要配置部分:

< form-beans>
    < form-bean name="searchForm"
      type="org.apache.struts.validator.DynaValidatorForm">
               < form-property name="isbn"    type="java.lang.String"/>
    < /form-bean>
 
  < /form-beans>

 < action    path="/searchSubmit"
               type="com.iwtxokhtd.books.actions.SearchSubmit"
               input="/searchEntry.do"
               validate="true"
               name="searchForm">
              < forward name="success" path="/WEB-INF/jsp/detail.jsp"/>
              < forward name="failure" path="/WEB-INF/jsp/search.jsp"/>
    < /action> 

 < controller processorClass="org.springframework.web.struts.
   DelegatingRequestProcessor"/>

< plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
    < set-property property="contextConfigLocation" value="/WEB-INF/classes/applicationContext.xml"/>
 < /plug-in>

然后在spring配置文件applicationContext.xml文件中进行bean配置:

< beans>
  < bean id="bookService" class="com.iwtxokhtd.books.business.BookServiceImpl"/>

  < bean name="/searchSubmit" //一定要与struts-config.xml文件中的action中的path名一样
    class="com.iwtxokhtd.books.actions.SearchSubmit"> //指名所在的action
     < property name="bookService">//dao
        < ref bean="bookService"/>//dao Impl 类
     < /property>
  < /bean>
< /beans>

此外,一定要在action类中加入 private BookService bookService;并生成相应的getter和setter方法

此方法比第一种要好,但如果您使用一个不同的 RequestProcessor,则需要手动整合 Spring 的 DelegatingRequestProcessor,添加的代码

会造成维护的麻烦并且将来会降低您的应用程序的灵活性。

第三种:将动作管理委托给 Spring

这里列出struts-config.xml中的主要部分:

< action    path="/searchSubmit"
             type="org.springframework.web.struts.DelegatingActionProxy"

             input="/searchEntry.do"
             validate="true"
             name="searchForm">
             < forward name="success" path="/WEB-INF/pages/detail.jsp"/>

             < forward name="failure" path="/WEB-INF/pages/search.jsp"/>
    < /action> 

< plug-in
    className="org.springframework.web.struts.ContextLoaderPlugIn">
    < set-property property="contextConfigLocation" value="/WEB-INF/classes/application.xml"/>
 < /plug-in>

然后在applicatinContext.xml文件中配置:

< beans>
  < bean id="bookService" class="ca.nexcel.books.business.BookServiceImpl"/>

  < bean name="/searchSubmit"  
        class="com.iwtxokhtd.books.actions.SearchSubmit">
     < property name="bookService">//dao
        < ref bean="bookService"/>//dao impl
     < /property>
  < /bean>

< /beans>

此方法是三种方法中最好的

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值