• 请求--拦截器--action经过


    引用
    我这里想知道的是同名的多个参数,会被自动的放置在List或者数组中,我想知道是怎么实现的,因为取一个参数和取多个同名的参数是不同的方法:
    一个是request.getParameter
    一个是request.getParameterValues


    先解释一下:

    Struts或则XWorlk提供的Interceptor,操作的是已经被封装好的Parameters的Map了。相信你应该看到过这句话。(在CheckboxInterceptor中有)

    Java代码 收藏代码
    Map parameters = ai.getInvocationContext().getParameters();

    关于你的问题:

    如果你说的是,同名的多个参数如何以List或者数组的方式,放在Parameter的Map中。

    这个应该是属于Servlet规范中的内容。在HTTP协议的Request传递到ServletContainer中后,Container就会为该次请求生成一个HTTPServletRequest对象,在HTTPServletRequest对象中,参数和值,放入到了Map中。

    在Tomcat的实现代码中,可以看到,同名的参数的值,被加入的String数组中。单个的参数,也是放在数组中的。
    Java代码 收藏代码

    /**
    * Put name and value pair in map. When name already exist, add value
    * to array of values.
    *
    * @param map The map to populate
    * @param name The parameter name
    * @param value The parameter value
    */
    private static void putMapEntry( Map map, String name, String value) {
    String[] newValues = null;
    String[] oldValues = (String[]) map.get(name);
    if (oldValues == null) {
    newValues = new String[1];
    newValues[0] = value;
    } else {
    newValues = new String[oldValues.length + 1];
    System.arraycopy(oldValues, 0, newValues, 0, oldValues.length);
    newValues[oldValues.length] = value;
    }
    map.put(name, newValues);
    }


    引用
    因为取一个参数和取多个同名的参数是不同的方法:
    一个是request.getParameter
    一个是request.getParameterValues

    都可以使用request.getParameterValues的方法,通过循环来得到值、或者赋值。


    如果你说的是,同名的多个参数如何以List或者数组的方式,放在Action中。

    那就需要看Struts的OGNL实现的源码,Interceptor只是将值转移到ValueStatck上,而后由ONGL进行赋值的。


    总结:你在Interceptor中是看不到你期望的判断语句的。因为,封装的判断语句在Servlet的容器中实现;解封装的语句,在struts2的OGNL中实现。Interceptor只是对其中值,稍微做些处理

  • 相关阅读:
    每日总结32
    每日总结31
    每日总结30
    Leetcode 115. 不同的子序列(二维DP)
    Leetcode 59. 螺旋矩阵 II
    Leetcode 227. 基本计算器 II
    macOS下将GitHub中单个子文件夹下载到本地
    P3796 【模板】AC自动机(加强版)
    P3808 【模板】AC自动机(简单版)
    【Kubernetes】副本的删除
  • 原文地址:https://www.cnblogs.com/Struts-pring/p/3937787.html
Copyright © 2020-2023  润新知