项目之spring+struts结合

公司有两条开发线,一条是老项目,使用的struts;一条线是新的项目开发,用的是spring,但是有一部分开发业务的开发是可以共有的,所以就有了下面的同一个项目里面既有stuts模块,又有spring的模块。

下面我们就来看一下是怎么实现的这一过程吧:
这张图是spring、struts所需要的代码部分


项目结构.pic_hd.jpg

我们再来看看web.xml是怎么加载的

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
    <display-name>mybatis</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:spring/spring.xml,classpath*:spring/spring-mybatis.xml</param-value>
    </context-param>
    <filter>
        <description>字符集过滤器</description>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <description>字符集编码</description>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/</url-pattern>
    </filter-mapping>
    <listener>
        <description>spring监听器</description>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- 防止spring内存溢出监听器 -->
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>
    <!-- spring mvc servlet -->
    <servlet>
        <description>spring mvc servlet</description>
        <servlet-name>springMvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <description>spring mvc 配置文件</description>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:spring/spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springMvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
    <!-- struts -->
        <servlet>
            <servlet-name>action</servlet-name>
            <servlet-class>owen.struts.action.OwenJavaActionServlet</servlet-class>
            <init-param>
                <param-name>config</param-name>
                <param-value>/WEB-INF/struts-config.xml</param-value>
            </init-param>
            <init-param>
                <param-name>debug</param-name>
                <param-value>3</param-value>
            </init-param>
            <init-param>
                <param-name>detail</param-name>
                <param-value>3</param-value>
            </init-param>
            <load-on-startup>0</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>action</servlet-name>
            <url-pattern>*.do</url-pattern>
        </servlet-mapping>
    <!-- struts --> 
    <welcome-file-list>
        <welcome-file>/index.jsp</welcome-file>
    </welcome-file-list>
    <!-- 配置session超时时间,单位分钟 -->
    <session-config>
        <session-timeout>15</session-timeout>
    </session-config>
</web-app>

配置文件中有一个类owen.struts.action.OwenJavaActionServlet,这个类的作用是加载struts的配置文件

package owen.struts.action;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.ServletException;
import javax.servlet.UnavailableException;

import org.apache.struts.action.ActionServlet;
import org.apache.struts.config.ForwardConfig;
import org.apache.struts.config.ModuleConfig;

import owen.utils.FileSearch;

public class OwenJavaActionServlet extends ActionServlet {

    private static final long serialVersionUID = 1L;

    public void init() throws ServletException {
        try {
            initInternal();
            initOther();
            initServlet();
            getServletContext().setAttribute(
                    "org.apache.struts.action.ACTION_SERVLET", this);
            initModuleConfigFactory();

            ModuleConfig moduleConfig = initModuleConfig("", this.config);
            initModuleMessageResources(moduleConfig);
            initModuleDataSources(moduleConfig);
            initModulePlugIns(moduleConfig);
            moduleConfig.freeze();
            String path = getServletContext().getRealPath("WEB-INF");
            FileSearch obj = new FileSearch();
            StringBuffer sb = new StringBuffer();
            String start = "struts-config-";
            String end = ".xml";
            obj.getFilePath(sb, path, start, end);
            String[] filePath = sb.toString().split(";");
            String tmp = "";
            Map map = new HashMap();
            for (int i = 0; i < filePath.length; i++) {
                if (filePath[i] == null || filePath[i].equals(""))
                    continue;
                String tmpPath = "\\WEB-INF" + filePath[i].replace(path, "");
                String prefix = tmpPath.substring(tmpPath.indexOf(start),
                        tmpPath.indexOf(end));
                prefix = "/" + prefix.replace(start, "");
                tmpPath = tmpPath.replace("\\", "/");
                
                //System.out.println("prefix:"+prefix);
                //System.out.println("prefix2:"+prefix.replace(start, ""));
                //System.out.println("tmppath:"+tmpPath);
                if (map.get(prefix) != null) {
                    tmp = String.valueOf(map.get(prefix));
                    map.put(prefix, tmp + "," + tmpPath);
                } else {
                    map.put(prefix, tmpPath);
                }
            }
            Entry entry = null;
            Iterator iter = map.entrySet().iterator();
            while (iter.hasNext()) {
                entry = (Entry) iter.next();
                log.info("load xml file " + entry.getValue().toString());
                moduleConfig = initModuleConfig(String.valueOf(entry.getKey()),
                        String.valueOf(entry.getValue()));
                initModuleMessageResources(moduleConfig);
                initModuleDataSources(moduleConfig);
                initModulePlugIns(moduleConfig);
                moduleConfig.freeze();
            }

            initModulePrefixes(getServletContext());

            destroyConfigDigester();
        } catch (UnavailableException ex) {
            throw ex;
        } catch (Throwable t) {
            t.printStackTrace();
            log.error("Unable to initialize Struts "
                    + "ActionServlet due to an "
                    + "unexpected exception or error " + "thrown, so marking "
                    + "the servlet as unavailable.  "
                    + "Most likely, this is due to an "
                    + "incorrect or missing library dependency.", t);
            throw new UnavailableException(t.getMessage());
        }
    }
}

主要是依赖这些配置文件
那如果想要了解整个源码,已经上传到git了,地址为:
https://gitee.com/toLeftBest/owen-java-spring-struts.git

项目运行之后的样子


struts和spring.pic_hd.jpg
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容