zoukankan      html  css  js  c++  java
  • SpringMvc_@RequestMapping设置Router Url大小写不敏感

    http://stackoverflow.com/questions/4150039/how-can-i-have-case-insensitive-urls-in-spring-mvc-with-annotated-mappings

     
    以下四个Url可以指向同一个Controller方法

    http://localhost:8080/login/userLOgin
    http://localhost:8080/LogiN/userLOgin
    http://localhost:8080/login/userlogin
    http://localhost:8080/Login/UserLogin

    一、解决方案One(下面的applicationContext.xml配置有些可以简化
    1
    2
    3
    4
    5
    6
    7
    public class CaseInsenseticePathMatcher extends AntPathMatcher {
        @Override
        protected boolean doMatch(String pattern, String path, boolean fullMatch, Map<String, String> uriTemplateVariables) {
            System.err.println(pattern + " -- " + path);
            return super.doMatch(pattern.toLowerCase(), path.toLowerCase(), fullMatch, uriTemplateVariables);
        }
    }

    applicationContext.xml:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    <bean id="matcher" class="test.CaseInsenseticePathMatcher"/>
     
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
        <property name="pathMatcher" ref="matcher"/>
    </bean>
     
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="pathMatcher" ref="matcher"/>
        <property name="webBindingInitializer">
            <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer"/>
        </property>
        <property name="messageConverters">
            <list>
                <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
                <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
                <bean class="org.springframework.http.converter.FormHttpMessageConverter"/>
                <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/>
            </list>
        </property>
    </bean>
     
    <bean id="conversion-service" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"/>


    简化后的applicationContext.xml: 必须配置HandlerMapping、HandlerAdapter(即Controller和Action)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <bean id="matcher" class="com.le.lej.common.CaseInsenseticePathMatcher" />
     
        <bean
            class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
            <property name="pathMatcher" ref="matcher" />
        </bean>
     
        <bean
            class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
            <property name="pathMatcher" ref="matcher" />
        </bean>


     

     

        

    二、解决方案Two(需要Spring4.2以上支持)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    @Configuration
    public class WebConfig extends WebMvcConfigurerAdapter {
        @Override
        public void configurePathMatch(PathMatchConfigurer configurer) {
            AntPathMatcher matcher = new AntPathMatcher();
            matcher.setCaseSensitive(false);
            configurer.setPathMatcher(matcher);
        }
    }


     


  • 相关阅读:
    Burp Suite抓包使用步骤
    抓包工具Burp Suite安装步骤(待补充)
    mysql版本和模式查询
    git仓库下拉和上传
    git仓库个人和企业版新增仓库和成员
    漏洞扫描工具acunetix破解安装步骤
    漏洞扫描工具acunetix12会遇到的问题
    memcached缓存:安装和清缓存
    oracle数据库备份、还原命令及常见问题(待补充)
    Linux中,关闭selinux
  • 原文地址:https://www.cnblogs.com/gossip/p/5441358.html
Copyright © 2011-2022 走看看