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


 


 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM