Spring与Struts2整合
Spring与Struts2为什么要整合呢?
把Action实例交给Spring来管理!!
1.单独测试Struts是否添加成功(jar包和配置文件),先单独测试,不要整合之后再测试,容易出问题
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<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>
</web-app>
Action
public class TestAction extends ActionSupport {
@Override
public String execute() throws Exception {
return "success";
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!--开发模式-->
<constant name="struts.devMode" value="true"/>
<!-- 配置action,在没有与Spring整合时,class属性写的是类的全限定名。
当与Spring整合后,class属性写的是bean的名称。
注意:这个bean一定要是多例的模式(prototype)。
-->
<package name="default" namespace="/" extends="struts-default">
<action name="test" class="com.winner.action.TestAction">
<result name="success">/test.jsp</result>
</action>
</package>
</struts>
2.单独测试spring是否添加成功(jar包和配置文件)
applicationContext.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 自动扫描与装配bean -->
<context:component-scan base-package="com.winner"/>
</beans>
@Service
public class UserService {
public void saveUser(Object user) {
System.out.println("保存一个User对象");
}
}
public class UserServiceTest {
@Test
public void testSaveUser() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) ac.getBean("userService");
System.out.println(userService);
}
}
IOC容器成功管理了实例对象,表示Spring环境添加成功。
3.整合
整个应用程序只需要一个容器对象就可以了
11.在web.xml中配置一个监听器,最大作用范围的监听器,当应用程序启动的时候,它就会初始化这个容器。
<!-- 配置Spring的用于初始化ApplicationContext的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--学习spring的时候我们就知道生成容器需要读取配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
22.拷贝一个用于整合的jar包(与Spring整合用的一个插件)
struts-2.1.8.1/lib/struts2-spring-plugin-2.1.8.1.jar
33.说明:
1.在写Action时要指定@Controller 与@Scope("prototype")
2.在struts.xml中配置action时,在class属性中写bean的名称
Spring与Struts2整合的更多相关文章
- Spring与Struts2整合VS Spring与Spring MVC整合
Spring与Struts2整合,struts.xml在src目录下 1.在web.xml配置监听器 web.xml <!-- 配置Spring的用于初始化ApplicationContext的 ...
- spring与struts2整合出现错误HTTP Status 500 - Unable to instantiate Action
在进行spring和struts2整合的时候因为大意遇到了一个问题,费了半天神终于找到了问题所在,故分享出来望广大博友引以为戒!! 我们都知道在spring和struts2整合时,spring接管了a ...
- Spring与Struts2整合时action自动注入的问题
当Struts和Spring框架进行整合时,原本由action实例化对象的过程移交给spring来做(这个过程依赖一个叫struts2-spring-plugin的jar包,这个包主要的功能就是实现刚 ...
- Spring注解与Spring与Struts2整合
@Component @Controller @Service @Repository 四大注解作用基本一样,只是表象在不同层面 @Resource @Scope Struts2与Spring整合:1 ...
- Spring和Struts2整合
目的:spring容器管理Action类,代替Servlet 步骤:主要在配置文件 Struts2: 添加支持spring的jar包, 配置<action class="Action类 ...
- spring+hibernate+Struts2 整合(全注解及注意事项)
最近帮同学做毕设,一个物流管理系统,一个点餐系统,用注解开发起来还是很快的,就是刚开始搭环境费了点事,今天把物流管理系统的一部分跟环境都贴出来,有什么不足的,请大神不吝赐教. 1.结构如下 2.jar ...
- spring与struts2整合出现常见错误
错误信息 严重: Exception starting filter struts2 Unable to load configuration. - bean - jar:file:/F:/Strut ...
- Spring+Hibernate+Struts2整合之实现登录功能
前端代码: <form id="loginForm" action="${ pageContext.request.contextPath }/user_login ...
- Spring学习6-Spring整合Struts2
一.Spring为什么要整合Struts2 Struts2与Spring进行整合的根本目的就是要让 Spring为Struts2的Action注入所需的资源对象,它们整合的原理则是只要导入了s ...
随机推荐
- Junit的最简单样例:Hello world!
我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3824934.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...
- CVTE面试总结
刚面完了CVTE的一面,感觉完败,现总结. 一:准备不充分 这个跟时间仓促也有关系.昨天下午收到通知,晚上九点多回来填写给定格式的简历,题目也比较多.由于时间较仓促,那些问题的答案没有过多斟酌.但糟糕 ...
- 屏蔽ubuntu桌面鼠标右键以及Ctrl Alt F*
1.屏蔽右键: xmodmap -e "pointer = 1 2 99"xmodmap -e 'pointer = 1 2 0 4 5 6 7 8 9' #xmodmap -e ...
- spring-cloud-bus
安装rabbitmq 依赖erlang: http://erlang.org/download/otp_win64_18.2.exe
- soinn
Growing Cell Structures A Self-Organizing Network for Unsupervised and Supervised Learning Here, and ...
- ES6学习笔记(十四)
1.Promise的含义 Promise是异步编程的一种解决方案,比传统的解决方案--回调函数和事件--更合理和更强大.它由社区最早提出和实现,ES6将其写进了语言标准,统一了用法,原生提供了Prom ...
- XZ压缩最新压缩率之王
xz这个压缩可能很多都很陌生,不过您可知道xz是绝大数linux默认就带的一个压缩工具. 之前xz使用一直很少,所以几乎没有什么提起. 我是在下载phpmyadmin的时候看到这种压缩格式的,phpm ...
- vector 内部方法大全 学习(初学者的参考资料)
1 vector构造函数:也就是如何对一个vector对象进行初始化 ////////////////////////////代码//////////////////////////////// ...
- OS/400相关介绍
OS/400是IBM公司为其AS/400以及AS/400e系列商业计算机开发的操作系统,由于OS/400的设计充分考虑了AS/400的硬件设计,而且通常作为AS/400的一个基本组件被提供,因此几乎没 ...
- Linux学习系列之Linux入门(三)gcc学习
GCC(GNU Compiler Collection,GNU编译器套装),是一套由GNU开发的编程语言编译器.它是一套以GPL及LGPL许可证所发布的自由软件,也是GNU计划的关键部分,亦是自由的类 ...