富文本编辑器是网站中十分常用的一个组件,无论是网站后台发布信息,前台评论回复发帖等操作,都离不开富文本编辑器的身影。
顶着百度的名头,UEditor是一个功能十分强大的富文本编辑器。然而不得不说,这货的文档写的实在是太烂了。
在刚刚开始使用时,就遇到了读取后台配置文件错误的问题,导致上传图片等功能不能使用。
##初见
在百度的UEditor官网可以下载到各个版本的编辑器,我选择的是jsp+utf8的版本。
然后你会欣慰的在顶部导航看到“文档”两个大字。
我对灯发誓,已经仔细的看过了这个文档。
然而谁要你来教我怎么部署jsp啊!!!
看来只能闭着眼睛摸索前进了,下载下来的文件结构如图所示
总体还算清晰明了。后台引用了jsp/lib下的包,然后我阅读了controller.jsp……
噩梦开始
<%@ page language="java" contentType="text/html; charset=UTF-8"
import="com.baidu.ueditor.ActionEnter"
pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true" %>
<%
request.setCharacterEncoding( "utf-8" );
response.setHeader("Content-Type" , "text/html");
String rootPath = application.getRealPath( "/" );
out.write( new ActionEnter( request, rootPath ).exec() );
%>
复制代码
这段代码其实还比较好理解。我将它翻译到了Spring中如下所示:
@RequestMapping("editorConfig")
public String editor (HttpServletRequest request,HttpServletResponse response) {
try {
request.setCharacterEncoding("utf-8");
response.setHeader("Content-Type", "text/html");
ServletContext application = request.getServletContext();
String rootPath = application.getRealPath("/");
String result = new ActionEnter(request, rootPath).exec();
return result;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return "";
}
复制代码
恩,我去读了它的源码,发现这个东西返回的就是一个字符串,看我给的url,你猜它是个什么字符串?
然而事情哪有这么简单?
##填坑
根据文档(你终于有用了)在浏览器调用url
http://localhost:8080/user/editorConfig?action=config
复制代码
然后我得到了一串unicode码,上网翻译后是:“配置文件加载失败”
###第一步 标准解
首先看配置文件在哪:
String rootPath = application.getRealPath("/");
复制代码
再对比一下路径: “/” “/resource/ueditor/jsp/config.json”
恩,貌似情况很明显了,修改如下:
String rootPath = application.getRealPath("/resource/ueditor");
复制代码
兴高采烈的重新部署项目,启动服务器,然后坚定地给我返回了一行unicode:
“配置文件加载失败”
第二步 追根溯源
失败后继续思考,既然加载失败,肯定就是获取不到文件,由于/resource/ueditor目录也明显没有指向config.json这个文件,那么这个坑爹的东西肯定在某个地方加上了什么奇怪的路径。
幸亏官方提供了源码,继续读源码,首先看启动类ActionEnter的构造方法:
public ActionEnter ( HttpServletRequest request, String rootPath ) {
this.request = request;
this.rootPath = rootPath;
this.actionType = request.getParameter( "action" );
this.contextPath = request.getContextPath();
this.configManager = ConfigManager.getInstance( this.rootPath, this.contextPath, request.getRequestURI() );
}
复制代码
我看到了什么?一个野生的ConfigManager!我赌五毛肯定是在这里加载的配置文件,于是继续追下去看看,可以找到它的工厂方法和构造方法
/*
* 通过一个给定的路径构建一个配置管理器, 该管理器要求地址路径所在目录下必须存在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.initEnv();
}
/**
* 配置管理器构造工厂
* @param rootPath 服务器根路径
* @param contextPath 服务器所在项目路径
* @param uri 当前访问的uri
* @return 配置管理器实例或者null
*/
public static ConfigManager getInstance ( String rootPath, String contextPath, String uri ) {
try {
return new ConfigManager(rootPath, contextPath, uri);
} catch ( Exception e ) {
return null;
}
}
复制代码
我肯定它的注释有好多个版本没有更新过了,但是并不影响我们理解它的意思。但是你为啥就不把这句话写到文档里面去呢?
罪魁祸首就在这里:
this.originalPath = this.rootPath + uri.substring( contextPath.length() );
复制代码
它又在基础路径上添加了浏览器的Uri路径!
鉴于spring的路径不是那么标准,于是这个路径已经歪歪扭扭的歪到了这里:
/my/tomcat/path/webapps/application/resource/ueditor/user/editorConfig
复制代码
找到了地方!我愉快的把config.json文件复制了过去
兴高采烈的重新部署项目,启动服务器,然后坚定地给我返回了一行unicode:
“配置文件加载失败”
第三步 终结
欲哭无泪的我只好继续找下去
于是终于在ConfigManager中发现了这个方法:
private void initEnv () throws FileNotFoundException, IOException {
File file = new File( this.originalPath );
if ( !file.isAbsolute() ) {
file = new File( file.getAbsolutePath() );
}
this.parentPath = file.getParent();
String configContent = this.readFile( this.getConfigPath() );
try{
JSONObject jsonConfig = new JSONObject( configContent );
this.jsonConfig = jsonConfig;
} catch ( Exception e ) {
this.jsonConfig = null;
}
}
复制代码
好哒,这个亲又取了一层parent!!!
找到答案之后回想一下倒是也有道理。这个路径本来指向controller.jsp文件,又有config.json与此文件在同一目录下,取parent才是正确的打开方式
再次将config.json复制了过去, 再次启动项目,终于成功的显示出了配置文件的内容!!