spring框架整合struts2框架有两种方式,一种是由struts创建action另一种是由Spring来创建(推荐使用第二种即没有被注释的代码),两种方式都写在代码中注意区分。整合之前需要前台UI页面,这里使用HM教程中的页面。
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>
<!-- customerAction -->
<package name="customerAction" extends="struts-default" namespace="/">
<!-- struts 创建action
<action name="customer_*" class="web.action.CustomerAction" method="{1}">
</action>
-->
<!-- 由 spring 创建action -->
<action name="customer_*" class="customerAction" method="{1}">
</action>
</package>
</struts>
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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置客户模块 -->
<!-- action类由struts2创建的方法
action类正常编写,需要导入struts2-spring-plugin-2.3.24.jar包
-->
<bean id="customerService" class="service.CustomerServiceImpl">
</bean>
<!-- action类由spring创建的方法
直接配置action bean 其中注入service
然后在struts.xml中action的class直接填入applicationContext.xml中bean的id值
需要注意:配置action bean 需要填写属性scope="prototype" 表示多例,其他一律单例
-->
<bean id="customerAction" class="web.action.CustomerAction" scope="prototype">
<property name="customerService" ref="customerService"></property>
</bean>
</beans>
action类
package web.action;
import javax.servlet.ServletContext;
import org.apache.struts2.ServletActionContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import domain.Customer;
import service.CustomerService;
public class CustomerAction extends ActionSupport implements ModelDriven<Customer>{
private static final long serialVersionUID = -6267988381956295357L;
/*
* 继承ModelDriven需要手动new需要的对象在实现方法中返回
*/
Customer cust = new Customer();
@Override
public Customer getModel() {
return cust;
}
/*
* action由struts2生成的创建
*
* 需要导入struts2-spring-plugin-2.3.24.jar包
* 将service交给spring管理,action中提供注入的属性与set方法即可使用
* !!注意!!
* 成员属性的 名称 要与 applicationContext.xml中bean中的id相同
* 因为导入的struts2-spring-plugin-2.3.24.jar 包自带一个配置文件 struts-plugin.xml ,该配置文件中有如下代码
* <constant name="struts.objectFactory" value="spring" /> 开启一个常量,如果该常量开启,那么下面的常量就可以使用
* struts.objectFactory.spring.autoWire = name,该常量是可以让Action的类来自动装配Bean对象!!
*/
private CustomerService customerService;
public void setCustomerService(CustomerService customerService) {
this.customerService = customerService;
}
public String add() {
System.out.println("web add");
/*
* web工厂获得bean方法,不推荐使用!!!
*
* 在web.xml中 配置了spring全局变量,启动系统时就会加载applicationContext.xml的配置,
* 之后就会创建里面配置的bean,再通过web工厂从容器中取出正常使用
*
* 现在就可以使用WebApplicationContext.getBean()来获得到在srping中配置的customerService,
WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext());
CustomerService cs = (CustomerService) context.getBean("customerService");
cs.add();
*/
//action由struts2生成的创建
customerService.add();
System.out.println(cust);
return NONE;
}
}
测试结果正常