首页 > 其他分享 >struts与spring集成

struts与spring集成

时间:2023-07-19 18:05:47浏览次数:35  
标签:集成 xml dao spring struts Action org


1.struts与spring集成

Spring 和 struts 整合的四种方式。

1.使用Spring 的 ActionSupport
2.使用Spring 的 DelegatingRequestProcessor 类。
3.全权委托。org.springframework.web.struts.DelegatingActionProxy

4.使用 org.springframework.web.struts.AutowiringRequestProcessor 类,只需配置controller,不需要在spring中配置。自动装载能力强

无论用那种方法来整合第一步就是要为struts来装载spring的应用环境。就是在 struts 中加入一个插件。
struts-config.xml中

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

spring 的配置文件被作为参数配置进来。这样可以省略对web.xml 文件中的配置。确保你的applicationContext.xml 在WEB-INF目录下面

1,使用Spring的ActionSupport .
Spring 的ActionSupport 继承至 org.apache.struts.action.Action
ActionSupport的子类可以或得 WebApplicationContext类型的全局变量。通过getWebApplicationContext()可以获得这个变量。

这是一个 servlet 的代码:

public class LoginAction extends org.springframework.web.struts.ActionSupport {

public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
   LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub
                //获得 WebApplicationContext 对象              
  WebApplicationContext ctx = this.getWebApplicationContext();
  
   LoginDao dao = (LoginDao) ctx.getBean("loginDao");
   User u = new User();
  
   u.setName(loginForm.getName());
   u.setPwd(loginForm.getPwd());
  
  
   if(dao.checkLogin(u)){
    return mapping.findForward("success");
   }else{
    return mapping.findForward("error");
   }
  
}
}applicationContext.xml 中的配置
<beans>
<bean id="loginDao" class="com.cao.dao.LoginDao"/>
</beans>

这中配置方式同直接在web.xml文件配置差别不大。注意:Action继承自 org.springframework.web.struts.ActionSupport 使得struts和spring耦合在一起。
但实现了表示层和业务逻辑层的解耦(LoginDao dao = (LoginDao) ctx.getBean("loginDao"))。

 

通常这种方式是:抽象出一个父类,继承org.springframework.web.struts.ActionSupport,这样在action业务中不会任何其他框架的代码。

Public class baseAction extends ActionSupport
{
Public Object getBean(String beanName)
{
Return getWebApplicationContext().getBean(beanName);
}
}

想把整个策略交给spring的IoC特性来控制。

Public class baseAction extends ActionSupport
{
Private Object serviceObj;
Public void setServiceObj(Object serviceObj)            //这是关键的由他来实施注入策略
{
This.serviceObj=serviceObj;
}
Public Object getBean(String beanName)
{
Return this.serviceObj;
}
}

实际上上边的参数beanName已经没有意义了。

2,使用Spring 的 DelegatingRequestProcessor 类

DelegatingRequestProcessor 继承自 org.apache.struts.action.RequestProcessor 并覆盖了里面的方法。
sturts-config.xml 中<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"/>
通过 <controller >来替代
org.apache.struts.action.RequestProcessor 的请求处理。public class LoginAction extends Action {
//利用spring来注入这个对象。
private LoginDao dao ;

public void setDao(LoginDao dao) {
   System.out.println("执行注入");
   this.dao = dao;
}public LoginDao getDao() {
   return dao;
}public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
   LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub
  //这样一改这行代码似乎没有必要了。
   //WebApplicationContext ctx = this.getWebApplicationContext();
   //LoginDao dao = (LoginDao) ctx.getBean("loginDao");

   User u = new User();
  
   u.setName(loginForm.getName());
   u.setPwd(loginForm.getPwd());
  
   //直接用dao来调用spring会将这个对象实例化。
   if(dao.checkLogin(u)){
    return mapping.findForward("success");
   }else{
    return mapping.findForward("error");
   }
  
}
}


这里的。
LoginAction extends Action 说明 struts 每有和spring 耦合。
看一下

applicationContext.xml 中的配置。
<beans>
<bean id="loginDao" class="com.cao.dao.LoginDao"/>

<bean name="/login" class="com.cao.struts.action.LoginAction">
   <property name="dao">
    <ref local="loginDao"/>
   </property>
</bean>
</beans>

这里 name="/login" 与struts 中的path匹配
    class="com.cao.struts.action.LoginAction" 与struts 中的type匹配

还要为 LoginAction 提供必要的setXXX方法。获得ApplicationCotext和依赖注入的工作都在DelegatingRequestProcessor中完成。

3,全权委托:
Action 的创建和对象的依赖注入全部由IOC容器来完成。使用Spring的DelegatingAcionProxy来帮助实现代理的工作
org.springframework.web.struts.DelegatingActiongProxy继承于org.apache.struts.action.Action .

全权委托的配置方式同 方式 2 类似 (applcationContext.xml文件的配置和 Action类的实现方式相同)。

<struts-config>
<data-sources />
<form-beans >
    <form-bean name="loginForm" type="com.cao.struts.form.LoginForm" /></form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
    <!-- type指向的是spring 的代理类 -->
    <action
      attribute="loginForm"
      input="login.jsp"
      name="loginForm"
      path="/login"
      scope="request" type="org.springframework.web.struts.DelegatingActionProxy" >
      
      <forward name="success" path="/ok.jsp" />
      <forward name="error" path="/error.jsp" />
    </action>

</action-mappings>

  
<message-resources parameter="com.cao.struts.ApplicationResources" /><plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
   <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml"/>
</plug-in></struts-config>

不同之处
1, <action>中 type指向的是spring 的代理类

2, 去掉struts-config.xml中 <controller >

 

4.使用 org.springframework.web.struts.AutowiringRequestProcessor 类,它是spring2.0 后加入的。类似DelegatingRequestProcessor,但它的自动完成能力更强。首先也要在struts-config.xml中用<controller>声明:
........................
<controller processorClass="org.springframework.web.struts.AutowiringRequestProcessor"/>

其他的照常就得了,它比DelegatingRequestProcessor少了在spring配置中声明action的bean,它会根据action类名来自动完成注入操作。即Struts的Action中如有某业务类A对象的属性,又在spring配置文件中声明了业务类A的Bean,则AutowiringRequestProcessor就会自动将业务类A的Bean注入到Action中。
看上去你好像啥都没做,而事实上,注入工作已经由AutowiringRequestProcessor自动完成。这种autowire的注入支持两种不同的方式,分别是byName和byType,默认是byType。有时byType可能不起作用

The default is "byType", matching service layer beans with the Action's bean property argument types. This behavior can be changed through specifying an "autowire" init-param for the Struts ActionServlet with the value "byName", which will match service layer bean names with the Action's bean property names.
详情见spring2.5API中的这个类。

三种整和方式中我们优先选用 全权委托的方式。
理由:
1,第一种使得过多的耦合了Spring和Action .
2,RequestProcessor类已经被代理 如果要再实现自己的实现方式(如:编码处理)怕有点麻烦。

总结一下:
整合工作中的步骤:
1,修改struts-config.xml  
2, 配置applicationContext.xml
3, 为Action添加get/set方法 来获得依赖注入的功能

2.hibernate与spring的整合

方法一:非托管。使用spring的配置文件引入hibernate的配置文件hibernate.cfg.xml

applicationContext.xml中的写法:
    <bean id="sessionFactory"
       class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
       <property name="configLocation"
       value="classpath:hibernate.cfg.xml">
       </property>
       </bean>

方法二:完全托管。抛弃hibernate的配置文件,完全由spring来管理。同时使用第三方的连接池。

<bean id="dataSource"
       class="org.apache.commons.dbcp.BasicDataSource">
       <property name="driverClassName"
           value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
       </property>
       <property name="url"
           value="jdbc:sqlserver://lab303\accp:1438;databaseName=zf">
       </property>
       <property name="username" value="sa"></property>
       <property name="password" value="sa"></property>
    </bean>
    
    <bean id="sessionFactory"
       class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
       <property name="dataSource" ref="dataSource" />
       <property name="mappingResources">
           <list>
              <value>org/wang/ssh/entity/Tuser.hbm.xml</value>
           </list>
       </property>
       <property name="hibernateProperties">
           <props>
              <prop key="hibernate.dialect">
                  org.hibernate.dialect.SQLServerDialect
               </prop>
              <prop key="hibernate.show_sql">true</prop>
           </props>
       </property>
 
    </bean>

注意:这两种方法有一定的区别,第一种方式使用的hibernate自带的连接池,本身存在一些问题,官方建议使用第三方的连接池,如dbcp,c0p3等。这种方式默认单条增删改语句时,不会自动提交给数据库,必须显示的写出事务。或在hibernate.cfg.xml中配置自动提交事务。而方法二,单条增删改语句执行自动提交事务,可以不必显示声明事务。引起的原因是两者使用的数据源(连接池不同)。

标签:集成,xml,dao,spring,struts,Action,org
From: https://blog.51cto.com/u_16193056/6778777

相关文章

  • springboot
    起步依赖这两个包的源码:第一个:<developers><developer><name>Pivotal</name><email>[email protected]</email><organization>PivotalSoftware,Inc.</organization><organizationUrl>https://www.sprin......
  • 转:springboot2.0 集成redis服务详解,以及 (Lettuce & Jedis)
    springboot2.0集成redis服务详解,以及(Lettuce&Jedis)   ......
  • springboot简介
    spring程序于springboot程序对比springboot程序必须联网  另外可以去spring的官网创建项目进入spring官网选择这个选择配置点击generate生成会生成zip压缩文件......
  • spring boot 如何配置多个数据源,mysql和oracle
    在SpringBoot中配置多个数据源,例如MySQL和Oracle,可以通过以下步骤实现:添加依赖在pom.xml文件中添加MySQL和Oracle的依赖:<!--MySQL--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!......
  • SpringBoot系列---【StopWatch优雅实现统计代码耗时】
    StopWatch优雅实现统计代码耗时#org.springframework.util.StopWatchStopWatchstopWatch=newStopWatch();stopWatch.start("task1");//逻辑代码1...stopWatch.stop();log.debug("task1执行耗时:{}ms",stopWatch.getLastTaskTimeMillis());stopWatch.start("......
  • MySqlInjector 加不到springboot
    MySqlInjector加不到SpringBootSpringBoot是一个快速开发框架,它简化了Java应用程序的配置和部署过程。然而,有时我们可能会遇到一些问题,例如无法将MySqlInjector加到SpringBoot项目中。本文将介绍如何解决这个问题,并提供代码示例。问题描述当我们尝试将MySqlInjecto......
  • 【Spring Cloud Alibaba】毕业组件版本关系
    目录cloud组件版本关系框架版本依赖关系cloud组件版本关系SpringCloudAlibabaVersionSentinelVersionNacosVersionRocketMQVersionDubboVersionSeatacVersion2021.0.1.0*1.8.31.4.24.9.22.7.151.4.22.2.7.RELEASE1.8.12.0.34.6.12.7.131.3.0......
  • Spring boot 编程式事务
    SpringBoot编程式事务在SpringBoot中,事务管理是一项非常重要的功能。事务管理可以确保在数据库操作中的一系列操作要么全部成功,要么全部失败。SpringBoot提供了两种事务管理方式:声明式事务和编程式事务。本文将重点介绍SpringBoot的编程式事务管理。编程式事务简介编程式事......
  • Spring Boot怎么输出到控制台
    SpringBoot是一个开发框架,它提供了许多方便的功能,包括输出到控制台。在本文中,我将介绍如何在SpringBoot应用程序中输出到控制台,并提供代码示例。在SpringBoot中,可以使用标准的Java输出语句System.out.println()来输出到控制台。但是,SpringBoot还提供了更方便的方法来输出日志......
  • 二、集成MyBatis
    1.创建新模块 添加依赖1.1.pom.xml文件<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="htt......