springMVC -- 整合UEditor(富文本编辑器)
工作中需要用到UEditor编辑文本,在与springMVC进行整合时,出现了一些问题,结果导致,在进行图片上传时出现如下提示:
上网查询了很多相关资料,此处简要记录下,防止以后遇到类似问题。
一种方式是直接修改源码,步骤如下:
1、编写controller 如下(该接口是ueditor前后台交互的统一路径) :
package com.test.dcdp.controller; import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import com.baidu.ueditor.ActionEnter; @Controller
@RequestMapping("/ueditor")
public class UeditorController { @RequestMapping("/dispatch")
public void config(HttpServletRequest request, HttpServletResponse response) {
// response.setContentType("application/json");
String rootPath = request.getSession().getServletContext().getRealPath("/");
response.setHeader("Content-Type" , "text/html");
try {
String a = request.getRequestURI();
String exec = new ActionEnter(request, rootPath).exec();
PrintWriter writer = response.getWriter();
writer.write(exec);
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
} }
}
2、修改ueditor的配置文件 ueditor.config.js(指定后台服务器地址),如下所示
修改前:
var URL = window.UEDITOR_HOME_URL || getUEBasePath(); /**
* 配置项主体。注意,此处所有涉及到路径的配置别遗漏URL变量。
*/
window.UEDITOR_CONFIG = { //为编辑器实例添加一个路径,这个不能被注释
UEDITOR_HOME_URL: URL // 服务器统一请求接口路径
, serverUrl: URL + "php/controller.php"
修改后 :
var getRootPath = function (){
//获取当前网址
var curWwwPath=window.document.location.href;
//获取主机地址之后的目录
var pathName=window.document.location.pathname;
var pos=curWwwPath.indexOf(pathName);
//获取主机地址
var localhostPaht=curWwwPath.substring(0,pos);
//获取带"/"的项目名,如:/uimcardprj
var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
return(localhostPaht+projectName);
}
//获取路径
var applicationPath = getRootPath();
var URL = window.UEDITOR_HOME_URL || getUEBasePath();
var serverURL = applicationPath; /**
* 配置项主体。注意,此处所有涉及到路径的配置别遗漏URL变量。
*/
window.UEDITOR_CONFIG = { //为编辑器实例添加一个路径,这个不能被注释
UEDITOR_HOME_URL: URL // 服务器统一请求接口路径
, serverUrl: serverURL + "ueditor/dispatch"
3、修改ueditor源码 ConfigManager.java(指定配置文件路径).
修改前 :
/*
* 通过一个给定的路径构建一个配置管理器, 该管理器要求地址路径所在目录下必须存在config.properties文件
*/
private ConfigManager ( String rootPath, String contextPath, String uri ) throws FileNotFoundException, IOException { rootPath = rootPath.replace( "\\", "/" ); this.rootPath = rootPath;
this.contextPath = contextPath; if ( contextPath.length() > ) {
this.originalPath = this.rootPath + uri.substring( contextPath.length() );
} else {
this.originalPath = this.rootPath + uri;
} this.initEnv(); }
修改后 :
/*
* 通过一个给定的路径构建一个配置管理器, 该管理器要求地址路径所在目录下必须存在config.properties文件
*/
private ConfigManager ( String rootPath, String contextPath, String uri ) throws FileNotFoundException, IOException { rootPath = rootPath.replace( "\\", "/" ); this.rootPath = rootPath;
this.contextPath = contextPath; /*if ( contextPath.length() > 0 ) {
this.originalPath = this.rootPath + uri.substring( contextPath.length() );
} else {
this.originalPath = this.rootPath + uri;
}*/ this.originalPath = rootPath + "static" + File.separator + "lib" + File.separator +
"ueditor" + File.separator + "1.4.3" + File.separator + "jsp" + File.separator + "controller.jsp";
///EdwManage/src/main/webapp/static/lib/ueditor/1.4.3/jsp/controller.jsp this.initEnv(); }
如上所述,主要修改 originalPath 的路径,否则ueditor的配置文件(ueditor_config.json)路径是错误的(与springMVC整合的情况),之所以向上面那样拼接路径,是因为我的ueditor相关文件拷贝在了(EdwManage/src/main/webapp/static/lib/ueditor/1.4.3/jsp/controller.jsp)路径里。
4、(若未执行该步操作,在选择好图片后,点击上传,将提示 : “未找到上传文件”)由于上传的文件都会被springmvc的文件上传拦截器拦截,包装,这样百度编辑器接收到文件后不能识别文件格式,因此把spring默认的commonsMultiparResolver,替换成我们自己写的commonsMultiparResolver ,并修改配置文件。
重写CommonsMultipartResolver :
package com.tianwen.dcdp.common; import org.springframework.web.multipart.commons.CommonsMultipartResolver; public class CommonsMultiparResolver extends CommonsMultipartResolver { @Override
public boolean isMultipart(javax.servlet.http.HttpServletRequest request) {
String uri = request.getRequestURI();
System.out.println(uri);
//过滤使用百度UEditor的URI
if (uri.indexOf("ueditor/dispatch") > ) { //此处拦截路径即为上面编写的controller路径
System.out.println("commonsMultipartResolver 放行");
return false;
}
System.out.println("commonsMultipartResolver 拦截");
return super.isMultipart(request);
}
}
修改springMVC配置文件spring-mvc.xml :
<!-- 修改为我们重写的CommonsMultiparResolver而不是spring提供的 -->
<bean id="multipartResolver"
class="com.tianwen.dcdp.common.CommonsMultiparResolver">
<!-- 默认编码 -->
<property name="defaultEncoding" value="utf-8" />
<!-- 文件大小最大值 -->
<property name="maxUploadSize" value="" />
<!-- 内存中的最大值 -->
<property name="maxInMemorySize" value="" />
</bean>
5、最后配置上传文件保存目录,修改ueditor配置文件(ueditor_config.json):
修改如下参数(即配置上传文件的URL路径,若配置不正确,富文本编辑器中将不能正确显示上传的图片):
"imageUrlPrefix": "http://localhost:80/EdwManage", /* 图片访问路径前缀 */
"imagePathFormat": "/static/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上传保存路径,可以自定义保存路径和文件名格式 */
此处 imagePathFormat 之所以配置为如上格式,是因为springMVC中,指定了static目录下的资源为静态资源(其他路径都会被springMVC拦截)。
文件默认保存的物理路径为: web应用根路径 + imagePathFormat 。
{yyyy}{mm}{dd} : 按天分类保存
{time}{rand:6} : 随机生成文件名
另外还有一种简单的解决办法:
1、新建一web工程(ueditor)。
2、将下载下来的ueditor文件拷贝到新建工程 的webapps目录下,可参考官网介绍。
3、在使用ueditor的工程中,修改ueditor配置文件(ueditor.config.js)如下 :
window.UEDITOR_HOME_URL = "http://localhost/ueditor/";
var URL = window.UEDITOR_HOME_URL || getUEBasePath(); /**
* 配置项主体。注意,此处所有涉及到路径的配置别遗漏URL变量。
*/
window.UEDITOR_CONFIG = { //为编辑器实例添加一个路径,这个不能被注释
UEDITOR_HOME_URL: URL // 服务器统一请求接口路径
, serverUrl: URL+ "jsp/controller.jsp"
3、配置上传文件保存路径,修改(ueditor_config.json) :
"imageUrlPrefix": "http://localhost:80/ueditor", /* 图片访问路径前缀 */
"imagePathFormat": "/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上传保存路径,可以自定义保存路径和文件名格式 */
springMVC -- 整合UEditor(富文本编辑器)的更多相关文章
- springMVC -- 对接UEditor(富文本编辑器)
工作中需要用到UEditor编辑文本,在与springMVC进行整合时,出现了一些问题,结果导致,在进行图片上传时出现如下提示: 上网查询了很多相关资料,此处简要记录下,防止以后遇到类似问题. 一种方 ...
- JAVA SpringBoot2 整合 JSP视图模板 整合 Ueditor富文本编辑器
一般涉及到后台管理系统,就少不了富文本编辑器,这个可以图,文,视频混排的高级工具,笔者通过对比,发现目前市场上最好的三方库还当属百度的 ueditor 近年来 SpringBoot 框架可谓越来越火, ...
- springboot+layui 整合百度富文本编辑器ueditor入门使用教程(踩过的坑)
springboot+layui 整合百度富文本编辑器ueditor入门使用教程(踩过的坑) 写在前面: 富文本编辑器,Multi-function Text Editor, 简称 MTE, 是一 ...
- 前后端分离ueditor富文本编辑器的使用-Java版本
最近在写一个自己的后台管理系统(主要是写着玩的,用来熟悉后端java的知识,目前只是会简单的写点接口),想在项目中编写一个发布新闻文章的功能,想到了使用百度的ueditor富文本编辑器,网上找了很多j ...
- MVC 使用 Ueditor富文本编辑器
一.Ueditor 1.下载Ueditor富文本编辑器 官方下载地址: http://ueditor.baidu.com/website/download.html 建议下载开发版,此处我下载的是 . ...
- 百度ueditor富文本编辑器的使用
百度ueditor富文本编辑器的使用 //以下为我在官网下载的ueditor v1.3.5 php版的大楷配置步骤第一步: //配置文件的引入应该比功能文件先引入,最后设置语言类型.即:editor. ...
- ASP.NET MVC5 中百度ueditor富文本编辑器的使用
随着网站信息发布内容越来越多,越来越重视美观,富文本编辑就是不可缺少的了,众多编辑器比较后我选了百度的ueditor富文本编辑器. 百度ueditor富文本编辑器分为两种一种是完全版的ueditor, ...
- PHP如何搭建百度Ueditor富文本编辑器
本文为大家分享了PHP搭建百度Ueditor富文本编辑器的方法,供大家参考,具体内容如下 下载UEditor 官网:下载地址 将下载好的文件解压到thinkphp项目中,本文是解压到PUBLIC目录下 ...
- ueditor富文本编辑器使用百度地图自定义动态地图组件及兼容https及http协议
ueditor富文本编辑器默认支持百度地图组件,但是如果导入动态地图后会加很多默认的地图组件在上面.如果需要自定义动态地图的组件则需要修改ueditor特定的html. ueditor百度地图组件所在 ...
随机推荐
- Ecshop开发
http://www.cnblogs.com/xcxc/category/579565.html
- linux平台的office文档转pdf(程序员的菜)
需要材料: 1. Openoffice3.4(我是32位的centos,可以根据自己的系统下载指定的openoffice软件包) 下载地址:http://sourceforge.net/projec ...
- astyle代码格式化
Artistic Style 1.24 A Free, Fast and Small Automatic Formatterfor C, C++, C#, and Java Source Code 项 ...
- Oracle 搜集统计信息
常用的统计信息收集脚本: 非分区表: BEGIN DBMS_STATS.GATHER_TABLE_STATS(ownname => 'SCOTT', ...
- bzoj3209
首先这道题目不难想到将答案转化为这种形式 2^s[2]*3*s[3]*…max*s[max] 这时候我们要分类讨论,设n的二进制位数为t 当1~n中二进制位数小于t时 我们可以直接用组合的知识,二进制 ...
- 依据 smtp协议的简单golang 的发邮件实现
依据 smtp协议的简单golang 的发邮件实现 协议格式如下 From:sender_user@demo.net To:to_user@demo.net Subject:这是主题 Mime-Ver ...
- Delphi安装/卸载OCX控件的方法
delphi 安装卸载ocx 请参见 如下 http://blog.csdn.net/xt_chaoji/article/details/7027298 打开Delphi,关闭所有项目. 1. ...
- AOP Aspect Oriented Programming
原理AOP(Aspect Oriented Programming),也就是面向方面编程的技术.AOP基于IoC基础,是对OOP的有益补充. AOP将应用系统分为两部分,核心业务逻辑(Core bus ...
- 封装获取网络信息Linux—API类
封装获取网络信息Linux—API类 封装好的库: #ifndef NETINFORMATION_H #define NETINFORMATION_H #include <netdb.h> ...
- 600字读懂 Git
译注:来自 Hacker School 的 Mary Rose Cook 实现了一个纯 JavaScript 写就的 Git:Gitlet,包含了最主要的一些命令.这个项目一是为了了解 Git 内部原 ...