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. Logstash Reference Getting started with Logstash

    进阶功能_Logstash_数据采集_用户指南_日志服务-阿里云 https://help.aliyun.com/document_detail/49025.html Logstash Referen ...

  2. Checksum 磁盘扇区故障检测

    w https://en.wikipedia.org/wiki/Checksum https://zh.wikipedia.org/wiki/校验和 A checksum is a small-siz ...

  3. Java中native关键字使用

    native是与C++异构开发的时候用的.java自己开发不会使用

  4. C#中Windows通用的回车转Tab方法

    标签: windowsc#textboxbuttondropdownobject 2007-03-28 09:37 2773人阅读 评论(0) 收藏 举报  分类: C#(5)  版权声明:本文为博主 ...

  5. python基础之类的isinstance与issubclass、反射

    一 isinstance(obj,cls)和issubclass(sub,super) isinstance(obj,cls)检查是否obj是否是类 cls 的对象 class Foo: pass o ...

  6. Compilation failed: internal java compiler error

    在Idea中编译时出现这个错误:Error:java: Compilation failed: internal java compiler error. Information:Using java ...

  7. ssh登陆gitlab

    官方文档:https://docs.gitlab.com/ee/ssh/ Generating a new SSH key pair To generate a new SSH key pair, u ...

  8. 解决:IDEA unable to import maven project see logs for details问题+java http请求报java.net.SocketException: Permission denied:connect 问题

    背景:用IDEA写了一个java发送http请求的maven项目. 运行时,项目报java.net.SocketException: Permission denied:connect问题: 修改po ...

  9. 【我的Android进阶之旅】如何在浏览器上使用Octotree插件树形地展示Github项目代码?

    前言 最近有个同事看到我打开Github项目时,浏览器上的展示效果是树形的,于是他问我这个是什么浏览器插件,我告诉他是Octotree插件.现在我就来介绍介绍这款Octotree插件. 效果对比 1. ...

  10. sql server自动化运维脚本

    数据库运维中盛传一个小段子,我误删除了数据库,改怎么办?有备份还原备份,没有备份就准备简历!听起来有趣但发生在谁身上,谁都笑不起来.接触了很多的客户发现90%客户的运维策略都不是很完善.本篇就分享一些 ...