博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Mvc Url和参数名称忽略大小写
阅读量:4670 次
发布时间:2019-06-09

本文共 3175 字,大约阅读时间需要 10 分钟。

在开发过程中Spring Mvc 默认 Url和参数名称都是区分大小写的

  比如:www.a.com/user/getUserInfo?userId=1 

     www.a.com/user/getuserInfo?userId=1 

       www.a.com/user/getUserInfo?userid=1 

            www.a.com/user/getuserinfo?userid=1 

  这些都认为不同的地址和参数,在实际中用户根本不区分这些,所以我们要忽略大小写

URL忽略大小写

import org.springframework.context.annotation.Configuration;import org.springframework.util.AntPathMatcher;import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;/** * Created by tianwei on 2017/6/22. */@Configurationpublic class SpringWebConfig extends WebMvcConfigurationSupport {    @Override    public void configurePathMatch(PathMatchConfigurer configurer) {        AntPathMatcher pathMatcher = new AntPathMatcher();        pathMatcher.setCaseSensitive(false);        configurer.setPathMatcher(pathMatcher);    }}

参数名称忽略大小写

import java.io.IOException;import java.util.Collections;import java.util.Enumeration;import java.util.Map;import javax.servlet.FilterChain;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletRequestWrapper;import javax.servlet.http.HttpServletResponse;import org.springframework.util.LinkedCaseInsensitiveMap;import org.springframework.web.filter.OncePerRequestFilter;public class CaseInsensitiveRequestParameterNameFilter extends OncePerRequestFilter {    @Override    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)            throws ServletException, IOException {        filterChain.doFilter(new CaseInsensitiveParameterNameHttpServletRequest(request), response);    }    public static class CaseInsensitiveParameterNameHttpServletRequest extends HttpServletRequestWrapper {        private final LinkedCaseInsensitiveMap
map = new LinkedCaseInsensitiveMap<>(); @SuppressWarnings("unchecked") public CaseInsensitiveParameterNameHttpServletRequest(HttpServletRequest request) { super(request); map.putAll(request.getParameterMap()); } @Override public String getParameter(String name) { String[] array = this.map.get(name); if (array != null && array.length > 0) return array[0]; return null; } @Override public Map
getParameterMap() { return Collections.unmodifiableMap(this.map); } @Override public Enumeration
getParameterNames() { return Collections.enumeration(this.map.keySet()); } @Override public String[] getParameterValues(String name) { return this.map.get(name); } }}

定义Bean

web.xml 增加Filter

caseInsensitiveRequestFilterProxy
org.springframework.web.filter.DelegatingFilterProxy
caseInsensitiveRequestFilterProxy
/*

到此再次运行项目就可以了,最上面的URL访问的是同一页面了

转载于:https://www.cnblogs.com/hantianwei/p/7065864.html

你可能感兴趣的文章
下载文件根据浏览器判断文件名,解决兼容性问题
查看>>
当网站不允许上传ASP,CGI,CER等脚本文件时
查看>>
Access 中数据库操作时提示from子句语法错误
查看>>
【备战NOIP】[算法总结] 二分查找
查看>>
sort函数用于vector向量的排序
查看>>
《算法》-- 总结
查看>>
El表达式
查看>>
leetcode-题8-3sum
查看>>
SQL-Server使用点滴(二-系统表)
查看>>
Djiango django跨域 cookie session
查看>>
vue通过webpack打包后怎么运行
查看>>
MYSQL中的日期转换
查看>>
在线修改Schema
查看>>
【学术篇】SDOI2008 仪仗队
查看>>
5.递归实现,把M元用最少的硬币来凑。不同面值的硬币,有10元,5元,2元,1元。...
查看>>
第6章—渲染web视图—使用Thymeleaf
查看>>
Android动态添加Fragment
查看>>
OGRE粒子系统简介
查看>>
C、C++语言中参数的压栈顺序
查看>>
用jquery实现简单的表单验证
查看>>