springMVC -- 整合UEditor(富文本編輯器)


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

 

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

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

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}", /* 上傳保存路徑,可以自定義保存路徑和文件名格式 */

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM