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

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

 

上網查詢了不少相關資料,此處簡要記錄下,防止之後遇到相似問題。html

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

一、編寫controller 以下(該接口是ueditor先後臺交互的統一路徑) :web

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();
            }
            
    }
}

 

二、修改ueditor的配置文件 ueditor.config.js(指定後臺服務器地址),以下所示spring

修改前:json

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

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

        //爲編輯器實例添加一個路徑,這個不能被註釋
        UEDITOR_HOME_URL: URL

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

修改後 :spring-mvc

   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"

 

三、修改ueditor源碼 ConfigManager.java(指定配置文件路徑).服務器

修改前 :mvc

    /*
     * 經過一個給定的路徑構建一個配置管理器, 該管理器要求地址路徑所在目錄下必須存在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();
        
    }

修改後 :app

    /*
     * 經過一個給定的路徑構建一個配置管理器, 該管理器要求地址路徑所在目錄下必須存在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)路徑裏。

 

四、(若未執行該步操做,在選擇好圖片後,點擊上傳,將提示 : 「未找到上傳文件」)因爲上傳的文件都會被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> 

 

五、最後配置上傳文件保存目錄,修改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} : 隨機生成文件名

 

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

一、新建一web工程(ueditor)。

二、將下載下來的ueditor文件拷貝到新建工程 的webapps目錄下,可參考官網介紹

三、在使用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"


三、配置上傳文件保存路徑,修改(ueditor_config.json) :

  "imageUrlPrefix": "http://localhost:80/ueditor", /* 圖片訪問路徑前綴 */
  "imagePathFormat": "/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上傳保存路徑,能夠自定義保存路徑和文件名格式 */
相關文章
相關標籤/搜索