簡體   English   中英

Spring MVC不區分大小寫的URL

[英]Spring MVC case insensitive URLs

我已經在Google和stackoverflow上尋找了這個答案,但不幸的是,所提供的解決方案要么假設有很多關於Spring MVC和Java的先前知識,要么是關於注釋的不區分大小寫。

因此,我不確定如何使這些解決方案適應我自己的問題,因此這個新問題的原因。

我想做的事聽起來很簡單。 我有一個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>

我希望/cms/*/admin/*鍵不區分大小寫,但對Java和Spring MVC都不熟悉,我不明白我應該怎么做。

例如,即使有人鍵入/CMS//Cms/我希望它使用pageController而此刻它只會顯示404頁面。

是否有人能夠向我解釋為了達到我想要的結果我必須做些什么?

任何幫助將不勝感激!

編輯:

根據Rupok的回答,我添加了一個類來擴展AntPathMatcher

不幸的是對此我不熟悉我不知道如何“將它設置回SimpleUrlHandlerMapping”。

有人能夠指出我正確的方向嗎?

SimpleUrlHandlerMapping的默認匹配機制是AntPathMatcher。 您可以創建自己的PathMatcher實現,也可以創建AntPathMatcher的子類,並將其設置回SimpleUrlHandlerMapping。

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的回答讓我開始朝着正確的方向前進,但我需要稍微改變實現以使其工作。

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)和sever其他方法委托給doMatch(String,String,boolean,Map)。

此外,由於我使用的是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