(1)将spring相关jar包及struts2-spring-plugin-2.0.11.2.jar拷贝到web-inf/lib下,在web.xml中设置spring监听器、配置文件路径。web.xml代码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>SSM</display-name>
<welcome-file-list>
<welcome-file>/demo/demo.jsp</welcome-file>
</welcome-file-list>
<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>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
(2)在web-inf根目录下新建文件spring.xml,配置要扫描的包路径,及需要注册的实体bean。spring.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 采用注释的方式配置bean -->
<context:annotation-config />
<!-- 该 BeanPostProcessor 将自动对标注 @Autowired 的 Bean 进行注入 -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<!-- 配置要扫描的包 -->
<context:component-scan base-package="com.xy6"></context:component-scan>
<bean id="demoAction" class="com.xy6.DemoAction" />
</beans>
(3)修改struts.xml,将com.xy6.DemoAction替换为demoAction。struts.xml代码:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.objectFactory" value="spring"/>
<package name="demo" extends="struts-default">
<action name="demo" class="demoAction" method="execute">
<result name="success">/demo/demo.jsp</result>
</action>
</package>
</struts>
(4)在包com.xy6下新建类DemoHelper.java,以测试spring的自动注入功能,由于该类使用@Component标记,则无需在spring.xml中注册该实体bean。DemoHelper.java代码:
package com.xy6;
import org.springframework.stereotype.Component;
@Component
public class DemoHelper {
public String getCurYear(){
return "2014";
}
}
(6)在DemoAction类中调用DemoHelper类的getCurYear()方法。DemoAction.java代码:
package com.xy6;
import org.springframework.beans.factory.annotation.Autowired;
import com.opensymphony.xwork2.ActionSupport;
public class DemoAction extends ActionSupport {
public static final long serialVersionUID = 1L;
@Autowired
private DemoHelper demoHelper = new DemoHelper();
public String execute(){
System.out.println("---curren year:"+demoHelper.getCurYear());
return "success";
}
}
(7)重启weblogic域服务,浏览器中访问http://localhost:9001/web/demo.action,页面显示demo.jsp,控制台输出2014。如下图:
(8)至此,spring配置结束。