'Spring MVC case insensitive URLs
I have looked for the answer to this on Google and stackoverflow, but unfortunately the solutions provided either assume a lot of prior knowelege about Spring MVC and Java or are about case insensitivity with annotations.
Because of this I am not sure how to adapt these solutions to my own problem, hence the reason for this new question about it.
What I want to do sounds simple.
I have a dispatcher-servlet.xml file with the following block of XML in it:
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="*.htm">pageController</prop>
<prop key="*.html">pageController</prop>
<prop key="/cms/*">pageController</prop>
<prop key="/admin/*">adminController</prop>
</props>
</property>
</bean>
I want the /cms/* and /admin/* keys to be case insensitive, but being new to both Java and Spring MVC, I don't understand how I should go about doing this.
For example, even if someone types /CMS/ or /Cms/ I want it to use the pageController whereas at the moment it will just display a 404 page.
Would anyone be able to explain to me exactly what I would have to do to achieve my desired result?
Any help would be greatly appreciated!
Edit:
As per Rupok's answer I have added a class to extend AntPathMatcher.
Unfortunately being new to this I do not know how to "set this back on the SimpleUrlHandlerMapping".
Would anybody be able to point me in the right direction?
Solution 1:[1]
Rupok's answer got me started in the right direction but I needed to change the implementation a little to get it to work.
public class CaseInsensitiveAntPathMatcher 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);
}
}
match(String,String) and sever other method delegate to doMatch(String,String,boolean,Map).
Also, since I am using a org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping I needed to plug in my new matcher like this
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="order" value="0" />
<property name="pathMatcher">
<bean class="youpackage.CaseInsensitiveAntPathMatcher" />
</property>
</bean>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Lonzak |
