百度编辑器ueditor简单易用


    最近刚刚被分配任务给一个新闻发布,它采用了富文本编辑器。本人也被用于,只是这一次我选择了百度的富文本编辑器ueditor1_4_3-utf8-jsp版。

    事实上看ueditor功能非常强大,只是百度的设计还是非常不错的。仅仅须要略微配置一下就能够用了。

    首先下载到 ueditor1_4_3-utf8-jsp.rar。连接地址是:http://ueditor.baidu.com/website/

我选择的是utf-8版本号的。

    解压以后总体复制到java web项目的网页根文件夹下。

我这里是用myeclipse建的项目,所以把ueditor复制到了WebRoot下就可以。

    第二步,引用ueditor/jsp/lib下的jar包。你能够选择直接在Java Build Path中配置。或者直接放入WebRoot/WEB-INF/lib目录中,项目会自己主动引用该目录下的jar。推荐使用直接放到WEB-INF/lib下,这样你的jar是随着项目移动的,系统会依照相对路径载入项目下的jar。假设选择第一种。假设当jar文件移动了,那么这个功能就会失效的。

    第三步,在页面上引入2个js文件

<scripttype="text/javascript"src="ueditor/ueditor.config.js"></script>
<scripttype="text/javascript"src="ueditor/ueditor.all.js"></script>
    第四步。在页面的编辑器位置。加入html代码

<textareaname="content" id="myEditor"></textarea>
 <scripttype="text/javascript">
 UEDITOR_CONFIG.UEDITOR_HOME_URL = './ueditor/'; //一定要用这句话,否则你须要去ueditor.config.js改动路径的配置信息
 UE.getEditor('myEditor');
 </script>

    第五步,ueditor支持图片、文档、音乐等文件上传功能,假设你想要配置上传路径,能够改动 ueditor/jsp/config.json。

    这个文件对于每个配置项,都明白的文字说明。附上一段图片上传的配置吧:

    /* 上传图片配置项 */
   "imageActionName": "uploadimage", /* 运行上传图片的action名称 */
   "imageFieldName": "upfile", /* 提交的图片表单名称 */
   "imageMaxSize": 2048000, /* 上传限制大小,单位B */
   "imageAllowFiles": [".png", ".jpg",".jpeg", ".gif", ".bmp"], /* 上传图片格式显示 */
   "imageCompressEnable": true, /* 是否压缩图片,默认是true*/
   "imageCompressBorder": 1600, /* 图片压缩最长边限制 */
   "imageInsertAlign": "none", /* 插入的图片浮动方式 */
   "imageUrlPrefix": "", /* 图片訪问路径前缀 */
   "imagePathFormat":"_images/image/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上传保存路径,能够自己定义保存路径和文件名称格式 */

第六步,ueditor支持自己定义功能,界面上显示的功能都是可配置的。仅仅要在ueditor/ueditor.config.js的toolbar中删改配置就可以,代码例如以下:

 //工具栏上的全部的功能button和下拉框。能够在new编辑器的实例时选择自己须要的从新定义 
        , toolbars: [[ 
            'fullscreen', 'source', '|', 'undo', 'redo', '|', 
            'bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'superscript', 'subscript', 'removeformat', 'formatmatch', 'autotypeset', 'blockquote', 'pasteplain', '|', 'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist', 'selectall', 'cleardoc', '|', 
            'rowspacingtop', 'rowspacingbottom', 'lineheight', '|', 
            'customstyle', 'paragraph', 'fontfamily', 'fontsize', '|', 
            'directionalityltr', 'directionalityrtl', 'indent', '|', 
            'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|', 'touppercase', 'tolowercase', '|', 
            'link', 'unlink', 'anchor', '|', 'imagenone', 'imageleft', 'imageright', 'imagecenter', '|', 
            'simpleupload', 'insertimage', 'emotion', 'scrawl', 'insertvideo', 'music', 'attachment', 'map', 'gmap', 'insertframe', 'insertcode', 'pagebreak', 'template', 'background', '|', 
            'horizontal', 'date', 'time', 'spechars', 'snapscreen', 'wordimage', '|', 
            'inserttable', 'deletetable', 'insertparagraphbeforetable', 'insertrow', 'deleterow', 'insertcol', 'deletecol', 'mergecells', 'mergeright', 'mergedown', 'splittocells', 'splittorows', 'splittocols', 'charts', '|', 
            'searchreplace', 'help', 'drafts' 
        ]]
    看起来是6步。事实上每一步都非常easy了。

为了让小伙伴们看得更清楚些,来张效果图吧:



    效果还不错吧,事实上,看起来复杂的功能。实现起来却非常easy。而简单的功能。实现起来可能会非常复杂。

我们不仅仅是使用那么看起来复杂,使用起来简单的各种开源组件。更要学习,怎样把复杂的功能封装起来。对外提供简单的接口,让别人也能简单使用。

 

版权声明:本文博客原创文章。博客,未经同意,不得转载。

原文地址:https://www.cnblogs.com/hrhguanli/p/4755505.html