富文本编辑器ueditor使用配置

富文本编辑器(UEditor)


      在平时开发Java Web项目的时候,往往会使用第三方插件来帮助我们更快的实现功能。

此文来自: 马开东云搜索 转载请注明出处 网址: http://makaidong.com

此文原标题: javaweb 集成UEditor 来源网址: http://makaidong.com/yrxperfect/7206_7923932.html


      这里教大家使用百度开源的富文本编辑器(UEditor)来帮助我们更好的编写文本。


官网下载地址

http://ueditor.baidu.com/website/download.html


这里下载最新版的就可以了

1



解压出来是这样的

2



打开index.html的效果

3

好了 ,废话不多说,开始我们的正题。





1、配置编辑器环境


创建一个动态web工程

4



将解压出来的编辑器文件夹整个拷贝到WebContent目录下

5


此时工程会报错,因为我们没有引用所需的jar包。


utf8-jsp -> jsp->lib目录下中的所有jar包拷贝到WEB-INF目录下的lib文件夹

6



在WebContent下创建一个index.jsp的文件。


将utf8-jsp中的index.html文件内容拷贝到index.jsp



注:使用插件时必须引入以下3个文件

<script type="text/javascript" charset="utf-8" src=" ueditor.config.js"></script> <script type="text/javascript" charset="utf-8" src=" ueditor.all.min.js"> </script> <script type="text/javascript" charset="utf-8" src=" lang/zh-cn/zh-cn.js"></script>


调用编辑器:

<script id="editor" type="text/plain" style="width:1024px;height:500px;"></script>

7



将引用js文件的相对路径补全

8



完成之后运行index.jsp或者右键工程运行

9

这样基本的配置就搭建好了。





2、获取编辑框的内容


我们来使用富文本编辑器随便写一些内容,然后点击获取内容

10



我们发现,在JavaScript中可以使用 editor.getContent()获得整个p标签的内容,那我们怎么在java web中拿到内容呢?

11


回到index.jsp中


使用form表单将整个 编辑器包涵,并且加上用于提交表单的按钮

<body>
<div>
    <form action="UeditorServlet">
	    <h1>完整demo</h1>
	    <script id="editor" type="text/plain" style="width:1024px;height:500px;"></script>
		<input type="submit" value="提交"/>
	</form>
</div>
<script type="text/javascript">


    //实例化编辑器
    //建议使用工厂方法getEditor创建和引用编辑器实例,如果在某个闭包下引用该编辑器,直接调用UE.getEditor('editor')就能拿到相关的实例
    var ue = UE.getEditor('editor');


</script>
</body>


将多余的按钮以及js脚本都删除,保留一个实例化编辑器的方法

var ue = UE.getEditor('editor');
  • 1
  • 1

12



运行之后 编辑一段内容然后点击提交

http://localhost:8080/ueditor/index.jspeditorValue=%3Cp%3E%E6%88%91%E6%98%AF%E5%86%85%E5%AE%B9%3C%2Fp%3E
  • 1
  • 1

13



我们可以发现,在提交表单的时候数据是保存在editorValue下的,知道原理之后我们就可以创建一个servlet来接收这个字段了

14



创建Servlet之后还需修改form表单中的action值

<form action="UeditorServlet" method="post">
  • 1
  • 1


UeditorServlet .Java中的doGet()方法

public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {


		request.setCharacterEncoding("UTF-8"); 
		response.setCharacterEncoding("UTF-8"); 
		String content = request.getParameter("editorValue"); 
		if(content != null){ 
			request.setAttribute("content",content);
			request.getRequestDispatcher("content.jsp").forward(request, response); 
		}else{ 
			response.getWriter().append("内容为空!");
		}
		
	}


Content.jsp页面就简单使用EL表达式接收数据即可

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">   
    <title>My JSP 'content.jsp' starting page</title>
  </head>
  
  <body>
  <%
  out.print(request.getRealPath(""));
   %>
   	<div> ${content } </div>
  </body>
</html>


运行index.jsp ,随便编辑一段文字提交

这时内容就已经传过来了。

15




3、配置图片路径


在没有配置图片上传路径的时候,添加一张图片时是显示不出来的

16



编辑utf8-jsp -> lib -> 下的config.json文件

17



修改图片上传的路径  ()上传图片保存路径 去掉最前面的/




编辑utf8-jsp目录下的ueditor.config.js

19



在配置中加入编辑器的路径地址

20



配置完成之后重启toncat服务器并且运行index.jsp,编写一条图文信息提交

21



提交之后的结果:

22



查看图片保存路径可以在jsp中使用以下代码,即可得到工程编译后的路径

<%
    //图片保存的路径,可以到这个路径下查看
    out.println(request.getRealPath("")); %>
  • 1

23



获取到了根目录位置

24


25



简单的配置以及使用就介绍到这里吧。


相关的编辑器配置信息可到 utf8-jsp目录下的ueditor.config.js文件中修改


  • 14
    点赞
  • 50
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
百度富文本编辑器UEditor是一款基于JavaScript的所见即所得富文本编辑器,可以轻松地将其集成到Java Web项目中,为用户提供更好的编辑体验。下面简单介绍一下在Java Web项目中使用百度富文本编辑器UEditor的方法。 1.下载UEditor 首先,需要从百度UEditor官网上下载UEditor的源代码。下载地址为:http://ueditor.baidu.com/website/download.html 2.将UEditor集成到Java Web项目中 下载完成后,将UEditor的源代码解压缩,并将解压后的文件夹复制到Java Web项目的WebContent目录下的任意一个子目录中(比如说WebContent/ueditor)。然后,将UEditor中的jsp目录下的所有文件复制到Java Web项目的WebContent目录下的任意一个子目录中(比如说WebContent/jsp)。 3.配置UEditor 在Java Web项目的WebContent目录下新建一个名为config.json的文件,该文件的内容如下: { "imageActionName": "uploadimage", "imageFieldName": "upfile", "imageMaxSize": 2048000, "imageAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], "imageCompressEnable": true, "imageCompressBorder": 1600, "imageInsertAlign": "none", "imageUrlPrefix": "", "imagePathFormat": "/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", "scrawlActionName": "uploadscrawl", "scrawlFieldName": "upfile", "scrawlPathFormat": "/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", "scrawlMaxSize": 2048000, "scrawlUrlPrefix": "", "scrawlInsertAlign": "none", "snapscreenActionName": "uploadimage", "snapscreenPathFormat": "/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", "snapscreenUrlPrefix": "", "snapscreenInsertAlign": "none", "catcherLocalDomain": ["127.0.0.1", "localhost", "img.baidu.com"], "catcherActionName": "catchimage", "catcherFieldName": "source", "catcherPathFormat": "/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", "catcherUrlPrefix": "", "catcherMaxSize": 2048000, "catcherAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], "videoActionName": "uploadvideo", "videoFieldName": "upfile", "videoPathFormat": "/ueditor/jsp/upload/video/{yyyy}{mm}{dd}/{time}{rand:6}", "videoUrlPrefix": "", "videoMaxSize": 102400000, "videoAllowFiles": [ ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg", ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid" ], "fileActionName": "uploadfile", "fileFieldName": "upfile", "filePathFormat": "/ueditor/jsp/upload/file/{yyyy}{mm}{dd}/{time}{rand:6}", "fileUrlPrefix": "", "fileMaxSize": 51200000, "fileAllowFiles": [ ".png", ".jpg", ".jpeg", ".gif", ".bmp", ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg", ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid", ".rar", ".zip", ".tar", ".gz", ".7z", ".bz2", ".cab", ".iso", ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md", ".xml" ], "imageManagerActionName": "listimage", "imageManagerListPath": "/ueditor/jsp/upload/image/", "imageManagerListSize": 20, "imageManagerUrlPrefix": "", "imageManagerInsertAlign": "none", "imageManagerAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], "fileManagerActionName": "listfile", "fileManagerListPath": "/ueditor/jsp/upload/file/", "fileManagerUrlPrefix": "", "fileManagerListSize": 20, "fileManagerAllowFiles": [ ".png", ".jpg", ".jpeg", ".gif", ".bmp", ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg", ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid", ".rar", ".zip", ".tar", ".gz", ".7z", ".bz2", ".cab", ".iso", ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md", ".xml" ] } 该文件是UEditor配置文件,主要配置了上传文件的相关设置。 4.在JSP页面中使用UEditor 在需要使用UEditor的JSP页面中,添加如下代码: <!-- 加载ueditor编辑器js文件 --> <script type="text/javascript" src="${pageContext.request.contextPath}/ueditor/ueditor.config.js"></script> <script type="text/javascript" src="${pageContext.request.contextPath}/ueditor/ueditor.all.min.js"></script> <!-- 创建编辑器 --> <script type="text/javascript"> var editor = UE.getEditor('editor'); </script> 其中,ueditor.config.js和ueditor.all.min.js是UEditor的核心文件,需要在页面中引入。editor是页面中一个div元素的id,表示要将该div元素转化为UEditor编辑器。 至此,就完成了在Java Web项目中使用百度富文本编辑器UEditor的集成。
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值