Javaweb Project static resource access issues

Source: Internet
Author: User

Today, there is a problem with the SSM project Web-inf static resources such as. css. js. Font and other files cannot be accessed. Baidu a bit, need to configure SPRINGMVC to access static resources support.

If the Dispatcherservlet request mapping is configured as "/", spring MVC captures all requests from the Web container, including requests for static resources, and spring MVC treats them as a normal request, so finding the corresponding processor will result in an error. This may be a historic issue. Since early spring MVC does not handle static resources well, configuring Dispatcherservlet request mappings in Web. XML often uses *.do, *.xhtml, and so on. This determines that the request URL must be a URL with a suffix, rather than a true restful URL.

How to enable the spring framework to capture requests for all URLs while simultaneously forwarding requests for static resources to the Web container is a precondition for configuring Dispatcherservlet request mappings as "/". Because rest is one of the most important features of Spring3.0, the spring team values the task of static resource handling, giving two classic solutions.

Method One: Adopt <mvc:default-servlet-handler/>

  After configuring <mvc:default-servlet-handler/> in Springmvc-servlet.xml, the spring The MVC context defines a org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler, which, like an inspector, screens the URL into the Dispatcherservlet If a request is found to be a static resource, the request is forwarded to the Web application server's default servlet processing, which is processed by Dispatcherservlet if it is not a request for a static resource.

General Web application Server The default servlet name is "default", so Defaultservlethttprequesthandler can find it. If your default servlet name for all Web application servers is not "default", you need to display the specified through the Default-servlet-name property:

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

Static resources in this way are best placed in the WebApp directory, which is the Web-inf sibling directory. More convenient addressing, the path is not very long:/Represents the WebApp directory.

  Method 2: Use <mvc:resources/>

For the time being not understand, read again to update.

Javaweb Project static resource access issues

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.