struts2和fck冲突造成无法上传图片或文件问题解决办法

struts2和fck冲突造成无法上传图片或文件问题解决办法
fck 在 struts2上不能上传文件的问题,struts2和fck冲突造成无法上传图片或文件,
原因是struts在web.xml中配制的过滤器把fck的servlet请求转发了。
解决办法是自实现一个过滤器ExtendStrutsFilter,继承struts2默认的过滤器StrutsPrepareAndExecuteFilter
用这个类替换掉web.xml中的struts过滤器类StrutsPrepareAndExecuteFilter就可以了

源码如下:

package com.sinowell.fckeditor;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter;

/**
* @author Rain
* 这个类用来解决struts2和fck冲突造成无法上传图片或文件 的问题,原
* 因是struts在web.xml中配制的过滤器把fck的servlet请求转发了。
* 用这个类替换掉web.xml中的struts过滤器类StrutsPrepareAndExecuteFilter就可以了
*/
public class ExtendStrutsFilter extends StrutsPrepareAndExecuteFilter {
   
    @Override
    public void doFilter(ServletRequest req, ServletResponse res,FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        //不过滤的url,可以不断添加,如fck可用/fckeditor/editor/filemanager/connectors/fileupload,下面的是flex
        if (request.getRequestURI().indexOf("/fckeditor/editor/filemanager/connectors")>0) {
            //System.out.println("使用自定义的过滤器");
            chain.doFilter(req, res);
        }else{
            //System.out.println("使用默认的过滤器");
            super.doFilter(req, res, chain);
        }
    }
}

在web.xml中:
<!-- struts -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>com.sinowell.fckeditor.ExtendStrutsFilter</filter-class><!-- 替换掉默认的过滤器org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter -->
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>




参考:StrutsPrepareAndExecuteFilter学习笔记

package org.apache.struts2.dispatcher.ng.filter;

import org.apache.struts2.StrutsStatics;
import org.apache.struts2.dispatcher.Dispatcher;
import org.apache.struts2.dispatcher.ng.PrepareOperations;
import org.apache.struts2.dispatcher.ng.ExecuteOperations;
import org.apache.struts2.dispatcher.ng.InitOperations;
import org.apache.struts2.dispatcher.mapper.ActionMapping;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* Handles both the preparation and execution phases of the Struts dispatching process.  This filter is better to use
* when you don't have another filter that needs access to action context information, such as Sitemesh.
*/
public class StrutsPrepareAndExecuteFilter implements StrutsStatics, Filter {
    private PrepareOperations prepare;
    private ExecuteOperations execute;
//初始化过滤器
    public void init(FilterConfig filterConfig) throws ServletException {
        InitOperations init = new InitOperations(); //初始化辅助对象,封装了初始化的一些操作
        try {
            FilterHostConfig config = new FilterHostConfig(filterConfig); //对filterConfig进行封装
            init.initLogging(config); //通过config,初始化内部Struts的记录

            Dispatcher dispatcher = init.initDispatcher(config); //通过config,创建并初始化dispatcher

            init.initStaticContentLoader(config, dispatcher); //通过config和dispatcher,初始化与过滤器相关的静态内容加载器

            prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher); //通过config和dispatcher,创建request被处理前的系列操作对象
            execute = new ExecuteOperations(filterConfig.getServletContext(), dispatcher);//通过config和dispatcher,创建处理request的系列操作对象

        } finally {
            init.cleanup(); //清空ActionContext
        }

    }

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;

        try {
            prepare.createActionContext(request, response); //创建ACTIONCONTEXT,并初始化Theadlocal
            prepare.assignDispatcherToThread(); //指派dispatcher给Theadlocal
            prepare.setEncodingAndLocale(request, response); //设置request的编码和LOCAL
            request = prepare.wrapRequest(request); //封装request
            ActionMapping mapping = prepare.findActionMapping(request, response); //查找并选择创建ActionMapping
            if (mapping == null) { //如果映射不存在
                boolean handled = execute.executeStaticResourceRequest(request, response); //试图执行一个静态资源的请求
                if (!handled) {
                    chain.doFilter(request, response);
                }
            } else { //如果存在映射
                execute.executeAction(request, response, mapping); //执行action
            }
        } finally {
            prepare.cleanupRequest(request); //清除request的Threadlocal
        }
    }

    public void destroy() {
        prepare.cleanupDispatcher();
    }
}

你可能感兴趣的:(struts fck)