SpringBoot整合UEditor的範例程式碼,springbootueditor

來源:互聯網
上載者:User

SpringBoot整合UEditor的範例程式碼,springbootueditor

當前開發項目涉及到富文字框,瞭解了不少富文字編輯器之後,最終決定使用度娘的UEditor。原因:功能強大,並且內建適配java後端的圖片和視頻上傳。

項目地址

不多說,上一下該項目的地址: http://ueditor.baidu.com/website/

簡書不支援markdown其他網站的外鏈很遺憾

整合過程

後端改造

因為項目使用的springboot架構,而UEditor對於java後端的支援僅僅是給了一個jsp檔案。因此,需要對該檔案進行一下處理,修改為面向springboot的統一controller。

@Controller@Transactional@RequestMapping("/static/common/ueditor/jsp")public class JSPController {  @RequestMapping("/controller")  @ResponseBody  public void getConfigInfo(HttpServletRequest request,HttpServletResponse response){    response.setContentType("application/json");    String rootPath = request.getSession().getServletContext()        .getRealPath("/");    try {      String exec = new ActionEnter(request, rootPath).exec();      PrintWriter writer = response.getWriter();      writer.write(exec);      writer.flush();      writer.close();    } catch (IOException | JSONException e) {      e.printStackTrace();    }  }

如上所述,該項目即支援來自/static/common/ueditor/jsp/controller的上傳請求了。

前端請求

在前端添加UEditor支援。即:將整個uediotr包進行項目引入,並且在使用該控制項的地方進行js的匯入。

項目引入,我的對應代碼結構如下:

頁面引入,引入對應代碼如下:

<script type="text/javascript" charset="utf-8" th:src="@{/static/common/ueditor/ueditor.config.js}"></script>  <script type="text/javascript" charset="utf-8" th:src="@{/static/common/ueditor/ueditor.all.js}"></script>

執行個體化UEditor編輯器即可,下面是我的初始化參數,僅做參考。

//執行個體化編輯器  var ue = UE.getEditor(''+id,{    toolbars: [      [        'fontfamily', //字型        'fontsize', //字型大小        'undo', //撤銷        'redo', //重做        '|',        'emotion', //表情        'forecolor', //字型顏色        'backcolor', //背景色        'bold', //加粗        'underline', //底線        'strikethrough', //刪除線        '|',        'justifyleft', //居靠左對齊        'justifyright', //居靠右對齊        'justifycenter', //置中對齊        '|',        'link', //超連結        'unlink', //取消連結        'simpleupload', //單圖上傳        'insertimage', //多圖上傳        //'music', //音樂        //'insertvideo', //視頻        'removeformat', //清除格式        'formatmatch', //格式刷        'source', //原始碼      ]    ],    enableAutoSave:false,    autoHeightEnabled: true,    autoFloatEnabled: true,    initialFrameWidth:width,    initialFrameHeight:height,    scaleEnabled:true//捲軸  });

此時,訪問我們的頁面就會看到富文字框了。

不過,此時會提示我們後台設定檔出錯,無法實現上傳功能

實現上傳功能

修改config.js檔案,對應的全域請求路徑。該請求是為了擷取config.json對應的配置資料。可以在Controller裡面直接返回配置資訊或者在controller裡面進行json檔案的讀取。我這裡使用的是讀取設定檔的方式,使用UEditor內建的方法,文章開頭已經實現,這裡貼一下需要修改的請求:

完成以上配置之後,再次載入UEditor的頁面,其中上傳圖片的按鈕即可完成圖片的上傳了。

注意:如果開始偵錯模式,加入斷點,測試載入json串的時候。會出現逾時錯誤。暫時沒從設定檔裡面找到配置欄位。所有,這裡需要注意,假如一切配置均無問題,但是依然返回後台配置錯誤的話,可以把斷點全部取消掉試一試。

注意:上傳需要加入上傳組件,此處使用fileuoload

<dependency>      <groupId>commons-fileupload</groupId>      <artifactId>commons-fileupload</artifactId>      <version>1.3</version>    </dependency>

使用servlet實現上傳

/** * 嘗試使用servlet來實現UEditor * * @author OnyWang * @create 2018-02-05 2:40 **/@WebServlet(name = "UEditorServlet", urlPatterns = "/static/common/ueditor/UEditor")public class UEditorControllerServlet extends HttpServlet {  @Override  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {    request.setCharacterEncoding( "utf-8" );    response.setHeader("Content-Type" , "text/html");    PrintWriter out = response.getWriter();    ServletContext application=this.getServletContext();    String rootPath = application.getRealPath( "/" );    String action = request.getParameter("action");    String result = new ActionEnter( request, rootPath+"WEB-INF/classes" ).exec();    if( action!=null &&        (action.equals("listfile") || action.equals("listimage") ) ){      rootPath = rootPath.replace("\\", "/");      result = result.replaceAll(rootPath, "/");    }    out.write( result );  }  @Override  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {    doPost(req, resp);  }

採用servlet的方式,建立一個註解式的servlet即可。

需要在main方法裡面加入@ServletComponentScan註解。

修改ueditor預設訪問路徑。

注意:springboot下面,所有的資源檔都是放在classes下面的,所有,對於路徑的處理一定要加倍小心。放在增加路徑web-inf/classes

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.