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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值