1 导入Spring 需要的包
2 applicationContext.xml
3 在web.xml中追加
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
4 在struts-config.xml中将struts与spring结合的插件包引入:
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/moduleContext.xml"/>
</plug-in>
moduleContext.xml 为Spring 定义Bean的xml文件
以上为整合struts和spring所需要修改的配置
开发实例:
FormBean的定义(OrderForm.Java):
package com.project.form;
import org.apache.struts.action.ActionForm;
public class OrderForm extends ActionForm {
/**
* 注文ID。
*/
private Long orderId = null;
/**
* 荷物名前。
*/
private String orderName = null;
}
Logic接口的定义(AddOrderBLogic.Java):
package com.project.service.blogic;
/**
*注文登録ビジネスロジックのインタフェース。
*
*/
public interface AddOrderBLogic {
public String addOrder(String name);
}
Logic接口实现类的定义(AddOrderBLogicImpl.Java):
package com.project.service.blogic;
public class AddOrderBLogicImpl implements AddOrderBLogic {
public String addOrder(String name) {
return "Hello" + name;
}
}
Action的定义(AddOrderAction.Java):
package com.project.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
importcom.project.service.blogic.AddOrderBLogic;
import com.project.form.OrderForm;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class AddOrderAction extends Action {
private AddOrderBLogic addOrderBLogic = null;
/**
* addOrderBLogicを返却する。
*
* @return 保持するaddOrderBLogic
*/
public AddOrderBLogic getAddOrderBLogic() {
return addOrderBLogic;
}
/**
* addOrderBLogicを設定する。
*
* @param addOrderBLogic addOrderBLogic
*/
public void setAddOrderBLogic(AddOrderBLogic addOrderBLogic) {
this.addOrderBLogic = addOrderBLogic;
}
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws Exception {
OrderForm orderForm = (OrderForm) form;
String returnStr = addOrderBLogic.addOrder(orderForm.getOrderName());
System.out.print(returnStr);
return mapping.findForward("success");
}
}
Jsp文件:
welcome.jsp, addOrder.jsp省
Spring的bean定义文件配置(moduleContext.xml):
<?xml version="1.0" encoding="UTF-8" ?>
<!-- モジュール固有のBean定義 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">
<!-- 共通定義のインポート
<import resource="/xxx.xml"/>
-->
<!-- Action定義 -->
<bean name="/addOrder" scope="prototype"
class="com.project.action.AddOrderAction">
<property name="addOrderBLogic">
<ref local="addOrderBLogic"/>
</property>
</bean>
<!--Logic定義 -->
<bean id="addOrderBLogic" scope="prototype"
class="com.project.service.blogic.AddOrderBLogicImpl">
</bean>
</beans>
Struts-Config.xml的配置:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<!-- Form Bean Definitions -->
<form-beans>
<form-bean name="orderForm"
type="com.project.form.OrderForm" />
</form-beans>
<!-- Global Exception Definitions -->
<global-exceptions>
</global-exceptions>
<!-- Global Forward Definitions -->
<global-forwards>
</global-forwards>
<!-- Global Forward Definitions -->
<action-mappings>
<action path="/addOrder"
type=" org.springframework.web.struts.DelegatingActionProxy"
name="orderForm" scope="request" input="/addOrder.jsp">
<forward name="success" path="/welcome.jsp"/>
</action>
</action-mappings>
<!-- Controller Definitions -->
<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"/>
<!-- Message Resouces Definitions -->
<!-- PlugIn Definitions -->
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/moduleContext.xml"/>
</plug-in>
</struts-config>
以上为Struts-config.xml的完整配置
1 其中<form-bean>的配置与struts的配置完全一样
2 Action的配置存在不同地方如下:
<action path="/addOrder"
type=" org.springframework.web.struts.DelegatingActionProxy"
name="orderForm" scope="request" input="/addOrder.jsp">
<forward name="success" path="/welcome.jsp"/>
</action>
path 对应的值与Spring 中定义对应的Bean的name相同
3 Action中的type属性不同,原来是指向Action对应的类,而现在这对应了org.springframework.web.struts.DelegatingActionProxy 相当设置了一个代理类
Action的配置还有一种方法,就是在Action中不用type属性(因为所有Action的type属性都为:org.springframework.web.struts.DelegatingActionProxy),只需修改一下struts-config.xml中的controller节点的值,如下:
<action path="/addOrder"
name="orderForm" scope="request" input="/addOrder.jsp">
<forward name="success" path="/welcome.jsp"/>
</action>
<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"/>
默认的controller类为:org.apache.struts.action.RequestProcessor ,在Spring中,org.springframework.web.struts.DelegatingRequestProcessor该类继承了org.apache.struts.action.RequestProcessor