本篇文章為大家展示了UEditor怎么在SpringBoot中使用,內(nèi)容簡明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
蘆溪網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)公司,蘆溪網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為蘆溪近千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站建設(shè)要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的蘆溪做網(wǎng)站的公司定做!
因?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)目即支持來自/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)如下:

頁面引入,引入對(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', //音樂
//'insertvideo', //視頻
'removeformat', //清除格式
'formatmatch', //格式刷
'source', //源代碼
]
],
enableAutoSave:false,
autoHeightEnabled: true,
autoFloatEnabled: true,
initialFrameWidth:width,
initialFrameHeight:height,
scaleEnabled:true//滾動(dòng)條
});此時(shí),訪問我們的頁面就會(huì)看到富文本框了。
不過,此時(shí)會(huì)提示我們后臺(tái)配置文件出錯(cuò),無法實(shí)現(xiàn)上傳功能
實(shí)現(xiàn)上傳功能
修改config.js文件,對(duì)應(yīng)的全局請(qǐng)求路徑。該請(qǐng)求是為了獲取config.json對(duì)應(yīng)的配置數(shù)據(jù)。可以在Controller里面直接返回配置信息或者在controller里面進(jìn)行json文件的讀取。我這里使用的是讀取配置文件的方式,使用UEditor自帶的方法,文章開頭已經(jīng)實(shí)現(xiàn),這里貼一下需要修改的請(qǐng)求:

完成以上配置之后,再次加載UEditor的頁面,其中上傳圖片的按鈕即可完成圖片的上傳了。
注意:如果開始調(diào)試模式,加入斷點(diǎn),測試加載json串的時(shí)候。會(huì)出現(xiàn)超時(shí)錯(cuò)誤。暫時(shí)沒從配置文件里面找到配置字段。所有,這里需要注意,假如一切配置均無問題,但是依然返回后臺(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來實(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)訪問路徑。
注意:springboot下面,所有的資源文件都是放在classes下面的,所有,對(duì)于路徑的處理一定要加倍小心。放在增加路徑web-inf/classes
上述內(nèi)容就是UEditor怎么在SpringBoot中使用,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)站欄目:UEditor怎么在SpringBoot中使用
URL地址:http://chinadenli.net/article48/gdghep.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、營銷型網(wǎng)站建設(shè)、電子商務(wù)、搜索引擎優(yōu)化、云服務(wù)器、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)