转载:struts2和spring的结合原理(精品)
转载网址:http://blog.sina.com.cn/s/blog_683278bc0101236z.html
Ioc将所有的对象之间的关系转移到了xml配置文件中来。 在TopLogic中声明存储SecondLogic的属性
public class TopLogic{
private SecondLogic logic ;
public void setSecondLogic(SecondLogic logic) {
this.logic = logic;
}
public TopLogic() {
}
public topMethod() {
loggic.someMethod();
}
}
声明变量之间的关系:
<beans>
<bean id="secondLogic" class="com.jpleasure.teamware.logic.SecondLogic">
<property name="XXX" value = "11111"/>
<property name="YYY" value = "22222"/>
</bean>
<bean id="topLogic" class="com.jpleasure.teamware.logic.TopLogic">
<property name="secondLogic" ref = "secondLogic"/>
</bean>
</beans>
当我们从Spring IOC容器中取得topLogic的时候,其中的secondLogic属性自动的通过
配置文件的配置得到了初始化。
1.为什么Strut2需要使用SpringFramework
在Struts2中Action是我们扩展的点,我们使用Action处理用户提交的请求,向用户展现数据。
为了更好的组织代码的结构,我们一般情况下使用三层的构架:
Action → Logic → Dao 这样如果我们手动的管理这些对象之间的关系,以方便非常的繁琐,另外也很不利于管理 变更,所以我们更倾向于使用Spring等类似的IOC框架类管理。
2.如何将Springframework和Struts2结合起来 回想第一章,我们指导在Struts2中每一个对象都是靠ObjectFactory创建的,而Springframework
就是用来管理对象创佳的,我们只需要将ObjectFactory的对象创建方法改为Spring即可。
1)在struts.properties或者struts.xml文件中将objectfactory的属性设置为spring方式:
xml方式:
<constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
properties方式:
struts.objectFactory=org.apache.struts2.spring.StrutsSpringObjectFactory
2)当然还需要将需要的Jar文件放在WEB-INF/lib目录下
这些文件包括:
struts2-spring-plugin-2.0.9.jar
spring.jar(为了省事,包含了所有的spring模块,实际项目中可以考虑使用具体的模块)
3)剩余的就是Springframework的配置了
第一,告诉Spring,对象间关系的配置在那里存放
需要在web.xml文件中添加如下内容(注意顺序):
这个Listener在应用程序启动的时候启动,用来读取配置文件。
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
这个参数配置了配置文件的路径,说明我们可以在classpath或者WEB-INF目录下
防止名字满足applicationContext-*.xml格式你的文件。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml
</param-value>
</context-param>
4)如何让Struts2的Action和Springframework的bean管理起来?
Struts2会根据struts.xml中配置的class名字在springframeowrk中找id相同的bean,如果找不到
就按照普通的方式实例化Action类。
例如:
struts-todo.xml
<action name="forward" method="forward"
class="todo">
<result name="insert">/todo/insert.jsp</result>
<result name="update">/todo/insert.jsp</result>
<result name="delete">/todo/insert.jsp</result>
</action>
applicationContext-todo.xml
<bean id="todo" class="com.jpleasure.teamware.action.TodoAction"
singleton="false">
<property name="todoLogic" ref="todoLogic"></property>
</bean>
则class=“todo”的Action会使用com.jpleasure.teamware.action.TodoAction。
基本程序结构:
│ .classpath
│ .project
├─src
│ │ applicationContext-todo.xml
│ │ struts-todo.xml
│ │ struts.properties
│ │ struts.xml
│ │ teamware.properties
│ │
│ └─com
│ └─jpleasure
│ └─teamware
│ ├─action
│ ├─dao
│ ├─fw
│ ├─logic
│ ├─util
│ └─vo
└─WebContent
│ Index.html
│ Index.jsp
│
├─META-INF
│ MANIFEST.MF
│
├─todo
│ list.jsp
│
└─WEB-INF
│ web.xml
│
└─lib
commons-logging-1.0.4.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
spring.jar
struts2-core-2.0.9.jar
struts2-spring-plugin-2.0.9.jar
xwork-2.0.4.jar
转载:struts2和spring的结合原理(精品)的更多相关文章
- Struts2、Spring、Hibernate 高效开发的最佳实践(转载)
Struts2.Spring.Hibernate 高效开发的最佳实践 Struts2.Spring.Hibernate(SSH)是最常用的 Java EE Web 组件层的开发技术搭配,网络中和许多 ...
- 搭建SSH框架整合Struts2和Spring时,使用@Autowired注解无法自动注入
© 版权声明:本文为博主原创文章,转载请注明出处 1.问题描述: 搭建SSH框架,在进行Struts2和Spring整合时,使用Spring的@Autowired自动注入失败,运行报错java.lan ...
- Struts2 整合Spring(Maven,注解版)
这两天正在试验Struts2与Spring框架的整合,和他们各自的“注解”.今天就总结一下这两个框架怎么用注解进行整合. 一,加入两者的依赖包,除了两者的必要依赖外,还需要导入struts2-spri ...
- 【Spring】Spring框架之Struts2和Spring的优点
Java Web开发使用Structs2和Spring框架的好处 今年我一直在思考web开发里的前后端分离的问题,到了现在也颇有点心得了,随着这个问题的深入,再加以现在公司很多web项目的控制层的技术 ...
- 基于struts2、spring的应用闲置一段时间后报空指针错(转)
在做struts2.spring网站时,在系统闲置一段时间后,访问页面会出错,第二次再访问就正常了.后来查了后台日志,发现是数据库连接关闭了,导致页面访问出错.页面上报空指针错误,错误没有保留,日志中 ...
- 二十六:Struts2 和 spring整合
二十六:Struts2 和 spring整合 将项目名称为day29_02_struts2Spring下的scr目录下的Struts.xml文件拷贝到新项目的scr目录下 在新项目的WebRoot-- ...
- web项目中加入struts2、spring的支持,并整合两者
Web项目中加入struts2 的支持 在lib下加入strut2的jar包 2. 在web.xml中添加配置 <filter> <filter-name>struts2< ...
- 第一次做的struts2与spring整合
参考:http://www.cnblogs.com/S-E-P/archive/2012/01/18/2325253.html 这篇文章说的关键就是“除了导入Struts2和Spring的核心库之外, ...
- struts2+hibernate-jpa+Spring+maven 整合(1)
1.0.0 struts2 与 spring 的整合. 1.1.0 新建maven工程 , 编写pom.xml ,这里只需要简单的添加 一个组件就够了: 在myeclipse 生成的pom.xml 添 ...
随机推荐
- 模拟springmvc 内部登陆,跳过spring filter
说明,因为我们的一个项目B使用spring mvc配置的登陆框架,所以对登陆控制全部交给了spring,导致我们如果想通过另一个项目A登陆到项目B就不太容易,具体是项目A登陆了,我们通过一个连接直接跳 ...
- 两段小PYTHON,作啥用的,行内人才懂~~~:(
哎,作也不是,不作也不是.... 下次有更新文件时,直接刷新一次了. #coding: UTF-8 import sys reload(sys) sys.setdefaultencoding( &qu ...
- poj3237 Tree
Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...
- SPOJ--K-query (线段树离线) 离线操作解决一些问题
K-query Given a sequence of n numbers a1, a2, ..., an and a number of k- queries. A k-query is a tri ...
- java与数据结构(3)---java实现循环链表
循环链表:将单链表中尾结点的指针由空指针改为指向头结点,就使整个单链表形成一个环,这种首尾相接的单链表称为单链表循环表,即循环链表. 循环链表与单链表最重要的区别是:尾结点的指针,不再是p->n ...
- Adobe Photoshop CC 2015安装激活
分享一下Adobe Photoshop CC 2015 安装激活步骤 工具/原料 xf-adobecc2015.exe Adobe Photoshop CC 2015官方原版 方法/步骤 1 ...
- Heritrix源码分析(十四)
近段时间在搞定Lucene的一些问题,所以Heritrix源码分析暂时告一段落.今天下午在群里有同学提到了Heritrix异常终止的问题以及让Heritrix不停的抓取(就是抓完一遍后载入种子继续抓取 ...
- 从 Windows 到 Android: 威胁的持续迁移
作者:趋势科技 新闻媒体现在正喧腾着 OBAD 这个 Android 恶意软件,这也是到目前为止,Android 恶意软件中“最坏”,同时也是“最先进的 Android 木马程序”.除了各种强大的功能 ...
- 高速排序-c++(分别用数组和容器实现)
/********************************************************************** *版权全部 (C)2014, cheng yang. * ...
- AJAX的概念介绍
AJAX学习 1.XMLHttpRequest对象创建 var request= new XMLHttpRequest(); 兼容ie6.ie5 var request; if(windoe.XMLH ...