Spring MVC 不区分大小写的 URL
我在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SimpleUrlHandlerMapping 的默认匹配机制是 AntPathMatcher。您可以创建自己的 PathMatcher 实现或将 AntPathMatcher 子类化并将其设置回 SimpleUrlHandlerMapping。
PathMatcher 接口的实现相当简单。
在配置中它看起来像这样:
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.
In the configuration it looks like this:
Rupok 的回答让我朝着正确的方向开始,但我需要对实现进行一些更改才能使其正常工作。
match(String,String)
并将其他方法委托给doMatch(String,String,boolean,Map)
。另外,由于我使用的是
org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
我需要像这样插入我的新匹配器Rupok's answer got me started in the right direction but I needed to change the implementation a little to get it to work.
match(String,String)
and sever other method delegate todoMatch(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