• 如何修改request请求中的参数


    一般情况下,在servlet中出于安全考虑是不允许修改request请求中的内容,但是在很多场景中,却有这样的需求,如在springmvc中从一个controller转发到另一个功能相同的controller,但是参数有差异,需要修改或补充参数。这就需要修改request请求中的参数,HttpServletRequest只提供了getParameter()方法,无法set,为了实现这种操作,servlet提供了HttpServletRequestWrapper类,HttpServletRequestWrapper是装饰器模式的经典实现,通过继承HttpServletRequestWrapper类型,可以重写Parameter系列方法,这样就实现了自定义的request请求。代码如下:

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletRequestWrapper;
    import java.util.Enumeration;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Vector;
    public class ParameterRequestWrapper extends HttpServletRequestWrapper {
    
        private Map<String, String[]> params = new HashMap<>();
    
        public ParameterRequestWrapper(HttpServletRequest request) {
            super(request);
            this.params.putAll(request.getParameterMap());
        }
    
        public ParameterRequestWrapper(HttpServletRequest request, Map<String, Object> extendParams) {
            this(request);
            //这里将扩展参数写入参数表
            addAllParameters(extendParams);
        }
    
        @Override
        public Enumeration<String> getParameterNames() {
            return new Vector(params.keySet()).elements();
        }
    
        @Override
        public String getParameter(String name) {
            String[] values = params.get(name);
            if (values == null || values.length == 0) {
                return null;
            }
            return values[0];
        }
    
        @Override
        public String[] getParameterValues(String name) {
            String[] values = params.get(name);
            if (values == null || values.length == 0) {
                return null;
            }
            return values;
        }
    
        private void addAllParameters(Map<String, Object> otherParams) {
            for (Map.Entry<String, Object> entry : otherParams.entrySet()) {
                addParameter(entry.getKey(), entry.getValue());
            }
        }
    
        private void addParameter(String name, Object value) {
            if (value != null) {
                if (value instanceof String[]) {
                    params.put(name, (String[]) value);
                } else if (value instanceof String) {
                    params.put(name, new String[]{(String) value});
                } else {
                    params.put(name, new String[]{String.valueOf(value)});
                }
            }
        }
    }

    有了上面自定义request,就可以在如下的场景使用改变request的参数。

    • 1、在过滤器中使用
    Map<String, Object> param = new HashMap<>(); //自定义参数
    param.put("watermark", "1231414");
    param.put("watermarkFontSize", "123123"); 
    ParameterRequestWrapper pr = new ParameterRequestWrapper(httpServletRequest, param);
    filterChain.doFilter(pr, response);
    • 2、在springMVC中转发
    Map<String, Object> param = new HashMap<>(); //自定义参数
    param.put("watermark", "1231414");
    param.put("watermarkFontSize", "123123"); 
    ParameterRequestWrapper pr = new ParameterRequestWrapper(request, param); 
    request.getRequestDispatcher("/general/openApi").forward(pr, response); // 转发
  • 相关阅读:
    每日练习
    AttributeError: module 'torch.jit' has no attribute 'get_trace_graph
    2020.3-SEAN: Image Synthesis with Semantic Region-Adaptive Normalization
    2019-11-DHAN~Towards Ghost-free Shadow Removal via Dual Hierarchical Aggregation Network and Shadow Matting GAN
    2020.2-DMFN-Image Fine-grained Inpainting
    Face completion with Hybrid Dilated Convolution
    FCSR-GAN: Joint Face Completion and Super-resolution via Multi-task Learning
    Joint Face Completion and Super-resolution using Multi-scale Feature Relation Learning
    浮点数转化为字符串
    Sublime Text2使用技巧
  • 原文地址:https://www.cnblogs.com/zhexuejun/p/15464613.html
Copyright © 2020-2023  润新知