Spring和Struts整合
Spring和Struts整合
1.添加struts2包
2.在web.xml配置struts2过滤器
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
3.编写struts.xml,写测试应用程序确保验证struts配置正确
- <package name="h" extends="struts-default">
- <action name="login" class="org.ymm.actions.LoginAction" method="mlogin">
- <result name="success" type="redirect">login.jsp</result>
- <result name="fail">fail.jsp</result>
- </action>
- </package>
4.如果struts已经ok,添加spring支持包,配置监听器ContextLoaderListener (让spring先于struts被加载,为后来struts的action被spring管理)
注:必须要加struts2-spring-plugin-2.3.3.jar此包
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:beans.xml</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
在struts2配置文件,加入 <constant name="struts.objectFactory" value="spring"></constant> 属性
5.编写beans.xml的bean,例如管理sturts中action的bean:
- <bean id="spring" class="org.ymm.actions.LoginAction">
- .......
- </bean>
评论暂时关闭