静态资源映射

Posted zhlblogs

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了静态资源映射相关的知识,希望对你有一定的参考价值。

静态资源需要直接访问时,可以通过在配置文件中重写addResourceHandlers方法来实现

package com.zhl.practice.config;

import com.zhl.practice.MyInteceptor.DemoInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
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;

/**
 * @author Holley
 * @Description 如果访问的静态文件不是以下路径则会被拦截
 * @create 2018-09-28 11:05
 **/
@Configuration
@EnableWebMvc
public class MvcConfig extends WebMvcConfigurerAdapter {

    /**
      * @Author holley
      * @Description 设置MVC的视图解析
      * @Date 2018/9/28 11:29
      * @Param []
      * @return org.springframework.web.servlet.view.InternalResourceViewResolver
      */
    @Bean
    public InternalResourceViewResolver viewResolver(){
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/classes/views");
        viewResolver.setSuffix(".jsp");
        viewResolver.setViewClass(JstlView.class);
        return viewResolver;
    }

    /**
      * @Author holley
      * @Description 设置静态资源文件的映射
      * @Date 2018/9/28 11:34
      * @Param [registry]
      * @return void
      */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // addResourceHandler 设置对外暴露的访问路径 ,addResourceLocations 设置的是文件放置的目录
        registry.addResourceHandler("/test/**")
                .addResourceLocations("classpath:/tset/");
    }

    /**
      * @Author holley
      * @Description java配置bean
      * @Date 2018/9/28 11:49
      * @Param []
      * @return com.zhl.practice.MyInteceptor.DemoInterceptor
      */
    @Bean
    public DemoInterceptor demoInterceptor(){
        return new DemoInterceptor();
    }

    /**
      * @Author holley
      * @Description 设置拦截器(调用自定义拦截器)
      * @Date 2018/9/28 11:35
      * @Param [registry]
      * @return void
      */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(demoInterceptor());
    }
}

 

 

自定义拦截器

package com.zhl.practice.MyInteceptor;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Holley
 * @Description 自定义MVC拦截器
 * @create 2018-09-28 11:38
 **/
public class DemoInterceptor extends HandlerInterceptorAdapter{

    /**
      * @Author holley
      * @Description 重写前置拦截
      * @Date 2018/9/28 11:40
      * @Param [request, response, handler]
      * @return boolean
      */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        long startTime = System.currentTimeMillis();
        request.setAttribute("startTime",startTime);
        return true;
    }

    /**
     * @Author holley
     * @Description 重写后置拦截
     * @Date 2018/9/28 11:40
     * @Param [request, response, handler]
     * @return boolean
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        long startTime = (Long)request.getAttribute("startTime");
        request.removeAttribute("startTime");
        long endTime = System.currentTimeMillis();
        System.out.println("本次请求处理时间为:"+(endTime - startTime) + "ms");
    }
}

 

以上是关于静态资源映射的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot自定义静态资源映射

SpringBoot2---静态资源映射规则

SpringBoot:静态资源映射定制404配置icon

spring boot 静态资源的映射规则 替他资源映射

5.对静态资源映射的规则

spring boot 静态资源的映射规则 欢迎页面映射