• Spring Mvc Url和参数名称忽略大小写


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

    URL忽略大小写:

    @Configuration
    public class SpringWebConfig extends WebMvcConfigurationSupport {
    
        @Override
        public void configurePathMatch(PathMatchConfigurer configurer) {
            AntPathMatcher pathMatcher = new AntPathMatcher();
            pathMatcher.setCaseSensitive(false);
            configurer.setPathMatcher(pathMatcher);
        }
    
    }

    或者

    @Configuration
    @ComponentScan(value = "com.sino.webapi.web", useDefaultFilters = false, includeFilters = {
            @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = { Controller.class }) })
    public class SpringWebConfig extends WebMvcConfigurationSupport{
        @Bean
        public RequestMappingHandlerMapping requestMappingHandlerMapping() {
            RequestMappingHandlerMapping m = new RequestMappingHandlerMapping();
            AntPathMatcher pathMatcher = new AntPathMatcher();
            pathMatcher.setCaseSensitive(false);
            m.setPathMatcher(pathMatcher);
            return m;
        }
    }

    参数名忽略大小写:

    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>

    添加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>
  • 相关阅读:
    循环语句的基本使用
    创建一个可拖动的dom元素。
    JavaScript中的callee,caller,call,apply的使用
    两个数组去重的方法。
    利用setTimeout建立能捕捉鼠标多次点击和鼠标长按的事件处理程序。
    document.getElementByClassName()的使用和兼容老浏览器。
    jQuery .data()方法的运用。
    javascript对象的深拷贝。
    未来、
    linux上机作业
  • 原文地址:https://www.cnblogs.com/icebutterfly/p/9817682.html
Copyright © 2020-2023  润新知