Spring和Struts整合

本文介绍如何整合Struts与Spring框架,包括配置web.xml、struts-config.xml等文件,定义FormBean、Logic接口及其实现类、Action等内容,并提供完整的配置示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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中将strutsspring结合的插件包引入:

<plug-in  className="org.springframework.web.struts.ContextLoaderPlugIn">

    <set-property property="contextConfigLocation"

                  value="/WEB-INF/moduleContext.xml"/>

</plug-in>

 

moduleContext.xml Spring 定义Beanxml文件

 

以上为整合strutsspring所需要修改的配置

   

开发实例:

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

Springbean定义文件配置(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 中定义对应的Beanname相同

 

3 Action中的type属性不同,原来是指向Action对应的类,而现在这对应了org.springframework.web.struts.DelegatingActionProxy  相当设置了一个代理类

 

Action的配置还有一种方法,就是在Action中不用type属性(因为所有Actiontype属性都为: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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值