问题描述
我的Controller中有多个@RequestMapping注释。
I have Controller having multiple @RequestMapping annotations in it.
@Controller
public class SignUpController {
@RequestMapping("signup")
public String showSignUp() throws Exception {
return "somejsp";
}
@RequestMapping("fullSignup")
public String showFullSignUp() throws Exception {
return "anotherjsp";
}
@RequestMapping("signup/createAccount")
public String createAccount() throws Exception {
return "anyjsp";
}
}
如何将这些@RequestMapping映射到不区分大小写。即如果我使用/ fullsignup或/ fullSignup,我应该得到anotherjsp。但现在这种情况并没有发生。只有/ fullSignup工作正常。
How can I map these @RequestMapping to case insensitive. i.e. if I use "/fullsignup" or "/fullSignup" I should get "anotherjsp". But this is not happening right now. Only "/fullSignup" is working fine.
我尝试过扩展RequestMappingHandlerMapping但没有成功。我也尝试过AntPathMatcher,就像那个提到的那个人在这个论坛上有另一个问题,但它也不适用于@RequestMapping注释。
I've tried extending RequestMappingHandlerMapping but no success. I've also tried AntPathMatcher like the guy mentioned there is another question on this forum but its also not working for @RequestMapping annotation.
调试控制台
Debugging console
服务器启动时的输出控制台。
Output console when server is up.
我添加了两张显示问题的图片。我已经尝试了下面提到的两种解决方案。控制台说它映射了小写的URL,但是当我请求访问带有小写url的方法时,它表明存储值的原始映射包含MixCase URLS。
I've added two images which shows the problem. I've tried both the solutions mentioned below. The console says that it mapped lowercased URLS but when I request to access a method with lowercase url then it shows that the original map where the values are stored stilled contained MixCase URLS.
推荐答案
One of the approaches in How can I have case insensitive URLS in Spring MVC with annotated mappings works perfectly. I just tried it with combinations of @RequestMapping at the level of controller and request methods and it has worked cleanly, I am just reproducing it here for Spring 3.1.2:
CaseInsensitivePathMatcher:
The CaseInsensitivePathMatcher:
import java.util.Map;
import org.springframework.util.AntPathMatcher;
public class CaseInsensitivePathMatcher extends AntPathMatcher {
@Override
protected boolean doMatch(String pattern, String path, boolean fullMatch, Map<String, String> uriTemplateVariables) {
return super.doMatch(pattern.toLowerCase(), path.toLowerCase(), fullMatch, uriTemplateVariables);
}
}
使用Spring MVC注册此路径匹配器,删除< mvc:annotation-driven />
注释,并替换为以下内容,进行适当配置:
Registering this path matcher with Spring MVC, remove the <mvc:annotation-driven/>
annotation, and replace with the following, configure appropriately:
<bean name="handlerAdapter" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="webBindingInitializer">
<bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="conversionService" ref="conversionService"></property>
<property name="validator">
<bean class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="providerClass" value="org.hibernate.validator.HibernateValidator"></property>
</bean>
</property>
</bean>
</property>
<property name="messageConverters">
<list>
<ref bean="byteArrayConverter"/>
<ref bean="jaxbConverter"/>
<ref bean="jsonConverter"/>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"></bean>
<bean class="org.springframework.http.converter.ResourceHttpMessageConverter"></bean>
<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"></bean>
<bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter"></bean>
</list>
</property>
</bean>
<bean name="byteArrayConverter" class="org.springframework.http.converter.ByteArrayHttpMessageConverter"></bean>
<bean name="jaxbConverter" class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"></bean>
<bean name="jsonConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
<bean name="caseInsensitivePathMatcher" class="org.bk.lmt.web.spring.CaseInsensitivePathMatcher"/>
<bean name="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
<property name="pathMatcher" ref="caseInsensitivePathMatcher"></property>
</bean>
使用@Configuration更轻松,更干净:
Or even more easily and cleanly using @Configuration:
@Configuration
@ComponentScan(basePackages="org.bk.webtestuuid")
public class WebConfiguration extends WebMvcConfigurationSupport{
@Bean
public PathMatcher pathMatcher(){
return new CaseInsensitivePathMatcher();
}
@Bean
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
RequestMappingHandlerMapping handlerMapping = new RequestMappingHandlerMapping();
handlerMapping.setOrder(0);
handlerMapping.setInterceptors(getInterceptors());
handlerMapping.setPathMatcher(pathMatcher());
return handlerMapping;
}
}
这篇关于Spring MVC @RequestMapping注释的不区分大小写映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!