问题描述:无法上传图片,提示配置项加载有问题

大致情形:直接下载的ue编辑器,放在了/resources/   目录下,也就是静态资源路径,然后更改web.xml,将tomcat默认拦截器配置放到所有servlet的最上面

1     <!-- 不拦截静态文件 -->  
2     <servlet-mapping>  
3         <servlet-name>default</servlet-name>  
4         <url-pattern>/resources/*</url-pattern>  
5     </servlet-mapping>  

可即使是这样,打开demo界面,点图片上传,提示后端配置未正常加载,手动输入百度ue的controller.jsp地址时却输出的是controller.jsp的源码,不放弃,继续:

查看官方文档得知默认会访问controller.jsp是因为如下配置项(ps:图是改过后的,非默认配置),既然这样,那咱们就自己定义个controller,把controller.jsp的代码copy到自己的controller里面,然后更改图中配置项来处理ue的请求:

SSM整合项目中使用百度Ueditor遇到的问题。

说做就做:

 1 package **********************************;
 2 
 3 import javax.servlet.http.HttpServletRequest;
 4 import javax.servlet.http.HttpServletResponse;
 5 
 6 import org.springframework.stereotype.Controller;
 7 import org.springframework.web.bind.annotation.RequestMapping;
 8 import org.springframework.web.bind.annotation.RequestParam;
 9 
10 import com.baidu.ueditor.ActionEnter;
11 
12 @Controller
13 @RequestMapping("/ueditor")
14 public class UeditorController {
15 
16     @RequestMapping("/upload")
17     public void exec(@RequestParam(value="action",required=false)String action,
18             @RequestParam(value="callback",required=false)String callback,
19             HttpServletRequest req, HttpServletResponse res) {
20         try {
21             req.setCharacterEncoding("utf-8");
22             res.setHeader("Content-Type", "text/html");
23             String rootPath =req.getSession().getServletContext().getRealPath("/");
24             res.getWriter().write(new ActionEnter(req, rootPath,"").exec());
25         } catch (Exception e) {
26             e.printStackTrace();
27         }
28     }
29 }

这下应该可以了吧,no,no,no,还是提示后端服务为正常加载,debug模式走起,原来百度ue有个小问题,归纳起来如下:

首先肯定要加载所有配置,java语言的配置是在config.json中配置的,然后引申出另一个问题,如何获取config.json的真实路径,

原来百度的大神们是这样做的:假设你们可以访问到ue的初始页面,也就是可以看到编辑框,那么ue在编辑框初始化的时候就发个询问后端配置是否正常的get请求,就是ueditor.config.js中配置的serverUrl,然后加个参数 ?action=config,代码里最后走这行代码:

1 res.getWriter().write(new ActionEnter(req, rootPath,"").exec());

rootpath是项目的真实物理路径,而后用request.getRequestURI获取访问路径,两者加到一起,就获取到了config.json文件所在路径的文件夹(假设还是访问的controller.jsp,那么是没问题的.ps:一改问题就多。。。),这样就可以加载config.json文件中的所有配置了。然而spring mvc可随意定义url路径,所以,这样做就有问题了,所以就出了上面那个配置项有问题无法上传的提示了。那怎么解决呢?在我走了一段弯路之后怒了,大爷的不就是要加载config.json吗?何必绕那么大的圈子,我直接给你不就完了?于是乎,下载ue的jar包源码,更改ActionEnter的构造方法,将路径传过去,最后的ActionEnter如下:

  1 package com.baidu.ueditor;
  2 
  3 import java.util.Map;
  4 
  5 import javax.servlet.http.HttpServletRequest;
  6 
  7 import com.baidu.ueditor.define.ActionMap;
  8 import com.baidu.ueditor.define.AppInfo;
  9 import com.baidu.ueditor.define.BaseState;
 10 import com.baidu.ueditor.define.State;
 11 import com.baidu.ueditor.hunter.FileManager;
 12 import com.baidu.ueditor.hunter.ImageHunter;
 13 import com.baidu.ueditor.upload.Uploader;
 14 
 15 public class ActionEnter {
 16     
 17     private HttpServletRequest request = null;
 18     
 19     private String rootPath = null;
 20     private String contextPath = null;
 21     
 22     private String actionType = null;
 23     
 24     private ConfigManager configManager = null;
 25 
 26     public ActionEnter ( HttpServletRequest request, String rootPath,String jsonFilePath ) {
 27         
 28         this.request = request;
 29         this.rootPath = rootPath;
 30         this.actionType = request.getParameter( "action" );
 31         this.contextPath = request.getContextPath();
 32         this.configManager = ConfigManager.getInstance( this.rootPath,jsonFilePath, this.contextPath, request.getRequestURI().replace(request.getContextPath(),"") );
 33         
 34     }
 35     
 36     public String exec () {
 37         
 38         String callbackName = this.request.getParameter("callback");
 39         
 40         if ( callbackName != null ) {
 41 
 42             if ( !validCallbackName( callbackName ) ) {
 43                 return new BaseState( false, AppInfo.ILLEGAL ).toJSONString();
 44             }
 45             
 46             return callbackName+"("+this.invoke()+");";
 47             
 48         } else {
 49             return this.invoke();
 50         }
 51 
 52     }
 53     
 54     public String invoke() {
 55         
 56         if ( actionType == null || !ActionMap.mapping.containsKey( actionType ) ) {
 57             return new BaseState( false, AppInfo.INVALID_ACTION ).toJSONString();
 58         }
 59         
 60         if ( this.configManager == null || !this.configManager.valid() ) {
 61             return new BaseState( false, AppInfo.CONFIG_ERROR ).toJSONString();
 62         }
 63         
 64         State state = null;
 65         
 66         int actionCode = ActionMap.getType( this.actionType );
 67         
 68         Map<String, Object> conf = null;
 69         
 70         switch ( actionCode ) {
 71         
 72             case ActionMap.CONFIG:
 73                 return this.configManager.getAllConfig().toString();
 74                 
 75             case ActionMap.UPLOAD_IMAGE:
 76             case ActionMap.UPLOAD_SCRAWL:
 77             case ActionMap.UPLOAD_VIDEO:
 78             case ActionMap.UPLOAD_FILE:
 79                 conf = this.configManager.getConfig( actionCode );
 80                 state = new Uploader( request, conf ).doExec();
 81                 break;
 82                 
 83             case ActionMap.CATCH_IMAGE:
 84                 conf = configManager.getConfig( actionCode );
 85                 String[] list = this.request.getParameterValues( (String)conf.get( "fieldName" ) );
 86                 state = new ImageHunter( conf ).capture( list );
 87                 break;
 88                 
 89             case ActionMap.LIST_IMAGE:
 90             case ActionMap.LIST_FILE:
 91                 conf = configManager.getConfig( actionCode );
 92                 int start = this.getStartIndex();
 93                 state = new FileManager( conf ).listFile( start );
 94                 break;
 95                 
 96         }
 97         
 98         return state.toJSONString();
 99         
100     }
101     
102     public int getStartIndex () {
103         
104         String start = this.request.getParameter( "start" );
105         
106         try {
107             return Integer.parseInt( start );
108         } catch ( Exception e ) {
109             return 0;
110         }
111         
112     }
113     
114     /**
115      * callback参数验证
116      */
117     public boolean validCallbackName ( String name ) {
118         
119         if ( name.matches( "^[a-zA-Z_]+[\\w0-9_]*$" ) ) {
120             return true;
121         }
122         
123         return false;
124         
125     }
126     
127 }
View Code

相关文章:

  • 2021-06-23
  • 2021-06-28
  • 2021-05-31
  • 2021-08-12
  • 2021-08-11
  • 2021-07-07
  • 2021-06-17
猜你喜欢
  • 2021-04-27
  • 2021-11-13
  • 2021-11-17
  • 2021-09-25
  • 2021-05-11
  • 2022-12-23
相关资源
相似解决方案