Three ideas: custom 404 page, custom 404 page

Source: Internet
Author: User

Three ideas: custom 404 page, custom 404 page
Define 404

404. To put it bluntly, the page cannot be found. How can we define "not found?

We can use the source code to see how Spring MVC defines "404:


// Determine handler for the current request.mappedHandler = getHandler(processedRequest, false);if (mappedHandler == null || mappedHandler.getHandler() == null) {noHandlerFound(processedRequest, response);return;}


GetHandler matches the Controller through handlerMapping based on the request url.

If no match is found, the noHandlerFound method is executed. This method is simple and returns a 404 error code.

Our Web container, such as tomcat, will generate an error interface based on this error code for the user.

So how can we customize this interface?


Override noHandlerFound Method

The first thing that comes to mind is to rewrite the noHandlerFound method, which is protected and can be rewritten.

We need to redirect the page to our custom 404 interface.


@Overrideprotected void noHandlerFound(HttpServletRequest request,HttpServletResponse response) throws Exception {response.sendRedirect(request.getContextPath() + "/notFound");}


Here, we need to define a @ requestMapping ("/notFound") method in our Controller to return a 404 page

Alternatively, you can directly access static files.

You can also throw an exception NoSuchRequestHandlingMethodException.

In this way, we have implemented a custom 404 page. So, are there other methods?


Use Spring MVC for exact matching

Spring MVC uses the most precise matching method for url matching. For example

For example, if we define "/test/a" and "/test/*" at the same time, if the request url ends with/test/a, it will match the exact one, that is, "/test/"

Can we use this feature to find pages that cannot be found?

1. First, we define a rule to intercept all URLs @ requestMapping ("*"), so there is actually no page that cannot be found, that is, it will never enter the noHandlerFound method body

2. Configure @ requestMapping for all other requests in the same steps as in normal time

Then, the request either enters the method we precisely matched (that is, the method we found), or the method body intercepted by @ requestMapping ("*) (that is, the method we cannot find)

So we only need to let @ requestMapping ("*) intercept this method and return a custom 404 interface ~


Use the error-page provided by the web Container

Do you still remember that the web Container previously mentioned will provide a default 404 interface?

In fact, we can replace it with our own interface, so it seems that this method should be the simplest.

You only need to write the following code in the web. xml file:


<error-page><error-code>404</error-code><location>/resource/view/404.htm</location></error-page>


However, the location configured here is actually accessed as a request.

Then our DispatcherServlet will intercept this request and cause access failure. At this time, the user interface is blank.

In this case, 404.htm is actually a static resource. We need to access it by accessing static resources.

In my Spring MVC, files in the resource Directory are not blocked.


Comparison of the three methods
1. Most convenient: it must be the third type. We only need to provide a static page. 2. Quickest: the first type must be the slowest, because it will initiate two requests. The second and third types should be similar. 3. The most flexible: From the Perspective of flexibility, the third type must be the most lacking, but it does not need to change frequently for 404, however, you may not be allowed to customize the 404 interface. The first and second methods provide flexibility. 4. versatility: The third type should be the most common, while the first and second types depend on Spring MVC.
Custom 404 page not displayed

Normally displayed in Firefox (404 )~~ 
 
How can I determine whether a website has a custom 404 page?

Use Last-Modified to determine whether apache has customized the 404 page

If Apache does not customize the 404 page, the requested page does not exist and there is no Last-Modified (because Last-Modified is mtime ).
If the 404 page is customized, the Last-Modified is the mtime of the 404 page.

Try the following:
Root @ ming-laptop :~ # Curl-I-X GET 172.16.1.54/ming.html
HTTP/1.1 404 Not Found
Date: Sun, 19 Jun 2011 15:06:42 GMT
Server: Apache/2.2.19 (Unix) DAV/2
Content-Length: 207
Content-Type: text/html; charset = iso-8859-1

Root @ ming-laptop :~ # Curl-I-X GET 172.16.1.54/ming.html
HTTP/1.1 404 Not Found
Date: Sun, 19 Jun 2011 15:08:32 GMT
Server: Apache/2.2.19 (Unix) DAV/2
Last-Modified: Sun, 19 Jun 2011 15:02:39 GMT
ETag: "88022-8-4a611e8e7c1c0"
Accept-Ranges: bytes
Content-Length: 8
Content-Type: text/html; charset = gbk
Root @ ming-laptop :~ #

# The returned results of the custom 404 page and the custom 404 page are not displayed.
# OK

# Note: The nginx test shows that the custom 404 page does not have the Last-Modified. (It Looks Like testing server behavior in the future, or using Apache as the boss)
Reference: blog.sina.com.cn/chengyu269

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.