1. 程式人生 > >springMVC學習--3 靜態資源對映

springMVC學習--3 靜態資源對映

由於Servlet的攔截作用(servlet-mapping子元素的對映模式),一般無法直接訪問靜態資源。為了直接訪問js、css、圖片等靜態資源,在對配置類新增@EnableWebMvc基礎上,將配置類繼承自WebMvcConfigurerAdapter類,重寫其addResourceHandlers方法。重寫該方法,也就是將直接訪問靜態資源的路徑與靜態資源的位置進行了對映。如Http GET請求
http://localhost:8080/com-springmvc/assets/js/jquery-3.3.1.min.js
http://localhost:8080/com-springmvc/assets/photoes/timg.jpeg


可直接訪問/assets/**路徑匹配模式下,位於類路徑/assets/目錄下的檔案。路徑匹配模式詳見示例的第3小節。

示例:

1. 新增靜態資源

在src/main/resources下建立assets/js目錄,並複製一個jquery.js放置其下。

2. 改寫“springMVC學習–2 常用註解”中的配置類

package com.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework
.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web
.servlet.view.InternalResourceViewResolver; import org.springframework.web.servlet.view.JstlView; @Configuration @ComponentScan("com") @EnableWebMvc public class MvcConfig extends WebMvcConfigurerAdapter { @Bean InternalResourceViewResolver viewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix("/WEB-INF/classes/views/"); viewResolver.setSuffix(".jsp"); //JstlView is used to support java standard tag library and create jsp view viewResolver.setViewClass(JstlView.class); return viewResolver; } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/assets/"); } }

3. 路徑匹配模式

以下pathMatcher知識點來自於org.springframework.util.AntPathMatcher。

org.springframework.util.AntPathMatcher

PathMatcher implementation for Ant-style path patterns.

Part of this mapping code has been kindly borrowed from Apache Ant.

The mapping matches URLs using the following rules:
? matches one character
* matches zero or more characters
** matches zero or more directories in a path
 {spring:[a-z]+}} matches the regexp [a-z]+ as a path variable named "spring"
Examples
com/t?st.jsp — matches com/test.jsp but also com/tast.jsp or com/txst.jsp
com/*.jsp — matches all .jsp files in the com directory
com/**/test.jsp — matches all test.jsp files underneath the com path
org/springframework/**/*.jsp — matches all .jsp files underneath the org/springframework path
org/**/servlet/bla.jsp — matches org/springframework/servlet/bla.jsp but also org/springframework/testing/servlet/bla.jsp and org/servlet/bla.jsp
 com/{filename:\\w+}.jsp} will match com/test.jsp and assign the value test to the filename variable
Note: a pattern and a path must both be absolute or must both be relative in order for the two to match. Therefore it is recommended that users of this implementation to sanitize patterns in order to prefix them with "/" as it makes sense in the context in which they're used.

Since:
16.07.2003
Author:
Alef Arendsen
Juergen Hoeller
Rob Harrop
Arjen Poutsma
Rossen Stoyanchev
Sam Brannen