Tomcat6上的Struts2.1与Spring3.03整合

Tomcat6下的Struts2.1与Spring3.03整合

1、从Struts及Spring库内复制以下文件到工程库(只引入必要的库),全部复制进来时往往会导致框架启动失败。


Tomcat6上的Struts2.1与Spring3.03整合
 2、在Web.xml内加入

<context-param>

<param-name>contextConfigLocation</param-name>

<!-- 如果有多个bean配置文件,有逗号分隔开 -->

<param-value>/WEB-INF/applicationContext.xml</param-value>

</context-param>

<listener>

<!-- 注册Spring的监听器,在程序启动时自动运行。(适合Servlet2.5及以上,早期版本使用servlet配置) -->

<listener-class> org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

3、注意Struts2的配置文件(struts.xml)内指定action时,class指向Spring的配置文件(applicationContext.xml)内的Bean的ID。如:struts.xml片断

<action name="login" class="loginAction">

<!-- 定义三个逻辑视图和物理资源之间的映射 -->

<result name="input">/login.jsp</result>

<result name="error">/error.jsp</result>

<result name="success">/welcome.jsp</result>

</action>

applicationContext.xml片断

<!-- 声明服务组件 -->

<bean id="userService" class="com.cabp.jordy.fxdata.UserService"/>

 

<!-- 配置Struts 2的Action对应的Bean声明 -->

<bean id="loginAction" class="com.cabp.jordy.fxstruts2.LoginAction"

scope="prototype">

<!-- 依赖注入业务组件 -->

<property name="userMgr" ref="userService"/>

</bean>