Spring与Struts2集成其实很简单, 跟我来...
第一步, 新建基于Maven的Web工程
第二步, 在Maven配置文件中添加依赖库
注: 加入struts2-spring-plugin:2.3.4.1之后, Spring和Struts2配置文件中,不需要进行任何特殊的配置,Struts2的struts.objectFactory将默认使用org.apache.struts2.spring.StrutsSpringObjectFactory, 这样Action类中可以直接使用@Autowired等注解来注入Spring中管理的Bean!
第三步, 新建一个Struts2的Action控制器类
HelloAction.java
package com.dmo.action;
import java.io.IOException;
import java.util.Date;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.dmo.service.HelloService;
@Controller
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class HelloAction {
@Autowired
private HelloService helloService;
private String param1;
public void sayHi() throws IOException {
StringBuffer html = new StringBuffer();
html.append("<html><hr/>");
html.append("MSG: " + helloService.sayHello(new Date().toString()));
html.append("<br/>");
html.append("This:" + this);
html.append("<br/>");
html.append("Param1:" + param1);
html.append("<hr/></html>");
ServletActionContext.getResponse().getWriter().println(html.toString());
}
public String getParam1() {
return param1;
}
public void setParam1(String param1) {
this.param1 = param1;
}
}
注: Struts2默认每次请求都会创建一个Action实例, 而通过Spring创建的Action默认是单例的, 单实例的Action是非线程安全的, 需要添加注解@Scope (ConfigurableBeanFactory.SCOPE_PROTOTYPE) , 将每次请求时都会创建一个Action实例, 大家可以通过访问查看页面中This, 和 Param1 的输出在添加注解@Scope 之前和之后有何不同!
第四步, 新建一个Spring管理的Service接口极其实现
HelloService.java
package com.dmo.service;
public interface HelloService {
public String sayHello(String msg);
}
HelloServiceImpl.java
package com.dmo.service.impl;
import org.springframework.stereotype.Service;
import com.dmo.service.HelloService;
@Service
public class HelloServiceImpl implements HelloService {
public String sayHello(String msg) {
return "Hello " + msg + " !";
}
}
第五步, 添加Spring及Struts2配置文件,及配置web.xml!
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:jee="http://www.springframework.org/schema/jee" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" 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 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd"> <!-- // Components Scan --> <context:component-scan base-package="com.dmo" /> </beans>
strtus.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> <package name="default" namespace="/" extends="struts-default"></package> <include file="struts-hello.xml" /> </struts>
struts-hello.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="hello" namespace="/hello" extends="default">
<!--// 类全路径
每次请求都会创建一个Action实例
-->
<action name="helloAction_*" method="{1}"
class="com.dmo.action.HelloAction" converter=""/>
<!--// Spring管理的Bean
默认Spring创建的Action是单例的,非线程安全的, 在Struts2环境下并不适合,
需要在Action类前添加注解:@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)来取消单实例模式
-->
<action name="helloAction2_*" method="{1}" class="helloAction" />
</package>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>ToDoMindManager</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:/META-INF/spring/applicationContext.xml </param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <init-param> <param-name>actionPackages</param-name> <param-value>com.dmo.action</param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
第六步, 通过Maven启动集成的Tomcat进行测试
启动Tomcat : mvn tomcat:run
测试地址 :
http://localhost:8080/SpringStruts2Integration/hello/helloAction_sayHi.action
http://localhost:8080/SpringStruts2Integration/hello/helloAction2_sayHi.action
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
注: 由于项目中引入接struts2-config-browser-plugin:2.3.4.1包
通过下面这个地址可以查看项目中配置的所有Action的情况:
http://localhost:8080/SpringStruts2Integration/config-browser/index