Spring和Struts整合

1        导入Spring 需要的包

2        applicationContext.xml

3        web.xml中追加

  <listener> 

         <listener-class>

             org.springframework.web.context.ContextLoaderListener

         listener-class>

  listener>

  <context-param>

    <param-name>contextConfigLocationparam-name>

    <param-value>/WEB-INF/applicationContext.xmlparam-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" ?>

<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">

 

   <bean name="/addOrder" scope="prototype"

        class="com.project.action.AddOrderAction">

    <property name="addOrderBLogic">

      <ref local="addOrderBLogic"/>

    property>

  bean>

 

  <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-beans>

    <form-bean name="orderForm"

               type="com.project.form.OrderForm" />

  form-beans>

  <global-exceptions>

  global-exceptions>

 

  <global-forwards>

  global-forwards>

 

  <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 processorClass="org.springframework.web.struts.DelegatingRequestProcessor"/>

 

<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 其中的配置与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

你可能感兴趣的:(JAVA开发)