1. Jar 文件 整合除了Struts2必须 的jar文件:
> struts2-core-xxx.jar
> xwork-xxx.jar
> ognl-xxx.jar
> freemarker-xxxjar
> commons-logging-xxx.jar
以外,还需要Spring的核心jar文件:
- spring-core.jar
- spring-beans.jar
- spring-context.jar
- spring-web.jar
- struts2-spring-plugin-xxx.jar // Spring与struts2的插件jar
2. 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"
id="WebApp_ID" version="2.5">
<!-- Struts cleanup filter -->
<filter>
<filter-name>struts_cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter-mapping>
<filter-name>struts_cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- struts2 core filter -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring context listener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
3. struts.properties. 位于类路径下,可自行创建,配置如下:
#spring context
struts.objectFactory = spring
4. applicationContext.xml. Spring配置文件,位于WEB-INF目录,可参考:
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="lotteryService" class="com.mkk.struts2.service.LotteryServiceImpl" />
<bean id="springAction" class="com.mkk.struts2.spring.SpringAction"
scope="prototype">
<property name="lotteryService" ref="lotteryService" />
</bean>
</beans>
5. struts.xml. Struts2配置文件,位于类路径下.可参考:
<?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>
<!-- Spring action -->
<package name="spring_package" extends="struts-default" namespace="/spring">
<action name="spring" class="springAction">
<result>/spring/spring.jsp</result>
</action>
</package>
</struts>
注意: action的class值是对应的Spring配置文件中bean的id.
6. 对应的Action类.可参考:
public class SpringAction extends ActionSupport implements InitializingBean {
/**
* serialVersionUID
*/
private static final long serialVersionUID = -1506018657717304870L;
private LotteryService lotteryService;
public String init() throws Exception {
getLottery();
return super.execute();
}
/**
* Lottery action
*
* @return
* @throws Exception
*/
public String lottery() throws Exception {
getLottery();
return SUCCESS;
}
private void getLottery() {
String res = lotteryService.getNextRandomLotteryNumber();
ServletActionContext.getRequest().setAttribute("result", res);
}
public void setLotteryService(LotteryService lotteryService) {
this.lotteryService = lotteryService;
}
public void afterPropertiesSet() throws Exception {
Assert.notNull(lotteryService);
}
}
Ok, 半个SSH2(Spring + Struts2 )整合完毕. 方便自己在工作中查询,也方便 别人.