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>
web.xml 的加載順序是:context-param -> listener -> filter -> servlet (由於listener先於filter,所以用listener啟動spring)
在struts2配置文件,加入 <constant name="struts.objectFactory" value="spring"></constant> 屬性
5.編寫beans.xml的bean,例如管理sturts中action的bean:
- <bean id="spring" class="org.ymm.actions.LoginAction">
- .......
- </bean>