1、下载UEditor,这里选择JSP(UTF8)版本和完整源码 。在你的项目中引入JSP(UTF8)版本的文件和完整源码对应的jsp源码。
2、使用maven引入对应的jar包(也可以同过直接引用ueditor自带的包)
<!-- ueditor依赖项 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
<!-- org.json -->
<!--JSON is a light-weight, language independent, data interchange format.
See http://www.JSON.org/ -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20140107</version>
</dependency>
3、修改jsp/config.json文件,
注意:上传保存路径对应的是项目的根目录(不需要添加项目名)。
但是如果这样上传成功之后,返回的地址有问题(同样也没有添加项目名称)。应次同时需要添加访问路径前缀(加入你的项目名称)。
如下图:
上传的路径就是 /LearnSpringMVCDemo/upload/…(默认在LearnSpringMVCDemo的根目录下)
返回的上传路径就是 /LearnSpringMVCDemo/upload/…(LearnSpringMVCDemo通过路径前缀加入)
4、添加静态文件的访问配置:
我的文件在resources目录下,同时上传的文件在根目录下。
<mvc:resources location="classpath:js/" mapping="/js/**" />
<mvc:resources location="classpath:css/" mapping="/css/**" />
<mvc:resources location="classpath:img/" mapping="/img/**" />
<mvc:resources location="classpath:ueditor/" mapping="/ueditor/**" />
<mvc:resources location="/upload/" mapping="/upload/**" />
5、根据springMVC的规则添加对应的controller,替换掉ueditor自带的controller.jsp文件(可以删除该文件)。同时修改ueditor.config.js对应的程序入口。
// ueditor.config.js
// 修改1
window.UEDITOR_HOME_URL = "/LearnSpringMVCDemo/ueditor/";
var URL = window.UEDITOR_HOME_URL || getUEBasePath();
/**
* 配置项主体。注意,此处所有涉及到路径的配置别遗漏URL变量。
*/
window.UEDITOR_CONFIG = {
// 为编辑器实例添加一个路径,这个不能被注释
UEDITOR_HOME_URL : URL
// 服务器统一请求接口路径
,
// 修改2
serverUrl : URL + "config.do"
// 对应的服务器统一请求入口
package com.learndemo.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(value = "/ueditor")
public class UeditorController {
@RequestMapping(value = "/config.do")
public void config(HttpServletRequest request, HttpServletResponse response) {
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();
}
}
}
6、修改源文件使其对应springMVC的规则
// ConfigManager.java
/*
* 通过一个给定的路径构建一个配置管理器, 该管理器要求地址路径所在目录下必须存在config.properties文件
*/
private ConfigManager(String rootPath, String contextPath, String uri)
throws FileNotFoundException, IOException, URISyntaxException {
rootPath = rootPath.replace("\\", "/");
this.rootPath = rootPath;
this.contextPath = contextPath;
// 第一处修改
// 因此这里我们需要求出配置文件的物理路径
// 我这里是 /我的项目/WEB-INF/classes/ueditor/jsp/config.json
this.parentPath = this.rootPath + "WEB-INF" + File.separator + "classes" + File.separator + "ueditor"
+ File.separator + "jsp";
this.initEnv();
}
// 第二处修改
private void initEnv() throws FileNotFoundException, IOException {
String configContent = this.readFile(this.getConfigPath());
try {
JSONObject jsonConfig = new JSONObject(configContent);
this.jsonConfig = jsonConfig;
} catch (Exception e) {
this.jsonConfig = null;
}
}
注:上述的配置文件物理路径指的是我发布在tomcat的路径。可以通过这里配置。
修改的目的是,这里需要获取配置文件config.json的配置。但是使用了springMVC之后,得到的url地址就会出问题。因此需要在这里手动更改写入你对应文件的物理路径。
至此UEditor便可以正常使用了