UEditor有着丰富的面板和功能。官方提供了JSP版本,但是这个JSP版本在JEE应用(如struts 1&2以及Spring MVC)中,如果使用这个版本,就有点让人很不爽了。焦点在于文件上传这一块。在我们的很多项目中,我们都是自己写上传模板,按照自己的规则对上传文件进行判断、改名以及指定存放目录。
在jsp版本中,输入以下地址,会返回一大堆信息。
http://host/scripts/js/ueditor-1.4.3.3/jsp/controller.jsp?action=config
于是按照网上查阅的内容,自己写个handler。
package com.lerx.handlers;
import com.baidu.ueditor.ActionEnter;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
@Controller
@Scope("prototype")
@RequestMapping("/action_uedit")
public class UEditorController {
@RequestMapping(value="/config")
public void config(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
request.setCharacterEncoding( "utf-8" );
response.setHeader("Content-Type" , "text/html");
response.setContentType("application/json");
String rootPath = request.getSession()
.getServletContext().getRealPath("/");
try {
String exec = new ActionEnter(request, rootPath).exec();
PrintWriter writer = response.getWriter();
writer.write(exec);
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
在富文本编辑器所在html页面中,按照官方的方法,更改代码:
<script type="text/javascript">
UE.getEditor('content');
UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;
UE.Editor.prototype.getActionUrl = function(action) {
if (action == 'uploadimage' || action == 'uploadscrawl' ) {
var uploadpath=contextPath+"/action_file/uploadForUEditor";
return uploadpath;
} else if (action == 'uploadvideo' || action == 'uploadfile') {
var uploadpath=contextPath+"/action_file/uploadForUEditor?intact=true";
return uploadpath;//此处写自定义的图片上传路径
} else {
return this._bkGetActionUrl.call(this, action);
}
}
$("#content").css("width","100%");
$("#content").css("height","250px");
$('#content').css('display', '');
</script>
但此时发现上传按钮变灰,不可使用。即便是不变灰,也不能上传。
在浏览器中输入自己的handler地址http://localhost:8080/LerxV5/action_uedit/config?action=config时,却返回: \u65e0\u6548\u7684Action(无效的Action)。
于是只有来看源码。
在官方提供的版本中,有五个jar文件。其中有一个是UEditor专用的,名称为ueditor-1.1.2.jar。
分析后发现,原来ueditor启动时,会寻找config.json这个文件,在jsp版本中,官方通过jsp文件的定位,自然就找到这个文件了。官方在udeitor-1.1.2中,将这个查找文件的方法写死了,就是查找执行JSP文件的当前目录。
但是在我们真正的Java项目中,这个就问题很大了。因为我们根本不用jsp,就无法找到这个文件。
于是,只有修改源码。
在com.baidu.ueditor.ConfigManager中。
增加一个方法来寻找classes目录的真实位置。
private static String classesPath()
{
if (!(Thread.currentThread().isAlive()))
return null;
String classesPath = Thread.currentThread().getContextClassLoader().getResource("/").getPath();
return classesPath;
}
修改getConfigPath():
private String getConfigPath()
{
return classesPath() + "ueditor.config.json.txt";
}
因为在eclipse中,对百度的这个json文件出现警告(原因是里面有很多注释,json格式文件不支持),所以我把它加了一个txt后缀,eclipse就不报警了。然后把它放在src(即最终的classes)目录下。
重新进行jar打包。再在浏览器中输入
http://localhost:8080/LerxV5/action_uedit/config?action=config。
此时出现了和官方的jsp版本一模一样的正确返回。
此时发现,自己写的上传模板也正常了。一切全部搞定。
【说明】:文中所说的udeitor-1.1.2,已打包在lerxV6版本中,单独使用,可以用下面的网址下载。
http://www.lerx.com/html/g7/2019/09-26/092828572-130.html