在网上下载了xheditor作为页面的编辑器,编辑内容后post到后台保存,后台方法用spring mvc的自动注入的方式接收参数。
这种方式在各个浏览器下运行良好,但是在ie11下发现,从word、文本编辑器或者其它编辑器复制内容到xheditor后,这时提交到后台的参数不能被接收到。
仔细排查下发现ie11下复制到xheditor的内容都被默默的加了一段无用的div:
<div style="top: 0px;">
</div>
此时用最原始的接收参数的方式:request.getParameter(“name”);也取不到值。但是抓包看浏览器的交互信息,编辑器里的内容确实被提交到了后台。
于是猜想这可能是spring mvc在解析request的参数时出现了什么问题,就打印了request的body的值查看,惊喜的发现有页面POST过来的内容,便取消了自动注入参数的方式,用了反射机制来初始化参数值。
@SuppressWarnings("rawtypes")
public void init(HttpServletRequest request){
try {
String queryBody = IOUtils.toString(request.getInputStream()); Class cls = Class.forName("Params"); if(StringUtils.isNotBlank(queryBody)){
StringTokenizer st = new StringTokenizer(queryBody, "&"); while (st.hasMoreTokens()) {
String pairs = st.nextToken();
String key = pairs.substring(0, pairs.indexOf('='));
String value = pairs.substring(pairs.indexOf('=') + 1); if(StringUtils.isBlank(value))
continue; value = URLDecoder.decode(value, "UTF-8"); Field fld = cls.getDeclaredField(key);
Class type = fld.getType();
if(type.toString().equalsIgnoreCase("int")){
fld.setInt(this, Integer.parseInt(value));
}else{
fld.set(this, value);
}
}
} } catch(UnsupportedEncodingException e){ } catch (IOException e) { } catch (SecurityException e) { } catch (NoSuchFieldException e) { } catch (ClassNotFoundException e) { } catch (IllegalArgumentException e) { } catch (IllegalAccessException e) { }
}