ueditor为什么无法在springboot中使用
官方提供的Java后端接口是 ueditor/jsp/controller.jsp。 jsp在springboot中存在一些问题,导致ueditor不能正确使用。
1、spingboot的视图可能不使用jsp
2、spingboot项目发布为jar包时,获取不到/ueditor/jsp/config.json文件。
解决思路
这时我们就要自定义controller,来处理ueditor的请求。
ueditor的请求主要分2中,获取配置和上传。
1、当ueditor初始化时,发起请求获取配置文件。请求地址 ueditor/jsp/controller.jsp?action=config。 该接口默认返回/ueditor/jsp/config.json内容。
2、当上传文件/图片时候,会发起请求 ueditor/jsp/controller.jsp?action=uploadXXX。
action的值再/ueditor/jsp/config.json 中搜索ActionName。
解决过程
一、修改配置文件
打开 plugins/ueditor/ueditor.config.js。这里修改URL和serverUrl 2个字段
URL 是ueditor代码根目录地址
serverUrl 是 /Resources/plugins/ueditor/jsp/controller
自定义controller接收请求,RequestMapping要和上一步中的serverUrl 对应
@Controller
@Transactional
@RequestMapping("Resources/plugins/ueditor/jsp")
public class JSPController {
@Autowired
IMgrFileUploadService mgrFileUploadService;
@RequestMapping("/controller")
public void getConfigInfo(HttpServletRequest request, HttpServletResponse response, String action, MultipartFile upfile) throws IOException {
if (action.equals("config")) {
//重定向到配置文件
response.sendRedirect("/Resources/plugins/ueditor/jsp/config.json");
return;
}
if (action.contains("upload")) {
//文件上传方法,自己实现一下
ApiResult apiResult = mgrFileUploadService.uploadMultipartFile(upfile, "img");
Map<String, Object> result = (Map<String, Object>) apiResult.getData();
//ueditor要求返回格式
Map<String, Object> map = new HashMap<>();
//state成功必须返回SUCCESS
map.put("state", "SUCCESS");
//图片地址,/ueditor/jsp/config.json配置中配置的图片前缀为"",所以这里返回完成地址
map.put("url", result.get("img_src"));
map.put("title", result.get("original"));
map.put("original", result.get("original"));
map.put("type", "jpg");
map.put("size", result.get("size"));
//返回json
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json; charset=utf-8");
response.setHeader("Access-Control-Allow-Origin", "*");
PrintWriter out = null;
try {
out = response.getWriter();
out.append(JSON.toJSONString(map));
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
out.close();
}
}
}
}
}