zoukankan      html  css  js  c++  java
  • spring mvc 3.1的自动注入参数遇到的问题

    在网上下载了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
  • 相关阅读:
    JavaWeb核心编程之(四.1)JSP
    一起来说 Vim 语
    你应该知道的基础 Git 命令
    Git 系列(五):三个 Git 图形化工具
    Git 系列(四):在 Git 中进行版本回退
    Git 系列(三):建立你的第一个 Git 仓库
    Git 系列(二):初步了解 Git
    Git 系列(一):什么是 Git
    JavaWeb核心编程之(三.6)HttpServlet
    多线程:子线程执行完成后通知主线程
  • 原文地址:https://www.cnblogs.com/mihu/p/3799593.html
Copyright © 2011-2022 走看看