zoukankan      html  css  js  c++  java
  • spring 整合 struts2 xml配置

    整合之前要搞清楚struts2是什么;

      struts2:表现层框架  增删改查  作用域  页面跳转   异常处理  ajax 上传下载  excel   调用service

      spring :IOC/DI:bean容器   aop

    整合点:

     1.struts2的所有对象 交由spring来管理  意味着struts.xml内对象生成方式要发生改变

     2.初始化容器的时机  服务器启动时完成

        spring已有方案解决

    整合步骤:

            1.建web 工程 copy jar包

         2.copy     struts.xml

                        a:<constant name="struts.objectFactory" value="spring"/>这个是说明struts的对象交于spring来处理

             b:action元素 class 属性的值 不再写包名+类名  而是写spring配置文件中的bean的id值

            3.copy web.xml

          监听器   启动时加载   spring的配置文件

          由于监听器默认 读WEB-INF 下面名字为 applicationContext.xml的配置文件,不能读其它文件

          所以需要再加一个配置:       

            <context-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>classpath:applicationContext*.xml</param-value>
            </context-param>

          意思是读取src下所有以applicationContext开头的文件

      web.xml:    

      <?xml version="1.0" encoding="UTF-8"?>
      <web-app id="WebApp_9" version="2.4"
        xmlns="http://java.sun.com/xml/ns/j2ee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
          <filter>
            <filter-name>struts2</filter-name>
            <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
          </filter>
          <filter-mapping>
            <filter-name>struts2</filter-name>
            <url-pattern>/*</url-pattern>
          </filter-mapping>
          <listener>
            <!-- 此监听器默认读WEB-INF 下的applicationContext.xml 文件 启动时加载我们的spring ioc 容器 -->
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
          </listener>
          <!-- context-param 元素由 ServletContext对象解析 意思是读取src下 以applicationContext开头的所有文件 -->
          <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext*.xml</param-value>
          </context-param>
      </web-app>

    action:

    public class TestAction {
      public String execute(){
        System.out.println("============TestAction===========");
        return "success";
      }
    }

    applicationContext-action.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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd" >
      <bean id="testAction" class="com.huawei.s2s.action.TestAction" scope="prototype" >
      </bean>
    </beans>

    struts.xml:

    <?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>
      <constant name="struts.objectFactory" value="spring"/>
      <package name="default" namespace="/" extends="struts-default">
        <action name="testAction" class="testAction" ><--注意这里class的值-->
          <result name="success">/index.jsp</result>
        </action>
      </package>
    </struts>

  • 相关阅读:
    coredns bug
    Android的Sepolicy
    漫谈fork
    ftrace总结
    Framebuffer
    .net core 5 发送windows10桌面通知
    test_app 测试环境搭建
    GitHub骚操作
    git基于某分支创建新分支
    mysql导入数据load data infile
  • 原文地址:https://www.cnblogs.com/hwgok/p/5451143.html
Copyright © 2011-2022 走看看