启天JAVA培训社区's Archiver

junsan 发表于 2008-9-7 12:42

Struts 2整合Spring

Struts 2框架为整合其他技术提供了良好的可扩展性,可以通过插件的方式来实现同Spring技术的整合。

[size=3][font=Times New Roman]Struts 2[/font][font=宋体]框架整合[/font][font=Times New Roman]Spring[/font][font=宋体]很简单,下面是整合的步骤。[/font][/size]
[size=3][font=宋体]([/font][font=Times New Roman]1[/font][font=宋体])复制文件。复制[/font][font=Times New Roman]struts2-spring-plugin-x-x-x.jar[/font][font=宋体]和[/font][font=Times New Roman]spring.jar[/font][font=宋体]到[/font][font=Times New Roman]WEB-INF/lib[/font][font=宋体]目录下。其中的[/font][font=Times New Roman]x[/font][font=宋体]对应了[/font][font=Times New Roman]Spring[/font][font=宋体]的版本号。还需要复制[/font][font=Times New Roman]commons-logging.jar[/font][font=宋体]文件到[/font][font=Times New Roman]WEB-INF/lib[/font][font=宋体]目录下。[/font][/size]
[size=3][font=宋体]([/font][font=Times New Roman]2[/font][font=宋体])配置[/font][font=Times New Roman]struts.objectFactory[/font][font=宋体]属性值。在[/font][font=Times New Roman]struts.properties[/font][font=宋体]中设置[/font][font=Times New Roman]struts.objectFactory[/font][font=宋体]属性值:[/font][/size]
[font=Courier New][size=2]struts.objectFactory = spring[/size][/font]
[size=3][font=宋体]或者在[/font][font=Times New Roman]XML[/font][font=宋体]文件中进行常量配置:[/font][/size]
[font=Courier New][size=2]<struts>[/size][/font]
[font=Courier New][size=2]     
<constant name="struts.objectFactory" value="spring" />[/size][/font]
[font=Courier New][size=2]</struts>[/size][/font]
[size=3][font=宋体]([/font][font=Times New Roman]3[/font][font=宋体])配置[/font][font=Times New Roman]Spring[/font][font=宋体]监听器。在[/font][font=Times New Roman]web.xml[/font][font=宋体]文件中增加如下内容:[/font][/size]
[font=Courier New][size=2]<listener>[/size][/font]
[font=Courier New][size=2]     
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>[/size][/font]
[font=Courier New][size=2]</listener>[/size][/font]
[size=3][font=宋体]([/font][font=Times New Roman]4[/font][font=宋体])[/font][font=Times New Roman]Spring[/font][font=宋体]配置文件。默认情况下,[/font][font=Times New Roman]Spring[/font][font=宋体]配置文件为[/font][font=Times New Roman]applicationContext.xml[/font][font=宋体],该文件需要保存在[/font][font=Times New Roman]Web[/font][font=宋体]应用的[/font][font=Times New Roman]WEB-INF[/font][font=宋体]目录下。内容示例如下所示:[/font][/size]
[font=Courier New][size=2]<?xml version="1.0" encoding="UTF-8"?>[/size][/font]
[font=Courier New][size=2]<!DOCTYPE beans PUBLIC [/size][/font]
[font=Courier New][size=2]     
"-//SPRING//DTD BEAN//EN"[/size][/font]
[font=Courier New][size=2]     
"http://www.springframework.org/dtd/spring-beans.dtd">[/size][/font]
[font=Courier New][size=2]<beans default-autowire="byName">[/size][/font]
[font=Courier New][size=2]     
<bean id="personManager" class="com.acme.PersonManager"/>[/size][/font]
[font=Courier New][size=2]</beans>[/size][/font]
[size=3][font=宋体]开发者实际上可以使用多个[/font][font=Times New Roman]Spring[/font][font=宋体]配置文件,在[/font][font=Times New Roman]web.xml[/font][font=宋体]中进行下列设置,从而使[/font][font=Times New Roman]Spring[/font][font=宋体]的[/font][font=Times New Roman]ApplicationContext[/font][font=宋体]通过匹配所给定模式的文件来初始化对象:[/font][/size]
[size=2][font=Courier New]<!-- [/font][font=宋体]用来定位[/font][font=Courier New]Spring XML[/font][font=宋体]文件的上下文配置[/font][font=Courier New] -->[/font][/size]
[font=Courier New][size=2]<context-param>[/size][/font]
[font=Courier New][size=2]     
<param-name>contextConfigLocation</param-name>[/size][/font]
[font=Courier New][size=2]     
<param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>[/size][/font]
[font=Courier New][size=2]</context-param>[/size][/font]
[size=3][font=宋体]([/font][font=Times New Roman]5[/font][font=宋体])修改[/font][font=Times New Roman]Struts[/font][font=宋体]配置文件。[/font][font=Times New Roman]Struts 2[/font][font=宋体]框架整合[/font][font=Times New Roman]Spring[/font][font=宋体]框架,需要在[/font][font=Times New Roman]Struts[/font][font=宋体]配置文件中有所改变,下面是一个示例:[/font][/size]
[font=Courier New][size=2]<!DOCTYPE struts PUBLIC[/size][/font]
[font=Courier New][size=2]     
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"[/size][/font]
[font=Courier New][size=2]     
"http://struts.apache.org/dtds/struts-2.0.dtd">[/size][/font]
[font=Courier New][size=2]<struts>[/size][/font]
[font=Courier New][size=2]     
<include file="struts-default.xml"/>[/size][/font]
[font=Courier New][size=2]     
<package name="default" extends="struts-default">[/size][/font]
[font=Courier New][size=2]         
<action name="foo" class="com.acme.Foo">[/size][/font]
[font=Courier New][size=2]            
<result>foo.ftl</result>[/size][/font]
[font=Courier New][size=2]         
</action>[/size][/font]
[font=Courier New][size=2]     
</package>[/size][/font]
[font=Courier New][size=2]     
<package name="secure" namespace="/secure" extends="default">[/size][/font]
[font=Courier New][size=2]         
<action name="bar" class="bar">[/size][/font]
[font=Courier New][size=2]            
<result>bar.ftl</result>[/size][/font]
[font=Courier New][size=2]         
</action>[/size][/font]
[font=Courier New][size=2]     
</package>[/size][/font]
[font=Courier New][size=2]</struts>[/size][/font]
[size=3][font=宋体]该配置文件中定义了两个[/font][font=Times New Roman]Action[/font][font=宋体]配置:[/font][font=Times New Roman]foo[/font][font=宋体]是一个标准的[/font][font=Times New Roman]Struts 2[/font][font=宋体]框架[/font][font=Times New Roman]Action[/font][font=宋体]配置,指定了[/font][font=Times New Roman]Action[/font][font=宋体]实现类为[/font][font=Times New Roman]com.acme.Foo[/font][font=宋体];[/font][font=Times New Roman]bar[/font][font=宋体]对应的[/font][font=Times New Roman]class[/font][font=宋体]并不存在,那么框架将在[/font][font=Times New Roman]Spring[/font][font=宋体]配置文件中查找[/font][font=Times New Roman]id[/font][font=宋体]属性为“[/font][font=Times New Roman]bar[/font][font=宋体]”的定义,该配置文件如下所示:[/font][/size]
[font=Courier New][size=2]<?xml version="1.0" encoding="UTF-8"?>[/size][/font]
[font=Courier New][size=2]<!DOCTYPE beans PUBLIC [/size][/font]
[font=Courier New][size=2]     
"-//SPRING//DTD BEAN//EN"[/size][/font]
[font=Courier New][size=2]     
"http://www.springframework.org/dtd/spring-beans.dtd">[/size][/font]
[font=Courier New][size=2]<beans default-autowire="byName">[/size][/font]
[font=Courier New][size=2]     
<bean id="bar" class="com.my.BarClass" singleton="false"/>[/size][/font]
[font=Courier New][size=2]     
...[/size][/font]
[font=Courier New][size=2]</beans>[/size][/font]

junsan 发表于 2008-9-7 12:43

[size=3][font=Times New Roman]Struts2[/font][font=宋体]与[/font][font=Times New Roman]Spring[/font][font=宋体]的集成要用到[/font][font=Times New Roman]Spring[/font][font=宋体]插件包[/font][font=Times New Roman]struts2-spring-plugin-x-x-x.jar[/font][font=宋体],这个包是同[/font][font=Times New Roman]Struts2[/font][font=宋体]一起发布的。[/font][font=Times New Roman]Spring[/font][font=宋体]插件是通过覆盖([/font][font=Times New Roman]override[/font][font=宋体])[/font][font=Times New Roman]Struts2[/font][font=宋体]的[/font][font=Times New Roman]ObjectFactory[/font][font=宋体]来增强核心框架对象的创建。当创建一个对象的时候,它会用[/font][font=Times New Roman]Struts2[/font][font=宋体]配置文件中的[/font][font=Times New Roman]class[/font][font=宋体]属性去和[/font][font=Times New Roman]Spring[/font][font=宋体]配置文件中的[/font][font=Times New Roman]id[/font][font=宋体]属性进行关联,如果能找到,则由[/font][font=Times New Roman]Spring[/font][font=宋体]创建,否则由[/font][font=Times New Roman]Struts 2[/font][font=宋体]框架自身创建,然后由[/font][font=Times New Roman]Spring[/font][font=宋体]来装配。[/font][font=Times New Roman]Spring[/font][font=宋体]插件具体有如下几个作用:[/font][/size]
[size=3]—[/size]
[size=3][font=宋体]允许[/font][font=Times New Roman]Spring[/font][font=宋体]创建[/font][font=Times New Roman]Action[/font][font=宋体]、[/font][font=Times New Roman]Interceptror[/font][font=宋体]和[/font][font=Times New Roman]Result[/font][font=宋体]。[/font][/size]
[size=3]—[/size]
[size=3][font=宋体]由[/font][font=Times New Roman]Struts[/font][font=宋体]创建的对象能够被[/font][font=Times New Roman]Spring[/font][font=宋体]装配。[/font][/size]
[size=3]—[/size]
[size=3][font=宋体]如果没有使用[/font][font=Times New Roman]Spring ObjectFactory[/font][font=宋体],提供了[/font][font=Times New Roman]2[/font][font=宋体]个拦截器来自动装配[/font][font=Times New Roman]action[/font][font=宋体]。[/font][/size]
[size=3][/size][font=宋体][size=11pt]★[/size][/font][size=11pt][font=Times New Roman] [/font][/size][font=黑体][size=11pt]注意[/size][/font][size=11pt][font=Times New Roman] [/font][/size][font=宋体][size=11pt]★[/size][/font][size=11pt][/size]
[size=3][font=Times New Roman][font=方正楷体简体]开发者不必在[/font]Spring[font=方正楷体简体]中去注册[/font]action[font=方正楷体简体],尽管可以这么去做,通常[/font]Struts[font=方正楷体简体]框架会自动地从[/font]action mapping[font=方正楷体简体]中创建[/font]action[font=方正楷体简体]对象。[/font][/font][/size]
[font=Times New Roman][size=3] [/size][/font]
[size=3][font=Times New Roman]struts2-spring-plugin-x-x-x.jar[/font][font=宋体]插件中有一个[/font][font=Times New Roman]struts-plugin.xml[/font][font=宋体]文件,该文件内容如下所示:[/font][/size]
[font=Courier New][size=2]<!DOCTYPE struts PUBLIC[/size][/font]
[size=2][font=Courier New]
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"[/font][/size]
[size=2][font=Courier New]
"http://struts.apache.org/dtds/struts-2.0.dtd">[/font][/size]
[font=Courier New][size=2]<struts>[/size][/font]
[size=2][font=Courier New]
<bean type="com.opensymphony.xwork2.ObjectFactory" name="spring" class="org.apache.struts2.spring.StrutsSpringObjectFactory" />[/font][/size]
[font=Courier New][size=2]    [/size][/font]
[size=2][font=Courier New]
<!--
[/font][font=宋体]设置[/font][font=Courier New]Spring[/font][font=宋体]对象工厂为自动[/font][font=Courier New] -->[/font][/size]
[size=2][font=Courier New]
<constant name="struts.objectFactory" value="spring" />[/font][/size]
[size=2][font=Courier New]
<package name="spring-default">[/font][/size]
[size=2][font=Courier New]
<interceptors>[/font][/size]
[size=2][font=Courier New]

<interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/>[/font][/size]
[size=2][font=Courier New]

<interceptor name="sessionAutowiring" class="org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor"/>[/font][/size]
[size=2][font=Courier New]
</interceptors>[/font][/size]
[size=2][font=Courier New]
</package>
[/font][/size]
[font=Courier New][size=2]</struts>[/size][/font]
[size=3][font=宋体]其中设置了[/font][font=Times New Roman]Struts 2[/font][font=宋体]框架常量[/font][font=Times New Roman]struts.objectFactory[/font][font=宋体]的值为[/font][font=Times New Roman]spring[/font][font=宋体],实际上,[/font][font=Times New Roman]spring[/font][font=宋体]是[/font][font=Times New Roman]org.apache.struts2.spring.StrutsSpringObjectFactory[/font][font=宋体]类的缩写,默认情况下所有由[/font][font=Times New Roman]Struts 2[/font][font=宋体]框架创建的对象都是由[/font][font=Times New Roman]ObjectFactory[/font][font=宋体]实例化的,[/font][font=Times New Roman]ObjectFactory[/font][font=宋体]提供了与其他[/font][font=Times New Roman]IoC[/font][font=宋体]容器如[/font][font=Times New Roman]Spring[/font][font=宋体]、[/font][font=Times New Roman]Pico[/font][font=宋体]等集成的方法。覆盖这个[/font][font=Times New Roman]ObjectFactory[/font][font=宋体]的类必须继承[/font][font=Times New Roman]ObjectFactory[/font][font=宋体]类或者它的任何子类,并且要带有一个不带参数的构造方法。在这里用[/font][font=Times New Roman]org.apache.struts2.spring.StrutsSpring ObjectFactory[/font][font=宋体]代替了默认的[/font][font=Times New Roman]ObjectFactory[/font][font=宋体]。[/font][/size]
[size=3][font=宋体]如果[/font][font=Times New Roman]Action[/font][font=宋体]不是使用[/font][font=Times New Roman]Spring ObjectFactory[/font][font=宋体]创建的话,插件提供了两个拦截器来自动装配[/font][font=Times New Roman]Action[/font][font=宋体],默认情况下框架使用的自动装配策略是[/font][font=Times New Roman]name[/font][font=宋体],也就是说框架会去[/font][font=Times New Roman]Spring[/font][font=宋体]中寻找与[/font][font=Times New Roman]Action[/font][font=宋体]属性名字相同的[/font][font=Times New Roman]bean[/font][font=宋体],可选的装配策略还有:[/font][font=Times New Roman]type[/font][font=宋体]、[/font][font=Times New Roman]auto[/font][font=宋体]、[/font][font=Times New Roman]constructor[/font][font=宋体],开发者可以通过常量[/font][font=Times New Roman]struts.objectFactory.spring.autoWire[/font][font=宋体]来进行设置。[/font][/size]
[size=3][/size][font=宋体][size=11pt]★[/size][/font][size=11pt][font=Times New Roman] [/font][/size][font=黑体][size=11pt]说明[/size][/font][size=11pt][font=Times New Roman] [/font][/size][font=宋体][size=11pt]★[/size][/font][size=11pt][/size]
[size=3][font=Times New Roman]Struts 2[font=方正楷体简体]框架整合[/font]Spring[font=方正楷体简体]后,处理用户请求的[/font]Action[font=方正楷体简体]并不是[/font]Struts[font=方正楷体简体]框架创建的,而是由[/font]Spring[font=方正楷体简体]插件创建的。创建实例时,不是利用配置[/font]Action[font=方正楷体简体]时指定的[/font]class[font=方正楷体简体]属性值,根据[/font]bean[font=方正楷体简体]的配置[/font]id[font=方正楷体简体]属性,从[/font]Spring[font=方正楷体简体]容器中获得相应的实例。[/font][/font][/size]

页: [1]

Powered by Discuz! Archiver 6.1.0F  © 2001-2007 Comsenz Inc.