spring mvc默认映射处理程序 [英] spring mvc default mapping handler

查看:128
本文介绍了spring mvc默认映射处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,使用Spring MVC,我正在尝试创建一个路由器控制器,该控制器将接收尚未由另一个控制器处理的任何URL并将其路由到其各自的资源,或者如果没有资源可以转发搜索请求找到。
使用 @RequestMapping(value =/ {qry},method = RequestMethod.GET)成功获取了我的其他人未获取的请求控制器(这似乎通过首先检查最具体的映射来工作)然后我可以做任何我需要的转发。但是,只要我在请求中放入/,映射就会中断并返回404。

Basically, using Spring MVC, I'm trying to create a router controller that will take any URL that hasn't already been handled by another controller and route it to its respective resource or forward a search request if no resource could be found. Using @RequestMapping(value="/{qry}", method = RequestMethod.GET) was successful in grabbing requests that weren't grabbed already by my other controllers (this seems to work by checking the most specific mappings first) and then I could do whatever forwarding I needed. However, as soon as I put a "/" in the request, the mapping breaks and returns a 404.

所以换句话说, / some-long-path-or-something正确映射到此catch-all控制器,但/ some / other / path(没有映射到任何其他控制器的东西没有被我的全部捕获。

So in other words, "/some-long-path-or-something" maps correctly to this catch-all controller, but "/some/other/path" (which does not map to any other controller) is not caught by my catch-all.

如何实现?我已经阅读了一些关于拦截器和默认处理程序的内容,但没有找到解决方案。

How can this be implemented? I've read a few things about interceptors and default handlers, but with no luck in finding a solution.

感谢您的任何建议!

推荐答案

开箱即用,Spring自动注册一个 DefaultAnnotationHandlerMapping bean,它匹配对控制器的请求使用注释。在大多数情况下,它的默认行为都很好。

Out-of-the-box, Spring automatically registers a DefaultAnnotationHandlerMapping bean, which matches requests to controllers using the annotations. Its default behaviour is fine in most situations.

如果在上下文中声明自己的 DefaultAnnotationHandlerMapping ,那么它将会覆盖默认值,并允许您设置其 defaultHandler 属性,该属性将在没有任何显式处理程序匹配时使用。只需将catch-all控制器注入该属性,然后将Bob's Your Uncle注入其中。

If you declare your own DefaultAnnotationHandlerMapping in the context, then it will override the default one, and will allow you to set its defaultHandler property, which will be used when none of the explicit handlers match. Just inject the "catch-all" controller into that property, and Bob's Your Uncle.

这篇关于spring mvc默认映射处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆