以下以FreeMarker为例,Velocity同理
1.官网:http://ueditor.baidu.com
下载:http://ueditor.baidu.com/website/download.html
下载jsp版本及源码版本(使用源码版本需要用grunt编译)
这里下源码版本的目的主要是为了获得 jsp/src/com/baidu/ueditor中的代码,可以根据自己需要修改代码。
2.导入必要jar包:
commons-fileupload
commons-fileupload
1.3.1
commons-io
commons-io
2.4
commons-codec
commons-codec
1.9
com.baidu
ueditor-json
1.0.0
3.拷贝代码:
把jsp/src/com/baidu/ueditor中源代码拷贝到工程中。
把ueditor目录拷贝到工程中webapp中,本例中拷贝到了webapp/media目录下。
4.页面中引入UEditor相关脚本:
用编辑器的位置加入
5.这里是重点!!!有2种方案可选择:
第一种方案:这种方案简单一点,由于现有工程的视图解析器只有FreeMarker,并不能直接使用jsp版本的UEditor,需要加入jsp解析器配置,以便UEditor调用jsp中代码,
第二种方案,不配置jsp视图解析器,请求返回时走jsp的视图解析器。
这种方案需要自定义接口“骗过”UEditor,修改UEditor请求的路径,请求自定义的接口。
a.通过看UEditor的js代码可以发现 ueditor.config.js文件配置的请求路径是serverUrl: URL + "jsp/controller.jsp" ,这里要改为自己接口的地址:serverUrl: URL + "jsp/upfile"。(注:这里路径前半段的jsp/不要修改,在UEditor的java代码中会根据这个路径查找config.js文件。如果非要改,就把jsp/config.json换个目录比如conf目录,这样就可以把接口地址改为serverUrl: URL + "conf/upfile")
b.仿照jsp/controller.jsp写接口
jsp:
<%
request.setCharacterEncoding( "utf-8" );
response.setHeader("Content-Type" , "text/html");
String rootPath = application.getRealPath( "/" );
out.write( new ActionEnter( request, rootPath ).exec() );
%>
接口: (一定要支持POST和GET,页面加载时会通过GET请求加载配置。)
@RequestMapping(value = "/media/ueditor/jsp/upfile", method = { RequestMethod.GET, RequestMethod.POST })
@ResponseBody
public String check(HttpServletRequest request) throws IOException {
String rootPath = request.getSession().getServletContext().getRealPath("/");
String exec = new ActionEnter(request, rootPath).exec();
return exec;
}
c.改造com.baidu.ueditor.upload.BinaryUploader类,处理MultipartFile
原:
FileItemIterator iterator = upload.getItemIterator(request);
while (iterator.hasNext()) {
fileStream = iterator.next();
if (!fileStream.isFormField())
break;
fileStream = null;
}
if (fileStream == null) {
return new BaseState(false, AppInfo.NOTFOUND_UPLOAD_DATA);
}
String savePath = (String) conf.get("savePath");
String originFileName = fileStream.getName();
现:
MultipartHttpServletRequest mulReq = (MultipartHttpServletRequest) request;
MultipartFile file = mulReq.getFile("upfile");
String savePath = (String) conf.get("savePath");
String originFileName = file.getOriginalFilename();
6.目前工程是分布式部署,管理后台和提供前台工程查询接口的工程分别部署在不同服务器上,UEditor默认会把图片存储在管理后台的服务器上,需要再次对BinaryUploader进行改造,每次上传到共享文件服务器上,将图片的imageUrl存到State对象中,
storageState.putInfo("url", PathFormat.format(imageUrl));
附:UEditor相关API参照以下js