springstruts:如何集成Spring和Struts

        本文想通过个简单例子阐述如何集成Spring和Struts

        1.Struts和Spring
        Struts 代表了MVC第 2类架构实现在Struts中最重要组件是ActionServlet, Action,和 ActionForm 子类ActionServlet 代表controller他基于配置文件接受请求和 把这些请求转发到相应ActionForm和Action子类 ActionForm把用户输入数据传送到Action, Action商务层组件完成必要操作最后提交到viewActionServlet使用个配置文件(struts-config.xml)加载Action子类定义用以接受用户请求基于请求URL, controller 找到个action定义去接受这个请求 Struts构件处理用户请求, 检查配置文件, 完成相应动作

        Spring是种轻量级容器它使得使用个外部XML配置文件非常容易绑定对象每个对象能够通过列出JavaBean属性得到个依赖对象指针,通过绑定XML配置文件使剩下工作更加简单依赖注入(DI)是非常强大功能Spring支持可插拔事务管理器提供事物管理方式更多选择. 它集成了持久性架构同时也提供了个统exception 分类Spring也提供面向方面(AOP)编程简单机制

        2.Struts和Spring集成
        将Struts应用集成到Spring框架可以采用多种思路方法首先Spring明显地被设计用于解决JEE现实问题如复杂性性能低下可测试性及其他;第 2Spring框架包括个AOP实现让你可以使用面向方面设计技术;第 3, Spring 框架可以能够非常容易地管理和协调Struts;和Struts类似, Spring也包含MVC 实现.两个架构都有优缺点Struts是MVC最重要架构,很多开发团队(Team)学会了依靠Struts在规定时限内开发出高质量软件Software因此开发团队(Team)宁愿集成Spring功能也不愿意转到Spring MVC;好消息是Spring结构允许你集成Struts Web 框架、基于Spring业务层和持久层我们思路方法是应用Spring中ActionSupport类去集成Struts 

        3.加载应用context
        首先我们需要使用Spring中ContextLoaderPlugin为Struts ActionServlet去装载Spring应用上下文简单在struts-config.xml 文件中增加plug-in如下(1)所示:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
 <form-beans>
    <form-bean name="searchForm"
                type="org.apache.struts.validator.DynaValidatorForm">
               <form-property name="cardno"    type="java.lang.String"/>
    </form-bean>
  </form-beans>
 <global-forwards type="org.apache.struts.action.ActionForward">
     <forward   name="welcome"          path="/welcome.do"/>
     <forward   name="searchEntry"       path="/searchEntry.do"/>
     <forward   name="searchSubmit"      path="/searchSubmit.do"/>
 </global-forwards>

 <action-mappings>
    <action    path="/welcome" forward="/WEB-INF/pages/welcome.htm"/>
    <action    path="/searchEntry" forward="/WEB-INF/pages/search.jsp"/>
    <action    path="/searchSubmit"
               type=" com.infotek.Creditcard.actions.SearchSubmit"
               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>  
 </action-mappings>
 <message-resources parameter="ApplicationResources"/>
 <plug-in Name="org.apache.struts.validator.ValidatorPlugIn">
    <-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
 </plug-in>
 <plug-in Name="org.springframework.web.struts.ContextLoaderPlugIn">  (1)
    <-property property="contextConfigLocation" value="/WEB-INF/beans.xml"/>
 </plug-in>
</struts-config> 


="xspace-totlerecord">2="xspace-totlepages">1/2="xspace-current">12>
Tags:  spring整合struts strutsspringhibe struts2spring springstruts

延伸阅读

最新评论

发表评论