SpringMVC js and other files cannot be found

Source: Internet
Author: User

If URL ing is configured when spring MVC is applied


[Html]
<Servlet>
<Servlet-name> appServlet </servlet-name>
<Servlet-class> org. springframework. web. servlet. DispatcherServlet </servlet-class>
<Init-param>
<Param-name> contextConfigLocation </param-name>
<Param-value>/WEB-INF/spring/appServlet/servlet-context.xml </param-value>
</Init-param>
<Load-on-startup> 1 </load-on-startup>
</Servlet>
 
<Servlet-mapping>
<Servlet-name> appServlet </servlet-name>
<Url-pattern>/</url-pattern>
</Servlet-mapping>

The js css referenced on the page may fail to be found. There are two ways to solve the problem:

Method 1: Add in web. xml


[Html]
<Servlet-mapping>
<Servlet-name> default </servlet-name>
<Url-pattern> *. css </url-pattern>
</Servlet-mapping>
 
<Servlet-mapping>
<Servlet-name> default </servlet-name>
<Url-pattern> *. gif </url-pattern>
</Servlet-mapping>
 
<Servlet-mapping>
<Servlet-name> default </servlet-name>
<Url-pattern> *. jpg </url-pattern>
</Servlet-mapping>
 
<Servlet-mapping>
<Servlet-name> default </servlet-name>
<Url-pattern> *. js </url-pattern>
</Servlet-mapping>
Specifies that spring filters are not used for the above resource files.
About default is a servlet configured for tomcat, "DefaultServlet is defined in $ CATALINA_HOME/conf/web. in xml, what is defined in the configuration file will be loaded at Tomcat startup, which is valid for all webapps. DefaultServlet is mainly used for Directory Listing in Tomcat"


[Html]
<Servlet>
<Servlet-name> default </servlet-name>
<Servlet-class>
Org. apache. catalina. servlets. DefaultServlet
</Servlet-class>
<Init-param>
<Param-name> debug </param-name>
<Param-value> 0 </param-value>
</Init-param>
<Init-param>
<Param-name> listings </param-name>
<Param-value> true </param-value>
</Init-param>
<Load-on-startup> 1 </load-on-startup>
</Servlet>
 
...
 
<Servlet-mapping>
<Servlet-name> default </servlet-name>
<Url-pattern>/</url-pattern>
</Servlet-mapping>

Some parameters can control the defaservlet servlet behavior. In addition, when the requested URL is/, that is, the DefaultServlet will be triggered when no matching is found.


Method 2: use the resources annotation of spring mvc to process static files.

Spring mvc's <mvc; resources mapping = "***" location = "***"> labels appear in spring3.0.4 and are mainly used to access static resources. Spring has not updated its schema when spring3.0.4 is released, so the <mvc: resources> label may not be found in the configuration file. This problem has been solved in spring3.0.5, there are also many other solutions on the Internet, which I will not record here.

First, use spring mvc to configure the servlet to be used. In web. xml:


[Html]
<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>
This will affect the acquisition of static resource files, so you need to have this tag to help you classify and complete the responsibility for obtaining static resources.
So add the following code to the springMVC-servlet.xml file:


[Html]
<? 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: mvc = "http://www.springframework.org/schema/mvc"
Xsi: schemaLocation = "http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
Http://www.springframework.org/schema/mvc
Http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd>

<Mvc: resources mapping = "/javascript /**"
Location = "/static_resources/javascript/"/>
<Mvc: resources mapping = "/styles /**"
Location = "/static_resources/css/"/>
<Mvc: resources mapping = "/images /**"
Location = "/static_resources/images/"/>
<Mvc: default-servlet-handler/>


<Bean class = "org. springframework. web. servlet. view. InternalResourceViewResolver">
<Property name = "prefix" value = "/WEB-INF/views/"/>
<Property name = "suffix" value = ". jsp"/>
</Bean>

</Beans>

Here we can see that all the resources referenced by/styles/** on my pages are searched from/static_resources/css.

 

Based on the above two methods, you can access the static resources of the site.

 

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.