如何在 Spring MVC 控制器的 @RequestMapping 路径中限制路由扩展? [英] How do I restrict route extensions in @RequestMapping paths for Spring MVC controllers?
问题描述
我有一个相当简单的任务想要完成,但似乎无法找到有关 Spring MVC 路由的信息.我有一个非常简单的控制器,可以将路径路由到视图:
I have a fairly simple task that I want to accomplish, but can't seem to find information for Spring MVC routing about it. I have a very simple controller that routes a path to a view:
@Controller
@RequestMapping(value = "/help")
public class HelpController {
private static final String HELP = "help";
@RequestMapping(method = RequestMethod.GET)
public String help(Model model, Locale locale) {
model.addAttribute("locale", locale);
return HELP;
}
}
如果 http://mysite.com/help.some.extension.is.entered,我想抛出 404,但 Spring 似乎将示例解析为/help.javadoc 说 @RequestMapping 注释只是一个 servlet URI 映射,但我认为/help 意味着它需要完全匹配.任何澄清将不胜感激.
I would like to throw a 404 if http://mysite.com/help.some.extension.is.entered, but Spring seems to resolve the example to /help. The javadoc says that the @RequestMapping annotation is just a servlet URI mapping, but I thought /help means it needs to be an exact match. Any clarification would be appreciated.
推荐答案
对于 Spring 4,它很容易解决:
For Spring 4 it's pretty easy to solve:
<mvc:annotation-driven>
<mvc:path-matching suffix-pattern="false" />
</mvc:annotation-driven>
所以你仍然可以使用 mvc:annotation-driven
作为你的配置.
So you still can use mvc:annotation-driven
for your config.
这篇关于如何在 Spring MVC 控制器的 @RequestMapping 路径中限制路由扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!