??xml version="1.0" encoding="utf-8" standalone="yes"?>
Spring与Struts集成开?br />
最q喜Ƣ将所学的东西理顺一下,且发现写blog可以达成q目的?br />
那就来整理一下我对Spring与Struts集成开发的一些想法?br />
首先认pȝ的结构ؓ(f)三层的B/S模式l构Q如下图Q?br />
在图中看出,Spring和Struts集成开发中QSpring在业务逻辑层被使用Q集成)(j)。因为Spring框架的依赖注入,AOP?qing)可声明的事务管理方面的技术优势,使得用Spring来管理业务实体,实体之间的依赖关p,业务逻辑服务接口变得单且可配|。至此我们要清楚Q?span style="color: red">Spring和Struts集成开发中QSpring在业务逻辑层被使用Q集成)(j)?/span>
清楚Struts和Spring在系l结构中分别充当的角色后Q接下来要讨论:(x)如何实现集成Q?br />
1、用Spring的ActionSurpertc集成Struts?/strong>
org.springframework.web.struts.ActionSurpert是一个承org.apache.struts.action.Action的类Q简要代码如下:(x)
public abstract class ActionSurpert extends Action {
private WebApplicationContext webApplicationContext;
public void setServlet(ActionServlet actionServlet) {//当容器实例化此Action时被容器调用
surper.setServlet(actionServlet);
if(actionServlet != null) {
this.webApplicationContext = initWebApplicationContext(actionServlet);//获取webApplicationContext
//........
}else{
//.......
}
}
//通过getXXX()Ҏ(gu)获取 webApplicationContext 对象
protected final WebApplictionContext getWebApplicationContext() {
return this.webApplicationContext;
}
}
通过上述代码可以看出Q所有承了(jin)ActionSupportcȝAction可以通过WebApplicationContext对象的getBean(beanKey)Ҏ(gu)获得Spring配置文g中定义的各种Bean?br />
WebApplicationContext要由Web Server来加载,有两U方法:(x)
Q、通过org.springframework.web.struts.ContextLoaderPlugIn加蝲QContextLoaderPlugIn是个插gQ需要在Struts配置文g中配|?br />
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property = "contextConfigLocation" value="/WEB-INF/applicationContext.xml"></set-property>
</plug-in>
2、在web.xml文g中加?br />
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-name>
<load-on-startup>1</load-on-startup>
</servlet>
举个单的例子说明一下相关配|信息:(x)
假定有ExampleActionQExampleBeanQExampleServiceq几个类Q它们工作流E是Q?br />
用户hExampleActionQExampleAction调用ExampleService的接口方法对ExampleBeanq行相关的操作?br />
1、applicationContext.xml中配|相关的bean信息
<beans>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/ssh?useUnicode=true&characterEncoding=UTF-8</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>root</value>
</property>
</bean>
<bean id="exampleBean" class="org.mypackge.beans.ExampleBean"/>
<bean id="exampleBeanService" class="org.mypackge.services.ExampleService"/>@
</beans>
通过q样配置后,在ExampleAction中可以用getWebApplicationContext() 获得webApplicationContext对象Q然后通过
webApplicationContext的getBean(beanKey)Ҏ(gu)获得相应的beanq行业务处理。标?jin)红色?beanKey"是applicationContext.xml?lt;bean>元素定义的bean idQ如:webApplicationContext.getBean("exampleBeanService")。@
当然QExampleActionq要在stuts-config.xml配置文g中配|,q里不作介绍?br />
2、用Spring的Action代理集成Struts
q种集成方式的核?j)思想是,Struts的配|文件中的所有Action的type属性设为org.springframwork.web.struts.DelegationActionProxy。当用户hActionӞ执行这代理Q代理会(x)在Spring应用上下文中扑ֈ真正的ActionQ然后交l它处理用户的请求。而真正用于处理用戯求的Action的配|放在了(jin)Spring的配|文件中。这PStruts中的Action以及(qing)其依赖关pd可以用Spring容器来管理,比如业务逻辑对象注入到Action中,供其使用。简单片D?lt;bean name="/exampleAction" class="org.myproj.struts.actions.ExampleAction">
....
</bean>
说明Q用name属性而不是用id来标识这个Bean,Spring不允许ID中出?/"Q而name可以Q?name"属性D和struts-config.xml文g中相?lt;action>元素中的path属性值相?span style="color: #ff99cc">(<action path="/exampleAction")Q这样Action代理才能扑ֈ相应的Action来处理请求?br />
关于Spring和Struts的集?q有W三U方?在下一文?Spring+Struts+Hibernate集成应用实例"中介l?br />
Ƣ迎讨论Q提出宝贉|见?br />