天天看點

springMVC -- 對接UEditor(富文本編輯器)

工作中需要用到UEditor編輯文本,在與springMVC進行整合時,出現了一些問題,結果導緻,在進行圖檔上傳時出現如下提示:

springMVC -- 對接UEditor(富文本編輯器)

上網查詢了很多相關資料,此處簡要記錄下,防止以後遇到類似問題。

 一種方式是直接修改源碼,步驟如下:

1、編寫controller 如下(該接口是ueditor前背景互動的統一路徑) :

package com.test.dcdp.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.baidu.ueditor.ActionEnter;

@Controller
@RequestMapping("/ueditor")
public class UeditorController {

    @RequestMapping("/dispatch")
    public void config(HttpServletRequest request,  HttpServletResponse response) {
           // response.setContentType("application/json");              
            String rootPath = request.getSession().getServletContext().getRealPath("/");
            response.setHeader("Content-Type" , "text/html");
            try {
                String a = request.getRequestURI();
                    String exec = new ActionEnter(request, rootPath).exec();
                    PrintWriter writer = response.getWriter();
                    writer.write(exec);
                    writer.flush();
                    writer.close();
            } catch (IOException e) {
                    e.printStackTrace();
            }
            
    }
}      

2、修改ueditor的配置檔案 ueditor.config.js(指定背景伺服器位址),如下所示

修改前:

var URL = window.UEDITOR_HOME_URL || getUEBasePath();

    /**
     * 配置項主體。注意,此處所有涉及到路徑的配置别遺漏URL變量。
     */
    window.UEDITOR_CONFIG = {

        //為編輯器執行個體添加一個路徑,這個不能被注釋
        UEDITOR_HOME_URL: URL

        // 伺服器統一請求接口路徑
        , serverUrl: URL + "php/controller.php"      

修改後 :

var getRootPath = function (){
        //擷取目前網址
        var curWwwPath=window.document.location.href;
        //擷取主機位址之後的目錄
        var pathName=window.document.location.pathname;
        
        var pos=curWwwPath.indexOf(pathName);
        //擷取主機位址
        var localhostPaht=curWwwPath.substring(0,pos);
        //擷取帶"/"的項目名,如:/uimcardprj
        var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
        
        return(localhostPaht+projectName);
    }
    //擷取路徑     
    var applicationPath = getRootPath();
    var URL = window.UEDITOR_HOME_URL || getUEBasePath();
    var serverURL = applicationPath;

    /**
     * 配置項主體。注意,此處所有涉及到路徑的配置别遺漏URL變量。
     */
    window.UEDITOR_CONFIG = {

        //為編輯器執行個體添加一個路徑,這個不能被注釋
        UEDITOR_HOME_URL: URL

        // 伺服器統一請求接口路徑
        , serverUrl: serverURL + "ueditor/dispatch"      

3、修改ueditor源碼 ConfigManager.java(指定配置檔案路徑).

修改前 :

/*
     * 通過一個給定的路徑建構一個配置管理器, 該管理器要求位址路徑所在目錄下必須存在config.properties檔案
     */
    private ConfigManager ( String rootPath, String contextPath, String uri ) throws FileNotFoundException, IOException {
        
        rootPath = rootPath.replace( "\\", "/" );
        
        this.rootPath = rootPath;
        this.contextPath = contextPath;
        
        if ( contextPath.length() > 0 ) {
            this.originalPath = this.rootPath + uri.substring( contextPath.length() );
        } else {
            this.originalPath = this.rootPath + uri;
        }
        
        this.initEnv();
        
    }      

修改後 :

/*
     * 通過一個給定的路徑建構一個配置管理器, 該管理器要求位址路徑所在目錄下必須存在config.properties檔案
     */
    private ConfigManager ( String rootPath, String contextPath, String uri ) throws FileNotFoundException, IOException {
        
        rootPath = rootPath.replace( "\\", "/" );
        
        this.rootPath = rootPath;
        this.contextPath = contextPath;
        
        /*if ( contextPath.length() > 0 ) {
            this.originalPath = this.rootPath + uri.substring( contextPath.length() );
        } else {
            this.originalPath = this.rootPath + uri;
        }*/
        
        this.originalPath = rootPath + "static" + File.separator + "lib" + File.separator +
                "ueditor" + File.separator + "1.4.3" + File.separator + "jsp" + File.separator + "controller.jsp";
        ///EdwManage/src/main/webapp/static/lib/ueditor/1.4.3/jsp/controller.jsp
        
        this.initEnv();
        
    }      

如上所述,主要修改 originalPath 的路徑,否則ueditor的配置檔案(ueditor_config.json)路徑是錯誤的(與springMVC整合的情況),之是以向上面那樣拼接路徑,是因為我的ueditor相關檔案拷貝在了(EdwManage/src/main/webapp/static/lib/ueditor/1.4.3/jsp/controller.jsp)路徑裡。

4、(若未執行該步操作,在選擇好圖檔後,點選上傳,将提示 : “未找到上傳檔案”)由于上傳的檔案都會被springmvc的檔案上傳攔截器攔截,包裝,這樣百度編輯器接收到檔案後不能識别檔案格式,是以把spring預設的commonsMultiparResolver,替換成我們自己寫的commonsMultiparResolver ,并修改配置檔案。

重寫CommonsMultipartResolver :

package com.tianwen.dcdp.common;

import org.springframework.web.multipart.commons.CommonsMultipartResolver;
             
public class CommonsMultiparResolver extends CommonsMultipartResolver {

    @Override  
      public boolean isMultipart(javax.servlet.http.HttpServletRequest request) {  
       String uri = request.getRequestURI();  
       System.out.println(uri);
       //過濾使用百度UEditor的URI  
       if (uri.indexOf("ueditor/dispatch") > 0) {     //此處攔截路徑即為上面編寫的controller路徑
        System.out.println("commonsMultipartResolver 放行");
        return false;  
       }  
       System.out.println("commonsMultipartResolver 攔截");
       return super.isMultipart(request);  
      }  
}      

修改springMVC配置檔案spring-mvc.xml :

<!-- 修改為我們重寫的CommonsMultiparResolver而不是spring提供的 -->
    <bean id="multipartResolver"  
        class="com.tianwen.dcdp.common.CommonsMultiparResolver">  
        <!-- 預設編碼 -->
        <property name="defaultEncoding" value="utf-8" />  
        <!-- 檔案大小最大值 -->
        <property name="maxUploadSize" value="10485760000" />  
        <!-- 記憶體中的最大值 -->
        <property name="maxInMemorySize" value="40960" />  
    </bean>       

5、最後配置上傳檔案儲存目錄,修改ueditor配置檔案(ueditor_config.json):

修改如下參數(即配置上傳檔案的URL路徑,若配置不正确,富文本編輯器中将不能正确顯示上傳的圖檔):

"imageUrlPrefix": "http://localhost:80/EdwManage", /* 圖檔通路路徑字首 */
    "imagePathFormat": "/static/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上傳儲存路徑,可以自定義儲存路徑和檔案名格式 */      

此處 imagePathFormat 之是以配置為如上格式,是因為springMVC中,指定了static目錄下的資源為靜态資源(其他路徑都會被springMVC攔截)。

檔案預設儲存的實體路徑為: web應用根路徑 + imagePathFormat 。

{yyyy}{mm}{dd} : 按天分類儲存

{time}{rand:6} : 随機生成檔案名

另外還有一種簡單的解決辦法:

1、建立一web工程(ueditor)。

2、将下載下傳下來的ueditor檔案拷貝到建立工程 的webapps目錄下,可參考官網介紹。

3、在使用ueditor的工程中,修改ueditor配置檔案(ueditor.config.js)如下 :

window.UEDITOR_HOME_URL = "http://localhost/ueditor/";
    var URL = window.UEDITOR_HOME_URL || getUEBasePath();

    /**
     * 配置項主體。注意,此處所有涉及到路徑的配置别遺漏URL變量。
     */
    window.UEDITOR_CONFIG = {

        //為編輯器執行個體添加一個路徑,這個不能被注釋
        UEDITOR_HOME_URL: URL

        // 伺服器統一請求接口路徑
        , serverUrl: URL+ "jsp/controller.jsp"      

3、配置上傳檔案儲存路徑,修改(ueditor_config.json) :

"imageUrlPrefix": "http://localhost:80/ueditor", /* 圖檔通路路徑字首 */
  "imagePathFormat": "/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上傳儲存路徑,可以自定義儲存路徑和檔案名格式 */      

轉載于:https://www.cnblogs.com/xiaoxin101/p/10997904.html