zoukankan      html  css  js  c++  java
  • Spring Mvc Url和参数名称忽略大小写

    在开发过程中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.
     */
    @Configuration
    public 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<String[]> 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<String, String[]> getParameterMap() {
                return Collections.unmodifiableMap(this.map);
            }
    
            @Override
            public Enumeration<String> getParameterNames() {
                return Collections.enumeration(this.map.keySet());
            }
    
            @Override
            public String[] getParameterValues(String name) {
                return this.map.get(name);
            }
    
        }
    
    }

    定义Bean

        <!--输入参数忽略大小写-->
        <bean id="caseInsensitiveRequestFilterProxy" class="org.springframework.web.filter.DelegatingFilterProxy">
            <property name="targetBeanName" value="caseInsensitiveRequestFilter"/>
        </bean>
        <bean id="caseInsensitiveRequestFilter"
              class="com.hantianwei.util.CaseInsensitiveRequestParameterNameFilter">
        </bean>

    web.xml 增加Filter

      <filter>
        <filter-name>caseInsensitiveRequestFilterProxy</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
      </filter>
      <filter-mapping>
        <filter-name>caseInsensitiveRequestFilterProxy</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>

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

  • 相关阅读:
    网络编程之即时通信程序(聊天室)(一)通信流程简介及通信协议定制
    C#常用加密方法解析
    ASP.NET常用数据绑定控件优劣总结
    使用XPO开发时可以参考使用的架构
    渠道会上的体会
    如何利用第三方SDK开发MSN机器人以及实现语音视频?
    对 XPO 的一些问题的解答
    c++ 参数传递 之引用形参 GIS
    指针与引用的区别 GIS
    c++ const 修饰数组 GIS
  • 原文地址:https://www.cnblogs.com/hantianwei/p/7065864.html
Copyright © 2011-2022 走看看