Springboot Configuring the Rich Text editor Xheditor

Source: Internet
Author: User

Wrote an article a year ago: Struts2 configuration xheditor articles. SSH was still in use, now start with spring boot. I wanted to configure CSDN's Markdown editor, but I couldn't find it on GitHub. So, take back the lightweight xheditor.

Environmental requirements: Spring Boot v1.5.1.release, jdk1.7, myeclipse2015, xheditor1.1.14

Xheditoe's official website seems to be down, I xheditor1.1.14 the compressed package to the resources there, click to enter the xheditor1.1.14 download page

Here is the configuration process:

1). Unzip the Xheditor, create a new Xheditor directory under the static directory, and put the extracted files in

2). Create a new HTML page under templates: blog.html
Here are two points to note:
<1> Pageinit () inside the configuration: Html5upload:false, must be set to FALSE, otherwise you can not upload pictures;
<2> Upimgurl: The/uploadimg of "/uploadimg" is the way to upload images asynchronously (next point)

<! DOCTYPE html><html xmlns:th="http://www.thymeleaf.org" ><head>    <meta name="keywords" content="Keyword1,keyword2,keyword3" ></meta>    <meta name="description" content="This is my page" ></meta>    <meta name="Content-type" content="text/html; Charset=utf-8 "></meta>    <title>Blog</title>    <link rel="stylesheet" href="Xheditor/common.css " type="Text/css" Media="screen" />    <script type="text/javascript" src="xheditor/jquery/ Jquery-1.4.4.min.js "></script>    <script type="text/javascript" src="xheditor/ Xheditor-1.1.14-zh-cn.min.js "></script>    <script  type  =< Span class= "Hljs-value" > "Text/javascript" ; $ (pageinit);  function pageinit(){ $.extend (xheditor.settings,{shortcuts:{' Ctrl+enter '    : SubmitForm}});    $ (' #elm1 '). Xheditor ({tools:' full ', skin:' default ', Internalscript:false, Html5upload:false, Upimgurl:"/uploadimg", Upimgext:"Jpg,jpeg,gif,png"});  function submitForm(){$ (' #frmDemo '). Submit ();}</script></head><body>    <textarea id="elm1" name="essaycontent" rows=  " cols" = " a" style="width:80%"></textarea></body></html>

3). Xheditor image upload is asynchronous, that is, the image is uploaded to the server, the server returns the image of the path echo. So to write a controller specifically to deal with the image upload, in order not to confuse the business logic, the special handling of the image upload controller written in the toolkit. One thing to note is that the message returned to Xheditor (in JSON format), has a msg parameter, xheditor the path of the image according to MSG, that is, MSG represents the path of the image that was just uploaded, the file name and path to complete! For example: String msg = "/imgupload/" + newfilename; /imgupload/is the folder where the picture is stored, NewFileName is the name of the file whose UUID has changed.

Packagecom. HSP. Util;Import Java. IO. File;Import Java. Util. UUID;Import Javax. servlet. HTTP. HttpServletRequest;import org. Springframework. Stereotype. Controller;import org. Springframework. Web. Bind. Annotation. Requestmapping;import org. Springframework. Web. Bind. Annotation. Requestmethod;import org. Springframework. Web. Bind. Annotation. Requestparam;import org. Springframework. Web. Bind. Annotation. Responsebody;import org. Springframework. Web. Multipart. Multipartfile;@Controllerpublic class Xheditorimgupload {@RequestMapping (value ="/uploadimg", Method=requestmethod. POSTPublic @ResponseBody String Imgupload (httpservletrequest request, @RequestParam Multipartfile filedata) { System. out. println("Filedata"+ filedata);String Filedatafilename = Filedata. Getoriginalfilename(); System. out. println("Filedatafilename"+ filedatafilename);String Path = Request. GetSession(). Getservletcontext(). Getrealpath("imgupload/"); UUID change file name, avoid duplicate file name String Newfilename=uuid. Randomuuid(). toString() +filedatafilename. Substring(Filedatafilename. IndexOf("."), Filedatafilename. Length());String message;String err ="";String msg ="/imgupload/"+ NewFileName;System. out. println("MSG--->"+ msg);try {Fileutil. UploadFile(Filedata. GetBytes(), Path, NewFileName);} catch (Exception e) {//err = E. GetMessage();} message ="{\" err\ ": \" "+ Err +"\", \ "msg\": \ ""+ msg +"\"}";Err = message;Return message;}}

4). How to upload files: Fileutil.uploadfile (...)

publicstaticvoiduploadFile(byte[] file, String filePath, String fileName) throws Exception {         new File(filePath);          if(!targetFile.exists()){                targetFile.mkdirs();            }               outnew FileOutputStream(filePath+fileName);        out.write(file);        out.flush();        out.close();    }

5). Set the maximum file upload limit, in application.properties add:

spring.http.multipart.maxFileSize=100Mbspring.http.multipart.maxRequestSize=100Mb

6). Test

7). If an error occurs, look at the controller in String msg = "/imgupload/" + newfilename; There is no write-all, no setting file size and so on.

8). Wish Everyone success!

Springboot Configuring the Rich Text editor Xheditor

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.