FCKeditor 与 struts2 上传图片时出错

来源:互联网 发布:轻量级看图软件 编辑:程序博客网 时间:2024/05/01 06:47
最近在struts2用到 FCKeditor,在上传图片时总是出错。
严重: Servlet.service() for servlet SimpleUploader threw exception
java.lang.RuntimeException: Unable to load bean org.apache.struts2.dispatcher.multipart.MultiPartRequest (jakarta) - [unknown location]
在网上找了一下,发现
http://bbs.laoer.com/read-topic-6-ff808081143476140114df232a1f2608-0-1-index-1.html
和我的情况差不多。
跟踪了一下源代码,发现struts2的filter拦截了上传文件的动作,
//org.apache.struts2.dispatcher.Dispatcher.java
public HttpServletRequest wrapRequest(HttpServletRequest request, ServletContext servletContext) throws IOException {
        // don't wrap more than once
        if (request instanceof StrutsRequestWrapper) {
            return request;
        }
        String content_type = request.getContentType();
        if (content_type != null && content_type.indexOf("multipart/form-data") != -1) {
            MultiPartRequest multi = getContainer().getInstance(MultiPartRequest.class);
            request = new MultiPartRequestWrapper(multi, request, getSaveDir(servletContext));
        } else {
            request = new StrutsRequestWrapper(request);
        }
        return request;
    }
在这里又找不到MultiPartRequest.class的实现类。于是出错了。
只要不让struts2拦截处理上传事件,就应该没问题了。
修改web.xml,把原来的
<filter-mapping>
  <filter-name>struts</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
改为
<filter-mapping>
  <filter-name>struts</filter-name>
  <url-pattern>*.action</url-pattern>
 </filter-mapping>
而上传页面调用时直接用文件名调用(如upload.jsp)。struts2只处理 *.action 的请求。
试了一下。和预想的一样,一切OK。
原创粉丝点击