How to define 404
404, plainly is not find the page, then how to define "can't find"?
We can look at the source code to see how spring MVC defines "404":
FALSE); if (mappednull) { nohandlerfound (processedrequest, response); return;}
GetHandler is the process of matching to the controller by Handlermapping, based on the requested URL.
If the match is not reached, then the Nohandlerfound method is executed. This method is simple and returns a 404 error code.
Our web container, such as Tomcat, generates an error interface to the user based on this error code.
So, how do we customize this interface?
Overriding the Nohandlerfound method
The first thing to think of is to rewrite the Nohandlerfound method, which is protected and can be rewritten.
We need to redirect the page to our custom 404 interface, so we just need to
@Override void Nohandlerfound (HttpServletRequest request, throws Exception {"/ NotFound "); }
Here in our controller, we need to define a @requestmapping ("/notfound") method that returns a 404 page
Alternatively, there should be a way to directly access the static file.
Alternatively, you can also throw an exception by throwing an nosuchrequesthandlingmethodexception
This allows us to implement a custom 404 page. So, is there any other way?
Take advantage of Spring MVC's most precise match
Spring MVC matches URLs with a method called "the most exact match", for example
For example, we also define "/test/a", "/test/*", then if the requested URL end is/test/a, then it will match the exact one, that is, "/test/a"
Can we use this feature to find pages that can't be found?
1, first we define a block all the URL of the rule @requestmapping ("*"), then there is actually no page found, that is, never into the Nohandlerfound method body
2, the following steps and usual, for other requests are configured on @requestmapping
Then request to come in, either into our exact matching method (that is, the search), or into the @requestmapping ("*) interception of the body (that is not found)
Then we just have to let @requestmapping ("*) intercept this method to return a custom 404 interface is OK ~
Using the error-page provided by the Web container
Remember that the Web container mentioned earlier provides a default interface of 404?
In fact, we can completely replace our own interface, then it seems that this method should be the simplest.
Just write the following code in the Web. xml file:
<error-page> <error-code>404</error-code>< location>/resource/view/404.htm</location></error-page>
It is worth noting, however, that the location configured here is actually accessed as a request.
Then our dispatcherservlet will intercept this request and cause it to be inaccessible, and the result is a blank user interface.
So the 404.htm here is actually a static resource, we need to access static resources in a way to access.
And in my spring MVC, the files in the resource directory are not intercepted.
Compare the differences between three different ways
1, the most convenient: that must be the third kind, we only need to provide a static page to
2, the quickest: the first one must be the slowest, because it will initiate 2 requests. The second and third should be about the same.
3, the most flexible: from the perspective of flexibility, the third is definitely the most lacking, but in fact, for 404来 said not need to change often, but also can not be guaranteed to allow users to customize the 404 interface, and here one, 22 ways to provide flexibility.
4, versatility: The third should be the most common, and one or two of the two are dependent on spring MVC
Methods for handling 404 when using SPRINGMVC