Spring requests a static resource file

Source: Internet
Author: User

Using restful style writing in the SPRINGMVC project, you need to configure full interception to Web. Xml.

<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

However, static resources such as IMAGE,JS,CSS do not want to intercept the project. There are three ways to configure it:

I. Web. XML filtering

<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>

But this needs to be configured for each type, cumbersome and easy to miss. To be straightforward, spring provides two ways to handle it. You need to include the MVC tag in the spring configuration file

Xmlns:mvc= "Http://www.springframework.org/schema/mvc"
Xsi:schemalocation= "
Http://www.springframework.org/schema/mvc
Http://www.springframework.org/schema/mvc/spring-mvc.xsd "

Second, the use of mvc:resources.

<mvc:resources location= "/style/" mapping= "/style/**"/>

Third, use the default servlet to respond to static files <mvc:default-servlet-handler/>

Spring requests a static resource file

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.