Spring整合Struts2
①导入Struts2 jar包
②在web.xml文件中创建过滤器
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <!-- 配置struts2 的filter --> <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> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
③创建Struts.xml文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <!-- Spring 整合 Struts2 时, 在 Struts2 中配置的 Spring 的 Action 的 class 需要指向 IOC 容器中该 bean 的 id --> <action name="person-save" class="personAction"> <result>/success.jsp</result> </action> </package> </struts>
④创建PersonService类
public class PersonService { public void save(){ System.out.println("person's save....."); } }
⑤创建PersonAction类
import com.atguigu.spring.struts.service.PersonService; public class PersonAction { private PersonService personService; public void setPersonService(PersonService personService) { this.personService = personService; } public String execute(){ System.out.println("execute....."); return "SUCCESS"; } }
⑥导入Spring的jar包
⑦创建applicationContext.xml文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <bean id="person" class="com.atguigu.spring.struts.beans.Person"> <property name="username" value="spring"></property> </bean> <bean id="personService" class="com.atguigu.spring.struts.service.PersonService"></bean> <!-- 注意在IOC容器中配置Struts2的Action时组要配置scope属性 --> <bean id="personAction" class="com.atguigu.spring.struts.actions.PersonAction" scope="prototype"> <property name="personService" ref="personService"></property> </bean> </beans>
⑧导入一个jar包——struts2-spring-plugin-2.3.15.3.jar
OK!