Springboot-2.1.4 整合 kindeditor富文本編輯器

搭建環境

  • 前去官網下載kindeditor需要的js文件。
  • 解壓出來後選擇部分文件導入到項目中(紅線是不要的)。
  • 導進項目後的目錄如下(不要忘記導入 jQuery)

編寫代碼

  • index.html
  <!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
<h2>Hello,kindeditor</h2>
<hr>



<textarea id="editor_id" name="content" style="width:700px;height:300px;">
&lt;strong&gt;HTML內容&lt;/strong&gt;
</textarea>
<br>
<br>
<br>
<button id="submit">提交</button>


</body>
<script charset="utf-8" src="/kindeditor/kindeditor-all.js"></script>
<script charset="utf-8" src="/kindeditor/lang/zh-CN.js"></script>
<script charset="utf-8" src="/jquery.min.js"></script>
<script>

  KindEditor.ready(function(K) {
      window.editor = K.create('#editor_id',{
          // 上傳圖片的名字
          filePostName  : "uploadFile",
          // 配置上傳路徑
          uploadJson : '/uploadImg',
          allowFileManager : true,
          allowPreviewEmoticons: true,
          allowImageUpload: true,
          // 設置上傳文件類型
          dir : "image"
      });
  });


</script>


<script>
  $("#submit").click(function () {
      alert("完成了點擊.")
  })
</script>
</html>
  • 後臺java
    @Controller
public class KindeditorController {


    @RequestMapping("/index")
    public String index() {

        return "index";
    }


    @ResponseBody
    @RequestMapping("/uploadImg")
    public JSONObject uploadImg(@RequestParam("uploadFile") MultipartFile multipartFile) throws IOException {

        JSONObject jsonObject = new JSONObject();
        // 這裏必須是小數類型,
        jsonObject.put("error",0);
        jsonObject.put("url","https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2114569028,998077550&fm=26&gp=0.jpg");

        return jsonObject;
    }
}

注意問題

  • 返回數據時 jsonObject.put(“error”,0); error 對應的一定要是數值類型因爲在 kindeditor-all.js 中將返回數值類型的判斷寫了 === 三個等於號,即數值類型也要相同。
  • springboot 2 是使用tomcat9作爲服務器,而tomcat9 加強就字符判斷的安全性。所以在kindeditor上使用顏色時需要將 #FF0000 這種類型改爲 red 這種英文的類型。

擴展內容:擴展內容
我的github主頁:github

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章