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. 【BZOJ3522】[Poi2014]Hotel 树形DP

    [BZOJ3522][Poi2014]Hotel Description 有一个树形结构的宾馆,n个房间,n-1条无向边,每条边的长度相同,任意两个房间可以相互到达.吉丽要给他的三个妹子各开(一个)房 ...

  2. Mindjet MindManager 出现Runtime Error解决方案

    Mindjet MindManager文件打开报错怎么解决?文件打开后提示Runtime Error!Program:C:\Program Files\MindManager 9\Mindmanage ...

  3. Go语言版本的helloworld

    新建一个project,然后建立一个main目录,在main目录下新建一个go类文件:main.go 内容如下: package main import "fmt" func ma ...

  4. Python高级教程-sorted

    Python中的排序算法 排序是程序中经常用到的算法.通常规定,对于两个元素x和y,如果认为x<y,则返回-1,如果认为x == y,则返回0,如果认为x > y,则返回1,这样,排序算法 ...

  5. Linux学习笔记—vim程序编辑器

    vi和vim vim是vi的升级版,支持vi的所有指令 vi的使用 vi分为三种模式:一般模式.编辑模式.命令行模式 一般模式 以vi打开一个文件就直接进入一般模式了,这个模式下可以使用上下左右按键来 ...

  6. The Unreasonable Effectiveness of Recurrent Neural Networks (RNN)

    http://karpathy.github.io/2015/05/21/rnn-effectiveness/ There’s something magical about Recurrent Ne ...

  7. 简明python教程五----数据结构

    python中有三种内建的数据结构:列表.元组和字典 list是处理一组有序项目的数据结构,即你可以在一个列表中存储一个序列的项目.在python中,每个项目之间用逗号分隔. 列表中的项目应该包括在方 ...

  8. python2函数

    1.函数的定义 函数的定义形式如下: def <name>(arg1,arg2...argN): <statements> 函数的名字必须以字母开头,可以包括下划线.函数的目的 ...

  9. sql join on 与where

    转载:http://www.cnblogs.com/Jessy/p/3525419.html left join :左连接,返回左表中所有的记录以及右表中连接字段相等的记录. right join : ...

  10. python16_day01【介绍、基本语法、流程控制】

    一.day01 1.二进制运算 60 & 13 =12 60 | 13 =61 60 ^ 13 =49 60<<2 =240 60>>2 =15 2.逻辑运算符 and ...