动力节点旗下在线教育品牌  |  咨询热线:400-8080-105 学Java全栈,上蛙课网
首页 > 文章

Spring MVC修改请求参数

09-02 17:29 77浏览
举报 T字号
  • 大字
  • 中字
  • 小字

在使用Spring MVC进行开发的过程中会涉及到很多的请求参数问题,如果潜心研究Spring MVC请求参数,你会发现不能修改request的参数内容,假如想要在request的param Map里面默认注入修改内容,实现Spring MVC修改请求参数你可以进行以下操作:

1.继承javax.servlet.http.HttpServletRequestWrapper

2.重写方法

    2.1 getParameter(String name); // 取值方法

    2.2 getParameterValues(String name); // 取值方法

    2.3 getParameterNames(); // 获取请求参数方法(需要时修改)

import java.io.BufferedReader;

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.Collections;

import java.util.Enumeration;

import java.util.HashMap;

import java.util.Map;

 

import javax.servlet.ServletInputStream;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletRequestWrapper;

 

import org.apache.commons.io.IOUtils;

 

 

public class BodyReaderHttpServletRequestWrapper extends HttpServletRequestWrapper {

 

    private ByteArrayOutputStream cacheBytes;

 

    // 用于存储请求参数

    private Map params = new HashMap();

 

    public BodyReaderHttpServletRequestWrapper(HttpServletRequest request) throws IOException {

        super(request);

        cacheBytes = new ByteArrayOutputStream();

        IOUtils.copy(super.getInputStream(), cacheBytes);

    }

    @Override

    public ServletInputStream getInputStream() throws IOException {

        return new CacheServletInputStream();

    }

 

    class CacheServletInputStream extends ServletInputStream{

 

        private ByteArrayInputStream bais;

 

        public CacheServletInputStream(){

            bais = new ByteArrayInputStream(cacheBytes.toByteArray());

        }

 

        @Override

        public int read() throws IOException {

            return bais.read();

        }

        @Override

        public void close() throws IOException {

            super.close();

            bais.close();

            cacheBytes.close();

        }

 

    }

    @Override

    public BufferedReader getReader() throws IOException {

        return new BufferedReader(new InputStreamReader(getInputStream(),"UTF-8"));

    }

 

    /**

     * 添加参数到map中

     * @param name

     * @param value

     */

    public void setParameter(String name, Object value) {

        if (value != null) {

            System.out.println(value);

            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)});

            }

        }

    }

 

    /**

     * 重写getParameter,代表参数从当前类中的map获取

     * @param name

     * @return

     */

    @Override

    public String getParameter(String name) {

        String[]values = params.get(name);

        if(values == null || values.length == 0) {

            return null;

        }

        return values[0];

    }

 

    /**

     * 重写getParameterValues方法,从当前类的 map中取值

     * @param name

     * @return

     */

    @Override

    public String[] getParameterValues(String name) {

        return params.get(name);

    }

 

    /**

     * 添加参数到map中

     * @param extraParams

     */

    public void extraParams(Map extraParams) {

        if (extraParams == null) return;

        for (Map.Entry entry : extraParams.entrySet()) {

            setParameter(entry.getKey(), entry.getValue());

        }

    }

 

    /**

     * 获取请求参数

     * @return

     */

    public Enumeration getParameterNames() {

        return Collections.enumeration(this.params.keySet());

    }

}

3.写过滤器

import net.xiaoyuanxia.framework.core.wrapper.BodyReaderHttpServletRequestWrapper;

import net.xiaoyuanxia.framework.core.wrapper.BodyReaderHttpServletResponseWrapper;

 

import javax.servlet.*;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

import java.util.HashMap;

import java.util.Map;

 

public class AppVeveryFilter implements Filter {

 

 

 

@Override

public void destroy() {

}

 

@Override

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

 

HttpServletRequest httpRequest = (HttpServletRequest) request;

 

HttpServletResponse httpResponse = (HttpServletResponse) response;

BodyReaderHttpServletResponseWrapper mResp = new BodyReaderHttpServletResponseWrapper(httpResponse);

 

Map map = new HashMap();

map.put("a", "a");

 

BodyReaderHttpServletRequestWrapper bodyReaderHttpServletRequestWrapper = new BodyReaderHttpServletRequestWrapper(httpRequest);

bodyReaderHttpServletRequestWrapper.extraParams(map);

 

chain.doFilter(bodyReaderHttpServletRequestWrapper, mResp);

 

}

 

@Override

public void init(FilterConfig config){

 

}

}

4.配置web.xml

<filter>

<filter-name>appVeveryFilter</filter-name>

<filter-class>xx.xx.xx.core.filter.AppVeveryFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>appVeveryFilter</filter-name>

<url-pattern>/app/*</url-pattern>

</filter-mapping>

5.测试

@Controller

@RequestMapping("/app")

public class TestController extends BaseAction {

 

    @RequestMapping("/test")

   public void test(String a) {

       System.out.println("a:" + a);

   }

}

   上面就是整个Spring MVC修改请求参数过程,不是很复杂,但涉及到的代码还是有一定数量的。只要我们能够牢记这些步骤,步步为营,处理好Spring MVC修改请求参数问题指日可待!当然,你还可以在本站的Spring MVC视频教程中学习更多关于Spring MVC的知识,不断充实自己,提升自己的能力。

0人推荐
共同学习,写下你的评论
0条评论
携代码的男孩纸
程序员携代码的男孩纸

24篇文章贡献108038字

作者相关文章更多>

推荐相关文章更多>

DOM渲染的详细过程

QCode09-04 14:38

CSS水平和垂直居中技巧大梳理

Code大师09-04 14:50

mui的input框在IOS系统下无法聚焦或点击多次才能聚焦

不写代码你养我啊08-23 11:14

推荐的-视.频播放器以及在线客服

不写代码你养我啊09-17 18:02

谈谈java多线程的三大特性

要学习了06-18 18:13

发评论

举报

0/150

取消