SSM framework--static resource access (replaces 404 pages)

Configure a custom 404 page to replace Tomcat's unfriendly 404 page

Common 404 pages:


404 means that the current resource cannot be found or the resource does not exist

The origin server did not find a current representation for the target resource 
or is not willing to disclose that one exists.

Alternative idea: For pages that often appear in error 404, we can set them as static resources to speed up web page access.

Step 1: We need to change the mvc-dispatcher under WEB-INF\web.xml to the global configuration.
<servlet-mapping>
      <servlet-name>mvc-dispatcher</servlet-name>
      <!-- matches all requests by default -->
      <!-- We configure this by default to allow our Spring framework to take over Servelt and implement Spring to control all site requests -->
      <url-pattern>/</url-pattern>
  </servlet-mapping>

The page with error 404 is one of the commonly used pages, so we create a static directory under the resource directory (webapp) of the project, which is specially used to store static resources, such as js, css, error pages, login, registration pages, etc. The pages are stored in the view

After creating the directory as follows



Step 2: Add the resources of the error page in web.xml
<error-page>
    <error-code>404</error-code>
    <location>/static/view/404.html</location>
  </error-page>

However, after the configuration is complete, the input error page of the startup project still cannot display the 404 page configured by yourself, and the relevant pictures are lost.


Step 3: Write spring-web.xml in the spring directory to control which resources are intercepted.

spring-web.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <!-Deployment Spring MVC->
    <!-- 1. Enable SpringMVC annotation mode-->
    <!-- Simplified configuration:
        (1) Automatically register DefaultAnootationHandlerMapping, AnotationMethodHandlerAdapter
        (2) Provide some columns: data binding, number and date format @NumberFormat, @DateTimeFormat, xml, json default read and write support
    -->
    <mvc:annotation-driven/>

    <!-- 2. Static resource default servlet configuration
        (1) Add processing of static resources: js, gif, png
        (2) Allow the use of "/" for overall mapping
     -->
    <mvc:resources mapping="/css/**" location="/static/css/" />
    <mvc:resources mapping="/js/**" location="/static/js/"/>
    <mvc:resources mapping="/images/**" location="/static/images/"/>
    <mvc:default-servlet-handler/>


    <!-- 3. Configure jsp to display ViewResolver -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!-- 4. Scan web-related beans -->
    <context:component-scan base-package="com.ray.web"/>


</beans>


404.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<img src="images/404.png">
</body>
</html>


After the above files are configured, restart the server and enter the wrong address. Now the inserted 404 page is displayed normally.

This is my 404 page



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325402908&siteId=291194637