1. 概述

spring和struts整合:
1.创建web程序
2.引入struts2类库.
3.创建HelloWorldAction
package cn.itcast.struts2.action;
import com.opensymphony.xwork2.ActionSupport;
/**
* HelloWorldAction
*/
public class HelloWorldAction extends ActionSupport {
private static final long serialVersionUID = 6480501738385774728L;
public String reg() {
System.out.println("hello world");
return SUCCESS;
}
}
3.配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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>action</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>action</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
4.配置struts.xml
<?xml version="1.0"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="helloworldPkg" extends="struts-default">
<action name="HelloWorldAction_*" class="cn.itcast.struts2.action.HelloWorldAction" method="{1}">
<result name="success">/index.jsp</result>
</action>
</package>
</struts> 5.引入spring类库 + struts2-spring-plugin-2.1.8.1.jar
6.配置web.xml
<!-- 通过上下文参数指定spring配置文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param> <!-- 上下文载入器监听器,确保web服务器启动时,直接完成spring容器的初始化,将ac放到application范围中 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> 7.修改struts.xml配置文件中的action
把action的class改成bean的id.
<!-- class:指定action在spring容器的id -->
<action name="HelloWorldAction_*" class="helloWorldAction" method="{1}"> struts2加载顺寻:
1.struts-core.jar/struts.xml
2.xxx-plugin.xml / struts-plugin.xml
3.project/struts.xml ac = new Classpathxmlac(new String[]{a.xml,b.xml});

2. 示例代码

HelloWorldService.java, service接口

public interface HelloWorldService {
public void sayHello();
}

HelloWorldServiceImpl.java, service实现

//方式二:注解配置
@Service("helloWorldService")
public class HelloWorldServiceImpl implements HelloWorldService {
public void sayHello() {
System.out.println("this is a service");
}
}

HelloWorldAction.java, 处理请求的action

/**
* HelloWorldAction, 方式二:注解配置
*/
@Controller("helloWorldAction")
@Scope("prototype")
public class HelloWorldAction extends ActionSupport {
private static final long serialVersionUID = 6480501738385774728L; @Resource()
private HelloWorldService hws ; public HelloWorldAction(){
System.out.println("new HelloWorldAction()");
}
public String reg() {
ServletActionContext.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
hws.sayHello();
System.out.println("hello world");
return SUCCESS;
}
}

beans.xml,spring配置bean, 只配置bean

<?xml version="1.0"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
<!-- 多个配置文件的情况,action,service配置分开
<import resource="actions.xml" />
-->
<!-- helloWorldService 方式一,xml配置
<bean id="helloWorldService"
class="cn.itcast.struts2.service.HelloWorldServiceImpl">
</bean>
-->
<!-- 组件扫描 方式二,注解配置 -->
<context:component-scan base-package="cn.itcast.struts2.action,cn.itcast.struts2.service" />
</beans>

actions.xml,spring配置action

<?xml version="1.0"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- helloworldAction -->
<bean id="helloWorldAction"
scope="prototype"
class="cn.itcast.struts2.action.HelloWorldAction">
<property name="hws" ref="helloWorldService" />
</bean>
</beans>

struts.xml, struts配置

<?xml version="1.0"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="helloworldPkg" extends="struts-default">
<!--class 应该设置为action.xml中对应的ID,才能有spring的注入属性-->
<action name="HelloWorldAction_*" class="helloWorldAction" method="{1}">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">
<!-- 通过上下文参数指定spring配置文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param> <!-- 上下文载入器监听器,确保web服务器启动时,直接完成spring容器的初始化,并把ac保存到application中 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>action</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>action</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

Spring -- spring整合struts2的更多相关文章

  1. Spring 框架整合Struts2 框架和 Hibernate 框架

    1. Spring 框架整合 Struts2 框架 // [第一种整合方式(不推荐)](http://www.cnblogs.com/linkworld/p/7718274.html) // 从 Se ...

  2. Spring笔记⑥--整合struts2

    Spring如何在web应用里面用 需要额外加入的jar包 Spring-web-4.0.0 Spring-webmvc-4.0.0 Spring的配置文件,没什么不同   需要在web.xml下配置 ...

  3. Spring框架整合Struts2

    1,用Spring架构,及Struts2-spring-plugin插件 导入Spring的dist全部所需的jar包 Struts2的spring插件 struts2-spring-plugin.X ...

  4. Spring框架整合Struts2框架的传统方法

    1. 导入CRM项目的UI页面,找到添加客户的页面,修改form表单,访问Action * 将menu.jsp中133行的新增客户的跳转地址改为:href="${pageContext.re ...

  5. spring整合struts2

    1. Spring 如何在 WEB 应用中使用 ? 1). 需要额外加入的 jar 包: spring-web-4.0.0.RELEASE.jarspring-webmvc-4.0.0.RELEASE ...

  6. Spring 整合 Struts2

    1. Spring 如何在 WEB 应用中使用 ? 1). 需要额外加入的 jar 包: spring-web-4.0.0.RELEASE.jar spring-webmvc-4.0.0.RELEAS ...

  7. struts2 spring mybatis 整合(test)

    这几天搭了个spring+struts2+mybatis的架子,练练手,顺便熟悉熟悉struts2. 环境:myEclipse10+tomcat7+jdk1.6(1.8的jre报错,所以换成了1.6) ...

  8. Struts2的使用以及Spring整合Struts2

    一.如何单独使用Struts2 (1)引入struts2的jar包 commons-fileupload-1.2.1.jar freemarker-2.3.15.jar ognl-2.7.3.jar ...

  9. Spring学习6-Spring整合Struts2

    一.Spring为什么要整合Struts2     Struts2与Spring进行整合的根本目的就是要让 Spring为Struts2的Action注入所需的资源对象,它们整合的原理则是只要导入了s ...

  10. Spring与Struts2整合VS Spring与Spring MVC整合

    Spring与Struts2整合,struts.xml在src目录下 1.在web.xml配置监听器 web.xml <!-- 配置Spring的用于初始化ApplicationContext的 ...

随机推荐

  1. 【BZOJ4421】[Cerc2015] Digit Division 动态规划

    [BZOJ4421][Cerc2015] Digit Division Description 给出一个数字串,现将其分成一个或多个子串,要求分出来的每个子串能Mod M等于0. 将方案数(mod 1 ...

  2. 推荐一个CSS类库

    animate.css 一个封装好的动画效果类

  3. Openstack虚拟机创建流程

    续上一篇Openstack安装配置 一,keystone交互认证阶段 1,发送用户名和密码给keystone认证获取token 2,带着token访问nova-api 3,nova-api使用toke ...

  4. 穿透Session 0 隔离(二)

    上一篇我们已经对Session 0 隔离有了进一步认识,如果在开发过程中确实需要服务与桌面用户进行交互,可以通过远程桌面服务的API 绕过Session 0 的隔离完成交互操作. 对于简单的交互,服务 ...

  5. 第三课作业——set类型、sorted set类型的增删改查,redis的事务

    第三课时作业 静哥 by 2016.2.23~2016.3.6   [作业描述] 1.总结什么是set以及什么是sorted set,并完成对set以及sorted set的增删改查(查需要至少4种方 ...

  6. 创建存储过程修改role密码

    1 创建存储过程 DELIMITER | drop procedure if exists pro_update_role_pwd; CREATE PROCEDURE pro_update_role_ ...

  7. IO流入门-第九章-BufferedReader_BufferedWriter复制

    利用BufferedReader和BufferedWriter进行复制粘贴 import java.io.*; public class BufferedReader_BufferedWriterCo ...

  8. python pip源配置,pip配置文件存放位置

    https://blog.csdn.net/u013066730/article/details/54580789/ pip源配置文件可以放置的位置: Linux/Unix: /etc/pip.con ...

  9. 编译Elasticsearch源码

    1.从github上clone  es的源码 git clone https://github.com/elastic/elasticsearch.git 2.如果没有安装gradle的话,需要安装g ...

  10. Python迭代对象、迭代器、生成器

    在了解Python的数据结构时,容器(container).可迭代对象(iterable).迭代器(iterator).生成器(generator).列表/集合/字典推导式(list,set,dict ...