关于structs和Spring框架整合中的模块装配问题心得
偶采用将Action的创建进行截取,并将其全权委托给Spring框架,当然还有其它诸如ReqestProcesser方法来截获,主要是因为全权委托的方法有很好的扩展性。^_^
我们软件要求的是松耦合,所以在web项目中,Structs框架就不能紧密的依赖dao和Spring。
所以采用,模块组装的方案进行解决,促使耦合减小。
一个简单显示全部用户信息的例子(核心代码,主要是实现全权委托的方案,达到松耦合的效果。考虑到篇幅问题,所以,dao,pojo,数据库代码,show.jsp,dbManager等代码就省略了,)
主要步骤为
1、注册插件(在web.xml文件中注册)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<welcome-file-list>
<welcome-file>show.jsp</welcome-file>
</welcome-file-list>
</web-app>
2、截获类
将structs-config.xml中type属性改为type="org.springframework.web.struts.DelegatingActionProxy" />
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans >
<form-bean name="showForm" type="com.wp.form.ShowForm" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action
attribute="showForm"
input="/show.jsp"
name="showForm"
path="/show"
scope="request"
type="org.springframework.web.struts.DelegatingActionProxy" />
</action-mappings>
<message-resources parameter="com.wp.ApplicationResources" />
<plug-in
className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml" />
</plug-in>
</struts-config>
3、Spring中配置文件中的bean的name属性要和structs中的path属性相同
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="dao" class="dao.UsersDao"></bean>
<bean name="/show" class="com.wp.action.ShowAction">
<property name="dao">
<ref local="dao"/>
</property>
</bean>
</beans>
以上,偶觉得在三大框架整合过程中实现松耦合,是一个很有意思的事情,所以就先把structs和Spring的整合心得,写了下来,对自己所学的内容有所巩固,同时也希望能帮到大家,如果有什么不足之处,还望高手,大虾们指正,偶深表感谢
^_^