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


     


  • 相关阅读:
    ML(5)——神经网络1(神经元模型与激活函数)
    ML(4)——逻辑回归
    ML(附录3)——过拟合与欠拟合
    多变量微积分笔记24——空间线积分
    iOS开发--横向流水布局实现
    李洪强和你一起学习前端之面试题
    前端面试题 -- JS篇
    很强大的HTML+CSS+JS面试题(附带答案)
    李洪强iOS之集成极光推送三iOS集成指南
    李洪强iOS之集成极光推送二iOS 证书 设置指南
  • 原文地址:https://www.cnblogs.com/gossip/p/5441358.html
Copyright © 2011-2022 走看看