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