简体   繁体   English

Spring MVC不区分大小写的URL

[英]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. 我已经在Google和stackoverflow上寻找了这个答案,但不幸的是,所提供的解决方案要么假设有很多关于Spring MVC和Java的先前知识,要么是关于注释的不区分大小写。

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: 我有一个dispatcher-servlet.xml文件,其中包含以下XML块:

<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. 我希望/cms/*/admin/*键不区分大小写,但对Java和Spring MVC都不熟悉,我不明白我应该怎么做。

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. 例如,即使有人键入/CMS//Cms/我希望它使用pageController而此刻它只会显示404页面。

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 . 根据Rupok的回答,我添加了一个类来扩展AntPathMatcher

Unfortunately being new to this I do not know how to "set this back on the SimpleUrlHandlerMapping". 不幸的是对此我不熟悉我不知道如何“将它设置回SimpleUrlHandlerMapping”。

Would anybody be able to point me in the right direction? 有人能够指出我正确的方向吗?

The default matching mechanism for the SimpleUrlHandlerMapping is an AntPathMatcher. SimpleUrlHandlerMapping的默认匹配机制是AntPathMatcher。 You could either create your own PathMatcher implementation or subclass the AntPathMatcher and set this back on the SimpleUrlHandlerMapping. 您可以创建自己的PathMatcher实现,也可以创建AntPathMatcher的子类,并将其设置回SimpleUrlHandlerMapping。

The PathMatcher interface is fairly straight forward to implement. PathMatcher接口非常直接实现。

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. Rupok的回答让我开始朝着正确的方向前进,但我需要稍微改变实现以使其工作。

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). match(String,String)和sever其他方法委托给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 此外,由于我使用的是org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping,我需要插入我的新匹配器

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="order" value="0" />
    <property name="pathMatcher">
        <bean class="youpackage.CaseInsensitiveAntPathMatcher" />
    </property>
</bean>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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