SpringBoot整合UEditor的示例代碼
當(dāng)前開(kāi)發(fā)項(xiàng)目涉及到富文本框,了解了不少富文本編輯器之后,最終決定使用度娘的UEditor。原因:功能強(qiáng)大,并且自帶適配java后端的圖片和視頻上傳。
項(xiàng)目地址
不多說(shuō),上一下該項(xiàng)目的地址: http://ueditor.baidu.com/website/
簡(jiǎn)書(shū)不支持markdown其他站點(diǎn)的外鏈很遺憾
整合過(guò)程
后端改造
因?yàn)轫?xiàng)目使用的springboot框架,而UEditor對(duì)于java后端的支持僅僅是給了一個(gè)jsp文件。因此,需要對(duì)該文件進(jìn)行一下處理,修改為面向springboot的統(tǒng)一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(); } }
如上所述,該項(xiàng)目即支持來(lái)自/static/common/ueditor/jsp/controller的上傳請(qǐng)求了。
前端請(qǐng)求
在前端添加UEditor支持。即:將整個(gè)uediotr包進(jìn)行項(xiàng)目引入,并且在使用該控件的地方進(jìn)行js的導(dǎo)入。
項(xiàng)目引入,我的對(duì)應(yīng)代碼結(jié)構(gòu)如下:
頁(yè)面引入,引入對(duì)應(yīng)代碼如下:
<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>
實(shí)例化UEditor編輯器即可,下面是我的初始化參數(shù),僅做參考。
//實(shí)例化編輯器 var ue = UE.getEditor(''+id,{ toolbars: [ [ 'fontfamily', //字體 'fontsize', //字號(hào) 'undo', //撤銷 'redo', //重做 '|', 'emotion', //表情 'forecolor', //字體顏色 'backcolor', //背景色 'bold', //加粗 'underline', //下劃線 'strikethrough', //刪除線 '|', 'justifyleft', //居左對(duì)齊 'justifyright', //居右對(duì)齊 'justifycenter', //居中對(duì)齊 '|', 'link', //超鏈接 'unlink', //取消鏈接 'simpleupload', //單圖上傳 'insertimage', //多圖上傳 //'music', //音樂(lè) //'insertvideo', //視頻 'removeformat', //清除格式 'formatmatch', //格式刷 'source', //源代碼 ] ], enableAutoSave:false, autoHeightEnabled: true, autoFloatEnabled: true, initialFrameWidth:width, initialFrameHeight:height, scaleEnabled:true//滾動(dòng)條 });
此時(shí),訪問(wèn)我們的頁(yè)面就會(huì)看到富文本框了。
不過(guò),此時(shí)會(huì)提示我們后臺(tái)配置文件出錯(cuò),無(wú)法實(shí)現(xiàn)上傳功能
實(shí)現(xiàn)上傳功能
修改config.js文件,對(duì)應(yīng)的全局請(qǐng)求路徑。該請(qǐng)求是為了獲取config.json對(duì)應(yīng)的配置數(shù)據(jù)??梢栽贑ontroller里面直接返回配置信息或者在controller里面進(jìn)行json文件的讀取。我這里使用的是讀取配置文件的方式,使用UEditor自帶的方法,文章開(kāi)頭已經(jīng)實(shí)現(xiàn),這里貼一下需要修改的請(qǐng)求:
完成以上配置之后,再次加載UEditor的頁(yè)面,其中上傳圖片的按鈕即可完成圖片的上傳了。
注意:如果開(kāi)始調(diào)試模式,加入斷點(diǎn),測(cè)試加載json串的時(shí)候。會(huì)出現(xiàn)超時(shí)錯(cuò)誤。暫時(shí)沒(méi)從配置文件里面找到配置字段。所有,這里需要注意,假如一切配置均無(wú)問(wèn)題,但是依然返回后臺(tái)配置錯(cuò)誤的話,可以把斷點(diǎn)全部取消掉試一試。
注意:上傳需要加入上傳組件,此處使用fileuoload
<dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3</version> </dependency>
使用servlet實(shí)現(xiàn)上傳
/** * 嘗試使用servlet來(lái)實(shí)現(xiàn)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的方式,新建一個(gè)注解式的servlet即可。
需要在main方法里面加入@ServletComponentScan注解。
修改ueditor默認(rèn)訪問(wèn)路徑。
注意:springboot下面,所有的資源文件都是放在classes下面的,所有,對(duì)于路徑的處理一定要加倍小心。放在增加路徑web-inf/classes
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java畢業(yè)設(shè)計(jì)實(shí)戰(zhàn)之財(cái)務(wù)預(yù)算管理系統(tǒng)的實(shí)現(xiàn)
這是一個(gè)使用了java+SSM+Jsp+Mysql+Layui+Maven開(kāi)發(fā)的財(cái)務(wù)預(yù)算管理系統(tǒng),是一個(gè)畢業(yè)設(shè)計(jì)的實(shí)戰(zhàn)練習(xí),具有財(cái)務(wù)預(yù)算管理該有的所有功能,感興趣的朋友快來(lái)看看吧2022-02-02Spring @Bean vs @Service注解區(qū)別
本篇文章主要介紹了Spring @Bean vs @Service注解區(qū)別,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12java實(shí)現(xiàn)簡(jiǎn)單美女拼圖游戲
這篇文章主要介紹了java實(shí)現(xiàn)簡(jiǎn)單美女拼圖游戲的相關(guān)資料,需要的朋友可以參考下2015-03-03一文搞懂spring boot本地事務(wù)@Transactional參數(shù)
這篇文章主要介紹了spring boot本地事務(wù)@Transactional參數(shù)詳解,本文通過(guò)示例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-10-10java實(shí)現(xiàn)圖片用Excel畫(huà)出來(lái)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)圖片用Excel畫(huà)出來(lái),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03