1. 程式人生 > >summernote富文字外掛快速上手

summernote富文字外掛快速上手

1:先匯入   summernote.css   summernote.js

2:寫個DIV 當做 富文字的框 供初始化

 <div id="oaNotifyContent">${oaNotify.content}</div>

3:在DOM 載入完成後進行初始化

 //   富文字初始化
           $("#oaNotifyContent").summernote({
               height:350,
               minHeight:null,    // 定義編輯框最低的高度
               maxHeight:null,    // 定義編輯框最高的高度
               disableDragAndDrop:true,   // 禁止拖拽
               lang:'zh-CN'
           });

4:就這樣的話是提交表單是沒辦法獲取到資料的。因為沒有input標籤。

          在富文字框上邊寫上個 hidden標籤進行接收富文字的值

<input type="hidden" name="content" />
<div id="oaNotifyContent">${oaNotify.content}</div>

5:在提交表單的事件      將富文字的值存到 剛剛的input  隱藏標籤中   

$("input[name='content']").val($('#oaNotifyContent').summernote('code'));

6:這時候input標籤裡面就有值了,        現在要進行驗證內容是否為空!  

var oaNotifyContentVal = $('#oaNotifyContent').summernote('code')//取富文字的值
   if(oaNotifyContentVal.trim().length <= 0 ){
       alert("內容不能為空!");
       return false;
}

7:   提交表單 或  呼叫非同步請求

form.submit();          $.post() 都可以

8:在後端伺服器  接收到  資料  進行  CRUD 之前   對富文字進行特殊處理   反轉義

物件.屬性(StringEscapeUtils.unescapeHtml4(物件.屬性));

PS:(1):如果沒有進行反轉義會是這樣       &lt;pre class=&quot;brush: java;&quot;&gt;

         (2):需要的應該是<pre class=\"brush: java;\"> 

         (3) StringEscapeUtils   類的   unescapeHtml4   這個 方法在

               org.apache.commons.lang3.包下;

          (4)POM :

                     <dependency>
                                 <groupId>org.apache.commons</groupId>
                                 <artifactId>commons-lang3</artifactId>
                                 <version>3.5</version>
                      </dependency>

9:儲存到資料庫裡面就是正確的資料了,在初始化的時候按照剛剛的步驟         自動把  html 標籤注入 OK