Springmvc JS and other files can not find errors, that is, access to static resources __js

Source: Internet
Author: User
When you apply Springmvc, configure URL mappings as follows
<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> 
     

will cause the page to reference the JS CSS can not find errors, the following two ways to solve the problem, for reference only:

Method One: Add in Web.xml

<servlet-mapping> <servlet-name>default</servlet-name> <url-pat tern>*.css</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-na 
  
     Me>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>defa ult</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> 


Indicates that no spring filter is used for resource files such as the above
About default is a servlet of Tomcat configuration, "Defaultservlet is defined in $catalina_home/conf/web.xml, and what is defined in the configuration file is loaded at Tomcat startup, is effective for all webapp. Defaultservlet is primarily a directory listing (directory Listing) in Tomcat to indicate that no spring filters are used for resource files such as the above
About default is a servlet of Tomcat configuration, "Defaultservlet is defined in $catalina_home/conf/web.xml, and what is defined in the configuration file is loaded at Tomcat startup, is effective for all webapp. Defaultservlet is primarily a directory listing (directory Listing) in Tomcat to indicate that no spring filters are used for resource files such as the above

About default is a servlet of Tomcat configuration, "Defaultservlet is defined in $catalina_home/conf/web.xml, and what is defined in the configuration file is loaded at Tomcat startup, is effective for all webapp. Defaultservlet is primarily a directory listing (directory Listing) in Tomcat

<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>  
     

There are some parameters to control the behavior of Defaultservlet. In addition, this defaultservlet is triggered when the requested URL is/is not matched.


Method Two: Use the resources annotation of spring MVC to process static files

Spring MVC's <mvc;resources mapping= "* *" location= "* *" > tags appear in spring3.0.4, mainly for static resource access. Spring has not yet updated its schema when the spring3.0.4 comes out, so it is possible that <mvc:resources > tags are not found in the configuration file, This problem has been solved in the spring3.0.5, and there are many other solutions on the Internet, I do not record here.

First you use spring MVC to configure the servlet that it uses. In Web.xml:

There are some parameters to control the behavior of Defaultservlet. In addition, this defaultservlet is triggered when the requested URL is/is not matched.

Method Two: Use the resources annotation of spring MVC to process static files

Spring MVC's <mvc;resources mapping= "* *" location= "* *" > tags appear in spring3.0.4, mainly for static resource access. Spring has not yet updated its schema when the spring3.0.4 comes out, so it is possible that <mvc:resources > tags are not found in the configuration file, This problem has been solved in the spring3.0.5, and there are many other solutions on the Internet, I do not record here.

First you use spring MVC to configure the servlet that it uses. In 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>   
 


This will affect the acquisition of static resource files, so you need to have this tag to help you classify the responsibility for obtaining static resources.
So add the following code in the Springmvc-servlet.xml file

<?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/schem A/mvc "xsi:schemalocation=" Http://www.springframework.org/schema/beans Http://www.springframework.org/schem A/beans/spring-beans-3.0.xsd Http://www.springframework.org/schema/mvc HTTP://WWW.SPRINGFRAMEWORK.ORG/SC Hema/mvc/spring-mvc-3.0.xsd "> <mvc:resources mapping="/javascript/** "location="/ static_resources/javascript/"/> <mvc:resources mapping="/styles/** "location="/static_resourc   
         es/css/"/> <mvc:resources mapping=/images/**" location= "/static_resources/images/"/> <mvc:default-servlet-handler/> <bean class= "Org.springframework.web.s Ervlet.view.InternalResoUrceviewresolver "> <property name=" prefix "value="/web-inf/views/"/> <property    Name= "suffix" value= ". jsp"/> </bean> </beans>


Here you can see all of my page references to/styles/** resources are searched from inside the/static_resources/css.

Based on the above two methods, you can access the static resources to 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.