spring 整合 struts2

整合之前要搞清楚struts2是什么;

struts2:表现层框架  增删改查  作用域  页面跳转   异常处理  ajax 上传下载  excel   调用service

spring :IOC/DI:bean容器   aop

整合点:

1.struts2的所有对象 交由spring来管理  意味着struts.xml内对象生成方式要发生改变

2.初始化容器的时机  服务器启动时完成

    spring已有方案解决

整合步骤:

1.建web 工程 copy jar包

     2.copy     struts.xml

a:<constant name="struts.objectFactory" value="spring"/>这个是说明struts的对象交于spring来处理

         b:action元素 class 属性的值 不再写包名+类名  而是写spring配置文件中的bean的id值

3.copy web.xml

      监听器   启动时加载   spring的配置文件

      由于监听器默认 读WEB-INF 下面名字为 applicationContext.xml的配置文件,不能读其它文件

      所以需要再加一个配置:       

        <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:applicationContext*.xml</param-value>
        </context-param>

      意思是读取src下所有以applicationContext开头的文件

web.xml:    

  <?xml version="1.0" encoding="UTF-8"?>
  <web-app id="WebApp_9" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
      <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
      </filter>
      <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
      <listener>
        <!-- 此监听器默认读WEB-INF 下的applicationContext.xml 文件 启动时加载我们的spring ioc 容器 -->
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      <!-- context-param 元素由 ServletContext对象解析 意思是读取src下 以applicationContext开头的所有文件 -->
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext*.xml</param-value>
      </context-param>
  </web-app>

action:

public class TestAction {
  public String execute(){
    System.out.println("============TestAction===========");
    return "success";
  }
}

applicationContext-action.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd" >
  <bean id="testAction" class="com.huawei.s2s.action.TestAction" scope="prototype" >
  </bean>
</beans>

struts.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
  <constant name="struts.objectFactory" value="spring"/>
  <package name="default" namespace="/" extends="struts-default">
    <action name="testAction" class="testAction" ><--注意这里class的值-->
      <result name="success">/index.jsp</result>
    </action>
  </package>
</struts>

时间: 2024-08-10 07:45:01

spring 整合 struts2的相关文章

Spring整合Struts2

Spring整合Struts21整合目的:让Spring的IOC容器去管理Struts2的Action, 2Struts2是web开源框架,Spring要整合Struts2,也就是说要在web应用使用Spring①. 需要额外加入的 jar 包:spring-web-4.0.0.RELEASE.jarspring-webmvc-4.0.0.RELEASE.jar ②. Spring 的配置文件, 和非 WEB 环境没有什么不同 ③. 需要在 web.xml 文件中加入如下配置: <!-- 配置

Spring整合Struts2(接上篇,来自传智播客视频,包含所有源码)

接上篇:Spring整合Hibernate:http://blog.csdn.net/u010800530/article/details/38470321 首先,如果要整合Struts2,则核心拦截器是必不可少的了,然后还要配置ContextLoaderListener监听器. 一.这里是web.xml的配置内容,接下来会详细介绍它的所有配置 <?xml version="1.0" encoding="UTF-8"?> <web-app vers

Struts2的使用以及Spring整合Struts2

一.如何单独使用Struts2 (1)引入struts2的jar包 commons-fileupload-1.2.1.jar freemarker-2.3.15.jar ognl-2.7.3.jar struts2-core-2.1.8.jar xwork-core-2.1.6.jar (2)在项目的src下引入struts.xml文件 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts

SSH之Spring整合struts2

Struts2+spring整合/result常用类型/拦截器 为什么?通过spring管理Struts2的组件,实现注入 怎么整合? 1.创建项目 2.导包:struts2-spring-plugin struts2-core 3.配置文件:web.xml spring-context.xml web.xml:配置struts2的filert,配置spring的启动加载配置文件,配置spring的监听器 <!-- 配置spring监听,用于初始化spring容器 --> <listen

Spring学习(八)spring整合struts2

一.spring框架对struts等表现层框架的整合原理 : 使用spring的ioc容器管理struts中用于处理请求的Action 将Action配置成ioc容器中的bean 延伸:spring对持久层框架/技术的整合原理 (封装) : 提供模板类封装对应技术/框架的开发流程 通过对模板类的使用,实现对传统开发流程的"代替". 二.整合方式: 插件方式 struts2为了实现对spring框架整合,也提供了一个插件的配置文件struts-plugin.xml struts2-spr

Spring整合Struts2的配置与测试

整合目的 让Spring的IOC容器管理Struts2的Action 整合步骤 1.新建一个Web项目 2.加入Spring的jar包和添加Spring的配置文件 3.在Web.xml中配置ContextLoadListener 4.加入Struts2的jar包 5.在web.xml文件中配置Struts的filter 6.加入Struts2配置文件 7.新建Bean,Service和Action类 8.在Spring配置文件中对Bean,Service和Action类进行配置 9.配置Stru

Spring整合Struts2的两种方式

Spring提供了一个ContextLoaderListener,该监听类实现了ServletContextListener接口.该类可以作为Listener使用,它会在创建时自动查找WEB-INF/下的applicationContext.xml文件,因此如果只有一个配置文件且配置文件命名为applicationContext.xml,则只需在web.xml文件中增加如下配置片段: <!-- 使用ContextLoaderListener初始化Spring容器 --> <listene

Spring和Struts2整合

Spring整合Struts2 步骤:1.导入Struts2jar相关包,并且导入Struts2-Spring-plugin-2.0.11.2.jar 2.配置xml文件:配置Struts2过滤器和Spring上下文参数和监听器 <?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/

struts2与spring整合时需要注意的点

首先我们需要明白spring整合struts2中的什么东西,spring中的核心就是IOC和AOP,IOC是对象的容器,AOP是处理动态代理的;比如spring与hibernate整合时就要用到aop,具体就是把事务的开启与关闭交于spring中的aop处理.一句话,spring就是整合struts2的对象. 先前struts.xml配置文件中action标签中的class属性的值是"包名+类名",这样配置就可以让struts2自己生成对象,但这样不符合模式设计六大原则,为了解决这个问