JavaWEB project static resource access problem

  I encountered a problem today, static resources such as .css .js .font and other files in the WEB-INF directory of the SSM project cannot be accessed. Baidu took a look, you need to configure SpringMvc's support for accessing static resources.

  If the DispatcherServlet request mapping is configured as "/", Spring MVC will capture all requests from the web container, including requests for static resources, and Spring MVC will treat them as a normal request, so it will cause an error if the corresponding handler cannot be found. This may be a historical question. Since the early Spring MVC cannot handle static resources well, the request mapping of DispatcherServlet is configured in web.xml, often using *.do, *.xhtml and other methods. This determines that the request URL must be a suffixed URL, and cannot be a true REST-style URL.

  How to enable the Spring framework to capture all URL requests and transfer requests for static resources to the Web container for processing is the premise that the request mapping of DispatcherServlet can be configured as "/". Since REST is one of the most important functions of Spring 3.0, the Spring team attaches great importance to the task of static resource processing and provides two classic solutions.

 

  Method 1: Use <mvc:default-servlet-handler />

  After configuring <mvc:default-servlet-handler /> in springMVC-servlet.xml, a org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler will be defined in the Spring MVC context, which will act like an inspector for entering DispatcherServlet If it is found to be a request for static resources, the request will be transferred to the default Servlet of the Web application server for processing. If it is not a request of static resources, the DispatcherServlet will continue to process it.

The default servlet name of a typical web application server is "default", so DefaultServletHttpRequestHandler can find it. If the default servlet name for all your web application servers is not "default", you need to explicitly specify it via the default-servlet-name attribute:

<mvc:default-servlet-handler default-servlet-name="The default servlet name used by the web server" />

  Method 2. Using <mvc:resources />

  I haven't understood it yet, but I'll update it when I understand it. .

Guess you like

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