项目之spring+struts结合

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

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


项目之spring+struts结合_第1张图片
项目结构.pic_hd.jpg

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



    mybatis
    
        contextConfigLocation
        classpath*:spring/spring.xml,classpath*:spring/spring-mybatis.xml
    
    
        字符集过滤器
        encodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            字符集编码
            encoding
            UTF-8
        
    
    
        encodingFilter
        /
    
    
        spring监听器
        org.springframework.web.context.ContextLoaderListener
    
    
    
        org.springframework.web.util.IntrospectorCleanupListener
    
    
    
        spring mvc servlet
        springMvc
        org.springframework.web.servlet.DispatcherServlet
        
            spring mvc 配置文件
            contextConfigLocation
            classpath*:spring/spring-mvc.xml
        
        1
    
    
        springMvc
        /
    
    
    
        
            action
            owen.struts.action.OwenJavaActionServlet
            
                config
                /WEB-INF/struts-config.xml
            
            
                debug
                3
            
            
                detail
                3
            
            0
        
    
        
            action
            *.do
        
     
    
        /index.jsp
    
    
    
        15
    

配置文件中有一个类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

项目运行之后的样子


项目之spring+struts结合_第2张图片
struts和spring.pic_hd.jpg

你可能感兴趣的:(项目之spring+struts结合)