SpringBoot整合富文本编辑器(UEditor)
UEditro是一款比较好用的富文本编辑器,所谓的富文本编辑器就是和服务器交互的数据不是普通的字符串文件,而是一些内容包含比较广的字符串,一般是指的html页面,目前比较好用的是百度的UEditor,到官方网站下载:
http://ueditor.baidu.com/website/download.html
1、首先在项目下新建ueditor文件夹,吧下载的ueditor文件里面的内容全部拷贝进去;
2、在我们需要使用富文本编辑器的页面里面引入ueditor:
<%@ 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 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script src="<%=path %>/ueditor/ueditor.config.js"></script>
<script src="<%=path %>/ueditor/ueditor.all.min.js"></script>
<script src="<%=path %>/ueditor/lang/zh-cn/zh-cn.js"></script>
<script type="text/javascript">
var editor = UE.getEditor('container');
alert(editor);
</script>
</head>
<body>
<textarea id="container" name="content" type="text/plain">这里写你的初始化内容</textarea>
</body>
</html>
启动程序,访问效果如下:
在index.jsp增加如下代码,将数据提交到服务器:
<%@ 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 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script src="<%=path %>/ueditor/ueditor.config.js"></script>
<script src="<%=path %>/ueditor/ueditor.all.min.js"></script>
<script src="<%=path %>/ueditor/lang/zh-cn/zh-cn.js"></script>
<script type="text/javascript">
var editor = UE.getEditor('container');
function postDate() {
}
</script>
</head>
<body>
<form action="<%=path %>/news/addNews.do" method="post">
<textarea id="container" name="content" type="text/plain">这里写你的初始化内容</textarea>
<button id="btn" onclick="postDate()">提交</button>
</form>
</body>
</html>
controller写法:
package com.xingxue.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/news")
public class NewsController {
@RequestMapping("/addNews.do")
public String addNews(String content) {
System.out.println(content);
return "ok";
}
}
测试结果
此时,我们服务器已经能收到数据,就可以存入数据库,或者生产静态html文件,
4、解决图片问题:
先编写好接收图片的controller
package com.xingxue.controller;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
@Controller
@RequestMapping("/news")
public class NewsController {
@RequestMapping("/addNews.do")
public String addNews(String content) {
System.out.println(content);
return "ok";
}
@RequestMapping("/upload.do")
@ResponseBody
public Map<String, Object> images (MultipartFile upfile, HttpServletRequest request, HttpServletResponse response){
Map<String, Object> params = new HashMap<String, Object>();
System.out.println("1111111111111");
try{
String basePath = "e:/img"; //与properties文件中lyz.uploading.url相同,未读取到文件数据时为basePath赋默认值
String visitUrl = "/upload/"; //与properties文件中lyz.visit.url相同,未读取到文件数据时为visitUrl赋默认值
String ext = "abc" + upfile.getOriginalFilename();
String fileName = String.valueOf(System.currentTimeMillis()).concat("_").concat(""+new Random().nextInt(6)).concat(".").concat(ext);
StringBuilder sb = new StringBuilder();
//拼接保存路径
sb.append(basePath).append("/").append(fileName);
visitUrl = visitUrl.concat(fileName);
File f = new File(sb.toString());
if(!f.exists()){
f.getParentFile().mkdirs();
}
OutputStream out = new FileOutputStream(f);
FileCopyUtils.copy(upfile.getInputStream(), out);
params.put("state", "SUCCESS");
params.put("url", visitUrl);
params.put("size", upfile.getSize());
params.put("original", fileName);
params.put("type", upfile.getContentType());
} catch (Exception e){
params.put("state", "ERROR");
}
return params;
}
}
该接收图片有几个细节:
第一: MultipartFile upfile 参数名必须和我们ueditro配置文件名字一样:
返回数据类型不能错误:
state:上传图片的状态
url:访问图片的路径
ueditro配置文件修改:
更改jspdiamante:
<%@ 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 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script src="<%=path %>/ueditor/ueditor.config.js"></script>
<script src="<%=path %>/ueditor/ueditor.all.min.js"></script>
<script src="<%=path %>/ueditor/lang/zh-cn/zh-cn.js"></script>
<script type="text/javascript">
var editor = UE.getEditor('container');
UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;
UE.Editor.prototype.getActionUrl = function(action){
if(action == '/news/upload.do'){
return '/news/upload.do';
}else{
return this._bkGetActionUrl.call(this, action);
}
}
</script>
</head>
<body>
<form action="<%=path %>/news/addNews.do" method="post">
<textarea id="container" name="content" type="text/plain">这里写你的初始化内容</textarea>
<button id="btn" onclick="postDate()">提交</button>
</form>
</body>
</html>
注意:
return ‘/news/upload.do’; //ssm要带项目名, springboot不需要带项目名
测试效果:
注意:图片的路径我们可以用一个虚拟的图片服务器来模拟,需要修改controller的代码
package com.xingxue.controller;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
@Controller
@RequestMapping("/news")
public class NewsController {
@RequestMapping("/addNews.do")
public String addNews(String content) {
System.out.println(content);
return "ok";
}
@RequestMapping("/upload.do")
@ResponseBody
public Map<String, Object> images (MultipartFile upfile, HttpServletRequest request, HttpServletResponse response){
Map<String, Object> params = new HashMap<String, Object>();
System.out.println("1111111111111");
try{
String basePath = "e:/img"; //与properties文件中lyz.uploading.url相同,未读取到文件数据时为basePath赋默认值
String ext = "abc" + upfile.getOriginalFilename();
String fileName = String.valueOf(System.currentTimeMillis()).concat("_").concat(""+new Random().nextInt(6)).concat(".").concat(ext);
StringBuilder sb = new StringBuilder();
//拼接保存路径
sb.append(basePath).append("/").append(fileName);
//写到e盘
File f = new File(sb.toString());
if(!f.exists()){
f.getParentFile().mkdirs();
}
OutputStream out = new FileOutputStream(f);
FileCopyUtils.copy(upfile.getInputStream(), out);
//虚拟图片服务器
String visitUrl = "http://localhost:9999/ssm/";
visitUrl = visitUrl.concat(fileName);
f = new File("D:/server/project/ssm/" + fileName);
out = new FileOutputStream(f);
FileCopyUtils.copy(upfile.getInputStream(), out);
params.put("state", "SUCCESS");
params.put("url", visitUrl);
params.put("size", upfile.getSize());
params.put("original", fileName);
params.put("type", upfile.getContentType());
} catch (Exception e){
e.printStackTrace();
params.put("state", "ERROR");
}
return params;
}
}
SpringBoot整合富文本编辑器(UEditor)的更多相关文章
- springboot+layui 整合百度富文本编辑器ueditor入门使用教程(踩过的坑)
springboot+layui 整合百度富文本编辑器ueditor入门使用教程(踩过的坑) 写在前面: 富文本编辑器,Multi-function Text Editor, 简称 MTE, 是一 ...
- Spring+SpringMVC+MyBatis+easyUI整合优化篇(六)easyUI与富文本编辑器UEditor整合
日常啰嗦 本来这一篇和接下来的几篇是打算讲一下JDBC和数据库优化的,但是最近很多朋友加我好友也讨论了一些问题,我发现大家似乎都是拿这个项目作为练手项目,作为脚手架来用的,因此呢,改变了一下思路,JD ...
- easyUI整合富文本编辑器KindEditor详细教程(附源码)
原因 在今年4月份的时候写过一篇关于easyui整合UEditor的文章Spring+SpringMVC+MyBatis+easyUI整合优化篇(六)easyUI与富文本编辑器UEditor整合,从那 ...
- 百度Web富文本编辑器ueditor在ASP.NET MVC3项目中的使用说明
====================================================================== [百度Web富文本编辑器ueditor在ASP.NET M ...
- 百度富文本编辑器UEditor安装配置全过程
网站开发时富文本编辑器是必不可少的,他可以让用户自行编辑内容的样式然后上传到后台!下面我们来介绍如何安装使用百度富文本编辑器 一.下载并且设置百度富文本编辑器的样式 你可以去百度UEditor ...
- 百度富文本编辑器ueditor使用总结
最近做的项目用到了ueditor这个东东,但是他的一些配置文档对初次使用者来说很难以理解,故作此总结 相关详细操作链接地址: http://blog.csdn.net/wusuopubupt/arti ...
- 富文本编辑器UEditor自定义工具栏(二、插入图片、音频、视频个性化功能按钮和弹层及自定义分页符)
导读:本篇将简单探讨插入图片.音频.视频的功能按钮实现方式 传送门:富文本编辑器UEditor自定义工具栏(一.基础配置与字体.背景色.行间距.超链接实现) 一.效果图 1.UEditor自定义工具栏 ...
- 富文本编辑器UEditor自定义工具栏(三、自定义工具栏功能按钮图标及工具栏样式简单修改)
导读 富文本编辑器UEditor提供丰富了定制配置项,如果想设置个性化的工具栏按钮图标有无办法呢?答案是肯定的!前两篇博文简要介绍了通过将原工具栏隐藏,在自定义的外部按钮上,调用UEditor各命令实 ...
- vue+富文本编辑器UEditor
vue+富文本编辑器UEditor 昨天的需求是把textarea换成富文本编辑器的形式, 网上找了几种富文本编辑器ueditor.tinymce等, 觉得ueditor实现双向绑定还挺有意思, 分享 ...
- 关于富文本编辑器ueditor(jsp版)上传文件到阿里云OSS的简单实例,适合新手
关于富文本编辑器ueditor(jsp版)上传文件到阿里云OSS的简单实例,适合新手 本人菜鸟一枚,最近公司有需求要用到富文本编辑器,我选择的是百度的ueditor富文本编辑器,闲话不多说,进入正 ...
随机推荐
- 从Delphi到Lazarus——我的编程之路
今天终于下定决心,把我使用的编程环境从Delphi转变成Lazarus了.这也许是一个明智的选择,但做出这个决定的过程包含了辛酸和无奈. 这应该是我第三次安装Lazarus了.以前安装之后总是感觉有很 ...
- nvm安装node.js无法使用
前情 最近在使用某此第三方模块需要依赖不同的node版本,于是想通nvm来管理node版本 坑 网上下载nvm-window的安装包,一步步傻瓜式安装下去,发现nrm无法使用,设置环境变量也没有用,再 ...
- HttpClientFactory in ASP.NET Core 2.1 Part 5: 日志
HttpClientFactory in ASP.NET Core 2.1 Part 5: 日志 原文地址:https://www.stevejgordon.co.uk/httpclientfacto ...
- Navicat Premium15安装与激活
Navicat premium是一款数据库管理工具,是一个可多重连线资料库的管理工具,它可以让你以单一程式同时连线到 MySQL.SQLite.Oracle 及 PostgreSQL 资料库,让管理不 ...
- Qt开发经验小技巧171-175
在Qt编程中经常会遇到编码的问题,由于跨平台的考虑兼容各种系统,而windows系统默认是gbk或者gb2312编码,当然后期可能msvc编译器都支持utf8编码,所以在部分程序中传入中文目录文件名称 ...
- Qt编写安防视频监控系统57-子模块1设备列表
一.前言 近期在经历过这次UI大重构以后,很多拆分的功能都以单独的模块的形式出现,以悬停窗体的形式嵌入或者悬浮在主窗体中,这种方式极大的增强了系统的拓展性,客户想要什么模块就开启什么模块,放置到合适的 ...
- 开源轻量级 IM 框架 MobileIMSDK v6.1.2 发布!
一.更新内容简介 本次更新为次要版本更新,进行了若干优化(更新历史详见:码云 Release Nodes).可能是市面上唯一同时支持 UDP+TCP+WebSocket 三种协议的同类开源IM框架. ...
- vue3-openlayers基础知识简介
vue3-openlayers基础知识简介 OpenLayers 3 Primer openlayers6:入门基础(一) openlayers 入门教程 一.基础概念介绍 地图(Map) OpenL ...
- 《计算机体系结构与SoC设计》(一)
1. 天机芯片 "天机"芯片,全称为"天机芯"(Tianjic),是由清华大学施路平团队研发的一款类脑计算芯片.这款芯片在2019年登上了世界顶级学术杂志< ...
- 前端学习openLayers配合vue3(面的绘制,至少三个点)
我们学习了点和线的绘制,当然我们也可以绘制一个面 关键代码,需要注意的一点就是面的绘制需要三维数组,线的绘制是个二维数组 const polygonLayer = new VectorLayer({ ...