美文网首页
wangEditor 富文本编辑器

wangEditor 富文本编辑器

作者: 索伦x | 来源:发表于2019-01-31 00:48 被阅读0次

为什么需要富文本编辑器

RTE目的是提供给不会写HTML代码的用户,编辑出类似WORD格式的WEB文档。
主要应用场景:邮件编写、论坛灌水、电商商品管理、新闻消息模块

常见的富文本编辑器

百度的UEditor、kindEditor、TinyMCE、ckeditor、王福朋的wangEditor

wangEditor的介绍

wangEditor于2014年11月推出,是基于bootstrap的一款富文本编辑器
wangEditor的定位是做最简单、易用、快捷、轻量化的富文本编辑器。
wangEditor(仅支持 IE10+ 的浏览器)
官网:www.wangEditor.com
文档:www.kancloud.cn/wangfupeng/wangeditor3/332599
源码:github.com/wangfupeng1988/wangEditor

Spring MVC整合wangEditor


POM.xml

        <!-- 文件上传与下载 -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.3</version>
        </dependency>

spring-mvc.xml

    <mvc:resources mapping="/upload/**" location="/upload/" cache-period="31536000"/>

    <!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 -->
    <bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 上传文件大小上限,单位为字节(10MB) -->
        <property name="maxUploadSize">
            <value>10485760</value>
        </property>
        <!-- 请求的编码格式,必须和jSP的pageEncoding属性一致,以便正确读取表单的内容,默认为ISO-8859-1 -->
        <property name="defaultEncoding">
            <value>UTF-8</value>
        </property>
    </bean>

pojo

public class WangEditor {
    private Integer errno; //错误代码, 0标识没有错误
    private String[] data; //已上传到图片路径
}

Controller

@Controller
@RequestMapping(value="/upload")
public class UploadController{

    @ResponseBody
    @RequestMapping(value="wang")
    public WangEditor uploadFile(
            @RequestParam("myFile") MultipartFile multipartFile,
            HttpServletRequest request) {

        try {
            // 获取服务器真实路径
            String realPath = request.getSession().getServletContext().getRealPath("");
            InputStream inputStream = multipartFile.getInputStream();
            String contextPath = request.getContextPath();
            // 服务器根目录的路径
            String path = null;
            if(contextPath.length()>0) {
                path = realPath.replace(contextPath.substring(1), "");
            }else{
                path = realPath;
            }
            // 根目录下新建文件夹upload,存放上传图片
            String uploadPath = path + "upload";
            /* 获取上传文件的后缀 */
            String suffix = multipartFile.getOriginalFilename().substring (multipartFile.getOriginalFilename().lastIndexOf("."));
            /* 保存文件名称 */
            String filename = UUID.randomUUID().toString()+ suffix;
            // 将文件上传的服务器根目录下的upload文件夹
            File file = new File(uploadPath, filename);
            FileUtils.copyInputStreamToFile(inputStream, file);
            // 返回图片访问路径
            String url = request.getScheme() + "://" + request.getServerName()
                    + ":" + request.getServerPort() + "/upload/" + filename;

            String[] strs = {url};
            WangEditor we = new WangEditor();
            we.setErrno(new Integer(0));
            we.setData(strs);
            return we;
        } catch (IOException e) {
            //log.error("上传文件失败", e);
            e.printStackTrace();
        }
        return null;

    }

HTML页面

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        .toolbar {
            border: 1px solid #ccc;
            width: 800px;
        }

        .text {
            border: 1px solid #ccc;
            height: 400px;
            width: 800px;
        }
    </style>
</head>

<body>
<div id="div1" class="toolbar"></div>
<div style="padding: 5px 0; color: #ccc"></div>
<div id="div2" class="text">
    <p>请在此输入内容</p>
</div>
</body>
<script src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
<script type="text/javascript" src="plugins/wangeditor/wangEditor.min.js"></script>
<script>
    var E = window.wangEditor
    var editor = new E('#div1', '#div2') // 两个参数也可以传入 elem 对象,class 选择器
    //开启debug模式
    editor.customConfig.debug = true;
    // 关闭粘贴内容中的样式
    editor.customConfig.pasteFilterStyle = false
    // 忽略粘贴内容中的图片
    editor.customConfig.pasteIgnoreImg = true
    // 使用 base64 保存图片
    //editor.customConfig.uploadImgShowBase64 = true

    // 上传图片到服务器
    editor.customConfig.uploadFileName = 'myFile'; //设置文件上传的参数名称
    editor.customConfig.uploadImgServer = '/upload/wang'; //设置上传文件的服务器路径
    editor.customConfig.uploadImgMaxSize = 3 * 1024 * 1024; // 将图片大小限制为 3M
    //自定义上传图片事件
    editor.customConfig.uploadImgHooks = {
        before: function(xhr, editor, files) {

        },
        success: function(xhr, editor, result) {
            console.log("上传成功");
        },
        fail: function(xhr, editor, result) {
            console.log("上传失败,原因是" + result);
        },
        error: function(xhr, editor) {
            console.log("上传出错");
        },
        timeout: function(xhr, editor) {
            console.log("上传超时");
        }
    }

    editor.create()
</script>
<br/>
<button id="editorGetBtn">获取内容</button>
<script>
    $("#editorGetBtn").click(function(){
        //获取编辑器的内容,带样式
        //一般使用这个获取数据,通过ajax发送给服务端 ,然后服务端以String接收,保存到数据库.
        alert(editor.txt.html());
    });
</script>
</html>

相关文章

网友评论

      本文标题:wangEditor 富文本编辑器

      本文链接:https://www.haomeiwen.com/subject/nxwhsqtx.html