关于项目打包到服务器后UEeditor报Uncaught ReferenceError: errorHandler is not defined错误解决办法

本文针对百度UEditor在SpringMVC项目中的配置问题进行了详细的解析,包括解决本地与服务器环境下config.json文件路径不匹配的问题,通过调整配置文件路径及自定义控制器方法成功解决了这一难题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近做的项目中使用的百度的UEeditor,说实话,百度的这个编辑器真的是好多的坑,API也是讲的简单的不能再简单了,报个错也是让人一头雾水

先简单说下,我们的项目,用的是SpringMVC,配置这些都是按照网上的来配置的,踩了几个坑,好不容易本地测试没问题了,然后打包到服务器上,一访问使用,就变成这样子了,


一看错误:


这他妈玩笑开大了。找了半天没找到问题所在。

最后我琢磨着,应该是没有加载到config.json这个文件,所以才会报这个错。

这是原来的controller.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
   import="com.baidu.ueditor.ActionEnter"
    pageEncoding="UTF-8"%>
<%@ page import="org.apache.log4j.Logger" %>
<%@ page trimDirectiveWhitespaces="true" %>

<%
   Logger logger = Logger.getLogger(this.getClass());
    request.setCharacterEncoding( "utf-8" );
   response.setHeader("Content-Type" , "text/html");
   String action = request.getParameter("action");
   String rootPath = application.getRealPath( "/" );
   logger.info(rootPath);
   String path = new ActionEnter( request, rootPath ).exec();
   if( action!=null && (action.equals("listfile") || action.equals("listimage") ) ){
      rootPath = rootPath.replace("\\", "/");
      path = path.replaceAll(rootPath, "/");

   }
   logger.info(rootPath);
   logger.info(path);
   out.write( path );

%>
因为打包到服务器后,服务器中就是一个war包,这个rootPath我看了,在本地是个项目的绝对路径:D:/workspace/项目名,但在服务器中,就不存在这个路径了,东西在war包里面,那它肯定获取不到config.json的绝对路径了,因为根本没有这个路径了

经过分析,我们就不能让它自己去查找配置文件了,所以,我将config.json这个文件直接复制到项目的classpath(resources)目录中:



然后自己在controller类中添加了方法:

@RequestMapping("ueconfig")
public void getUEConfig(HttpServletRequest request, HttpServletResponse response){
   org.springframework.core.io.Resource res = new ClassPathResource("config.json");
   InputStream is = null;
   response.setHeader("Content-Type" , "text/html");
   try {
      is = new FileInputStream(res.getFile());
      StringBuffer sb = new StringBuffer();
      byte[] b = new byte[1024];
      int length=0;
      while(-1!=(length=is.read(b))){
         sb.append(new String(b,0,length,"utf-8"));
      }
      String result = sb.toString().replaceAll("/\\*(.|[\\r\\n])*?\\*/","");
      JSONObject json = JSON.parseObject(result);
      logger.info(json.toJSONString());
      PrintWriter out = response.getWriter();
      out.print(json.toString());
   } catch (IOException e) {
      e.printStackTrace();
   }finally {
      try {
         is.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}
注意:一定要加上这句话:
response.setHeader("Content-Type" , "text/html");

然后在ueditor.config.js中修改为自己的controller方法路径:

,serverUrl: basePath+"/comm/ueconfig"
测试OK!

希望能够帮到更多的人。



评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值