Spring MVC 不区分大小写的 URL

发布于 2024-12-04 06:54:36 字数 1229 浏览 3 评论 0原文

我在 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”。

有人能指出我正确的方向吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幸福丶如此 2024-12-11 06:54:36

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
  }
}

在配置中它看起来像这样:

<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>
<property name="pathMatcher">
    <bean class="packagename.CaseInsensitiveAntPathMatcher"/>
</property>
<property name="alwaysUseFullPath" value="true"/>

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
  }
}

In the configuration it looks like this:

<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>
<property name="pathMatcher">
    <bean class="packagename.CaseInsensitiveAntPathMatcher"/>
</property>
<property name="alwaysUseFullPath" value="true"/>
仅冇旳回忆 2024-12-11 06:54:36

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) 并将其他方法委托给 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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文