为了在Struts中加载Spring context,需要在struts-config.xml文件中加入如下部分:
- <struts-config>
- <plug-in
- className="org.springframework.web.struts.ContextLoaderPlugIn">
- <set-propertyproperty="contextConfigLocation"
- value="/WEB-INF/applicationContext.xml"/>
- </plug-in>
- </struts-config>
<struts-config> <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" /> </plug-in> </struts-config>
第一种方法:
通过Struts的plug-in在Struts和Spring之间提供了良好的结合点。通过plug-in我们实现了Spring context的加载,不过仅仅加载Spring context并没有什么实际的意义,还应该经过配置将Struts的Action交给Spring容器进行管理。
- <action-mappings>
- <action path="/login"
- type="org.springframework.web.struts.DelegatingActionProxy"
- name="loginForm">
- <forwardname="success"path="/main.jsp"/>
- <forwardname="failure"path="/login.jsp"/>
- </action>
- </action-mappings>
<action-mappings> <action path="/login" type="org.springframework.web.struts.DelegatingActionProxy" name="loginForm"> <forward name="success" path="/main.jsp" /> <forward name="failure" path="/login.jsp" /> </action> </action-mappings>
在form bean这个节点上与传统的Struts配置没有什么区别,而在Action上面则发生了变化。在传统的action节点上type属性写入action类的完整类名,而和Spring结合后在这点上是使用了Spring提供的DelegatingActionProxy作为action的type属性,DelegatingActionProxy同样是org.apache.struts.action.Action的一个子类,它将把调用请求转交给真正的Action实现。通过这样的方式,Spring获得了Action实例的管理权,它将对Action进行调度,并为Struts提供所需的Action实例。这样,就可以将Action看作是Spring的一个bean,它就可以享受Spring的所有服务,如依赖注入、实例管理、事务管理等。
在applicationContext.xml中相应的配置如下的节点:
- <beans>
- .......
- <bean name="/login"singleton="false">
- <propertyname="userDAO">
- <refbean="userDAOProxy"/>
- </property>
- </bean>
- </beans>
<beans> ....... <bean name="/login" singleton="false"> <property name="userDAO"> <ref bean="userDAOProxy" /> </property> </bean> </beans>
最后这个bean的配置是关键,这个名为“/login”的bean与Struts中的
- <action path="/login" ……>
- ……
- </action>
<action path="/login" ……> …… </action>
节点相对应,这样,Spring Bean Name与Struts Action Path相关联,当Struts加载对应的Action时,DelegatingActionProxy就根据传入的path属性,在Spring Context寻找对应bean,并将其实例返回给Struts。与此同时,还可以看到,"/login" bean 中包含了一个userDAO 引用,Spring 在运行期将根据配置为其提供userDAO 实例,以及围绕userDAO 的事务管理服务。这样一来,对于Struts 开发而言,我们既可以延续Struts 的开发流程,也可以享受Spring 提供的事务管理服务。而bean 的另外一个属性singleton="false",指明了Action 的实例获取方式为每次重新创建。这也解决了Struts中令人诟病的线程安全问题。
第二种方法:
为了在 struts-config.xml 文件中配置 DelegatingRequestProcessor,你需要重载 <controller> 元素的 “processorClass” 属性。 下面的几行应该放在 <action-mapping> 元素的后面。
- <controller>
- <set-propertyproperty="processorClass"value="http://www.zhmy.com/org.springframework.web.struts.DelegatingRequestProcessor"/>
- </controller>
<controller> <set-property property="processorClass"value="http://www.zhmy.com/org.springframework.web.struts.DelegatingRequestProcessor"/> </controller>
增加这些设置之后,不管你查询任何类型的 Action,Sping都自动在它的context配置文件中寻找。 实际上,你甚至不需要指定类型。下面两个代码片断都可以工作:
<action path="/user" type="com.whatever.struts.UserAction"/><action path="/user"/>
如果你使用 Struts 的 modules 特性,你的 bean 命名必须含有 module 的前缀。 举个例子,如果一个 Action 的定义为 <action path="/user"/>,而且它的 module 前缀为“admin”, 那么它应该对应名为 <bean name="/admin/user"/> 的 bean。
如果你在 Struts 应用中使用了 Tiles,你需要配置 <controller> 为 DelegatingTilesRequestProcessor。
如果第二种方法不行,再用第一种方法。至此,SS组合已经将Struts MVC以及Spring中的Bean管理、事务管理融为一体。如果算上userDAO 中的Hibernate 部分,我们就获得了一个全面、成熟、高效、自顶而下的Web 开发框架。
一个简单示例:
下面由我来演示spring + struts的最简单的示例,没有数据库,高手飘过,呵呵。
我用的是myeclipse3.2。
首先建立web工程ssh,加入spring框架,加入struts框架,
增加struts的 action,form,jsp组合,命名为login.
struts-config.xml中action为
<action
attribute="loginForm"
input="/login.jsp"
name="loginForm"
path="/login"
scope="request"
type="org.springframework.web.struts.DelegatingActionProxy" />
注意 type类型由spring来代理,当我们访问"/login"的时候,struts会跟据type类型来查找Spring 环境中的动作,为些,我们还要加载spring环境,可以在struts中加入一个plugin,同样在struts-config.xml中加入
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/beans.xml" />
</plug-in>
这时我们就在struts中加载了spring.
下面就是要把struts 动作注册为spring的bean.
在beans.xml中加入
<bean name="/login"
class="com.hdlb.struts.action.LoginAction">
</bean>
这样,我们就能访问"/login"这个动作了。只不过它由spring代理了。
下面写action
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub
if("test".equals(loginForm.getName())&&("test").equals(loginForm.getPwd())){
System.out.println("登陆成功");
}else{
System.out.println("登陆失败");
}
return null;
}
=========================================
------------------------------------------------
对于struts2和spring整合方式呢:
一 、首先,来看看如何让Spring 来管理Action.
(1)在struts.xml中加入
<constant name="struts.objectFactory" value="spring" />
(2)在web.xml中增加WebApplicationContext的相应配置,以下两种配置方式本质是一样的。
1. Servlet 2.3及以上版本可以使用监听器,相应配置如下:<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/applicationContext.xml</param-value></context-param><listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>如果spring配置文件被命名为applicationContext.xml,并且放在WEB-INF目录下,则不需要配置<context-param>,因为ContextLoaderListener默认在WEB-INF目录下寻找名为applicationContext.xml的文件。若存在多个Spring配置文件,则在<param-value>中依次列出,之间以逗号隔开。
2. Servlet 2.3以下版本由于不支持<listener>,需要配置<servlet>,格式如下:<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/applicationContext.xml</param-value></context-param><servlet> <servlet-name>contextLoaderServlet</servlet-name> <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class> <load-on-startup>1</load-on-startup></servlet>如果spring配置文件被命名为applicationContext.xml,并且放在WEB-INF目录下,则不需要配置<context-param>,因为ContextLoaderListener默认在WEB-INF目录下寻找名为applicationContext.xml的文件,或者是名字为contextConfigLocation的ServletContext参数所指定的文件。由于该Servlet配置只是为了在容器启动时能启动ContextLoaderServlet使其工作,而不需要引用该Servlet,所以不需要配置<servlet-mapping>。
二 、有两种整合方式:
(1) 把Action配置在beans.xml里,利用Spring初始化Action的bean:
将Struts的业务逻辑控制器类配置在Spring的配置文件中,Action中引用的业务类一并注入。
(这样的处理,必须将action类的scope配置成property)
Xml代码
<bean id="LoginAction" class="yaso.struts.action.LoginAction">
<property name="loginDao" ref="LoginDao"/>
</bean>
接着,在struts.xml或者等效的Struts2配置文件中配置Action时,指定<action>的class属性为Spring配置文件中相应bean的id或者name值。示例如下:
Xml代码
<action name=”LoginAction” class=”LoginAction”>
<result name=”success”>/index.jsp</result>
</action>
(2) 不需要在beans.xml里配置Action,利用Struts-Spring_Plugin插件自动初始化Action:
业务类在Spring配置文件中配置,Action不需要配置,Struts2的Action像没有整合Spring之前一样配置,<action>的class属性指定业务逻辑控制器类的全限定名。
Action中引用的业务类不需要自己去初始化,Struts2的Spring插件会使用bean的自动装配将业务类注入进来,其实Action也不是Struts2创建的,而是Struts2的Spring插件创建的。默认情况下,插件使用by name的方式装配,可以通过增加Struts2常量来修改匹配方式:设置方式为:struts.objectFactory.spring.autoWire = typeName,可选的装配参数如下:
name:相当于spring配置的autowrie="byName"(默认)
type:相当于spring配置的autowrie="byType"
auto:相当于spring 配置的autowrie="autodetect"
constructor: 相当于spring配置的autowrie="constructor"
OK,这里说了配置部分,但是,这里有一个问题, 就是Spring管理Action,如果按照第一方式,那么只要通过scope="property"来配置为每个请求创建一个Action实例。 那么第二种方式,我们并没有指定Action的作用域。
(好似也没有地方可配……),那么,这样的整合方式,Action的创建到底是单例还是多例的呢?
答案:也是每个请求一个实例.
----------------------------------
补充:(我在JavaEye上找到struts2-spring-plugin包的作用,不知道对不对,接下来验证)
struts2与spring的整合。导入struts2-spring-plugin包,在web.xml中设置spring的监听器,
spring监听器对应的API类为:org.springframework.web.context.ContextLoaderListener。
struts2-spring-plugin包为我们将struts2的对象工厂设置为spring的IoC容器,其代码为:
<struts>
<bean type="com.opensymphony.xwork2.ObjectFactory" name="spring" class="org.apache.struts2.spring.StrutsSpringObjectFactory" />
<!-- Make the Spring object factory the automatic default -->
<constant name="struts.objectFactory" value="spring" />
<package name="spring-default">
<interceptors>
<interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/>
<interceptor name="sessionAutowiring" class="org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor"/>
</interceptors>
</package>
</struts>
很明显,将struts.objectFactory定位为org.apache.struts2.spring.StrutsSpringObjectFactory
其余的工作就交给spring的IoC容器去做了。
另外:当我们需要增加spring的配置文件时,需要在web.xml中设定contextConfigLocation参数。代码如下:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>其他的spring配置文件名,用逗号隔开</param-value>
</context-param>