简体   繁体   中英

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?

The default matching mechanism for the SimpleUrlHandlerMapping is an AntPathMatcher. You could either create your own PathMatcher implementation or subclass the AntPathMatcher and set this back on the SimpleUrlHandlerMapping.

The PathMatcher interface is fairly straight forward to implement.

public class CaseInsensitiveAntPathMatcher extends AntPathMatcher {

@Override
public boolean match(String pattern, String string) {
    return super.match(pattern.toLowerCase(), string.toLowerCase()); // make this according to your need
}

}

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM