spring mvc default mapping handler
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.
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.
Thanks for any suggestions!
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.
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.
Why not just catch the 404s and handle accordingly? If I'm not mistaken, I think that's what you are trying to accomplish.
You could place this in your web.xml to handle the 404:
<error-page>
<error-code>404</error-code>
<location>/yourHandlerPage</location>
</error-page>
Am I missing something here?
精彩评论