The page resources in spring mvc cannot read css and js static files, 404

Problem description: The page resources in spring mvc cannot read css and js static files. After splicing the URLs of the static resources by themselves, a 404 is reported.

http://localhost:8080/ds-sys-web/assets/css/ie10-viewport-bug-workaround.css

The static resource path is as follows:

 

Reason: The assets/css/ie10-viewport-bug-workaround.css part was intercepted and processed by spring. There is no controller configured with requestmapping as assets/css/ie10-viewport-bug-workaround.css, and a 404 is naturally reported.

 

Solution:

 

Method 1: Modify the url-pattern of the spring interceptor in web.xml as follows:

 

	<servlet>
		<servlet-name>SpringMVC</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
		<async-supported>true</async-supported>
	</servlet>
	<servlet-mapping>
		<servlet-name>SpringMVC</servlet-name>
		<url-pattern>*.shtml</url-pattern>
<!-- Previously configured as <url-pattern>/</url-pattern> -->
 	</servlet-mapping>

 Add a suffix to make spring only process urls ending in shtml

 

solve!

 

Method 2: Add the filtering url in web.xml:

	<servlet>
		<servlet-name>SpringMVC</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
		<async-supported>true</async-supported>
	</servlet>
	<servlet-mapping>
		<servlet-name>SpringMVC</servlet-name>
		<url-pattern>/</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>  
	<servlet-mapping>  
	    <servlet-name>default</servlet-name>             
	    <url-pattern>*.css</url-pattern>   
	</servlet-mapping>  
	<servlet-mapping>  
	    <servlet-name>default</servlet-name>             
	    <url-pattern>*.html</url-pattern>  
	</servlet-mapping>
  solve!

 

Method 3: Add the following configuration to the spring configuration file:

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

 solve!

 

 There are 5 configuration patterns for url-pattern:

(1) /xxx: exactly match the path of /xxx

(2) /xxx/*: matches paths starting with /xxx, and the request must contain xxx.

(3) /*: matches all paths under /, the request can enter the action or controller, but it is intercepted again when forwarding jsp and cannot access the jsp interface.

(4) .xx: matches paths ending with xx, all requests must end with .xx, but will not affect access to static files.

 

(5)/: The default mode, the unmatched paths will be mapped to the thorn servlet, and the static files such as jpg, js, and css will also be intercepted and cannot be accessed.

 

Guess you like

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