在网上下载了xheditor作为页面的编辑器,编辑内容后post到后台保存,后台方法用spring mvc的自动注入的方式接收参数。

这种方式在各个浏览器下运行良好,但是在ie11下发现,从word、文本编辑器或者其它编辑器复制内容到xheditor后,这时提交到后台的参数不能被接收到。

仔细排查下发现ie11下复制到xheditor的内容都被默默的加了一段无用的div:

<div style="top: 0px;">
</div>

 

此时用最原始的接收参数的方式:request.getParameter(“name”);也取不到值。但是抓包看浏览器的交互信息,编辑器里的内容确实被提交到了后台。

于是猜想这可能是spring mvc在解析request的参数时出现了什么问题,就打印了request的body的值查看,惊喜的发现有页面POST过来的内容,便取消了自动注入参数的方式,用了反射机制来初始化参数值。

 1 @SuppressWarnings("rawtypes")
 2     public void init(HttpServletRequest request){
 3         try {
 4             String queryBody = IOUtils.toString(request.getInputStream());
 5             
 6             Class cls = Class.forName("Params");
 7             
 8             if(StringUtils.isNotBlank(queryBody)){
 9                 StringTokenizer st = new StringTokenizer(queryBody, "&");
10                 
11                 while (st.hasMoreTokens()) {
12                     String pairs = st.nextToken();
13                     String key = pairs.substring(0, pairs.indexOf('='));
14                     String value = pairs.substring(pairs.indexOf('=') + 1);
15                     
16                     if(StringUtils.isBlank(value))
17                         continue;
18                     
19                     value = URLDecoder.decode(value, "UTF-8");
20                     
21                     Field fld = cls.getDeclaredField(key);
22                     Class type = fld.getType();
23                     if(type.toString().equalsIgnoreCase("int")){
24                         fld.setInt(this, Integer.parseInt(value));
25                     }else{
26                         fld.set(this, value);
27                     }
28                 }
29             }
30             
31         } catch(UnsupportedEncodingException e){
32             
33         } catch (IOException e) {
34             
35         } catch (SecurityException e) {
36             
37         } catch (NoSuchFieldException e) {
38             
39         } catch (ClassNotFoundException e) {
40             
41         } catch (IllegalArgumentException e) {
42             
43         } catch (IllegalAccessException e) {
44             
45         }
46     }
View Code

 

posted on 2014-06-20 16:23  小迷糊@  阅读(1088)  评论(0编辑  收藏  举报