静态资源映射

静态资源需要直接访问时,可以通过在配置文件中重写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");
    }
}

 

官方解释没有提及一点,就是不能使用@EnableWebMvc,当然如果Spring Boot在classpath里看到有 spring webmvc 也会自动添加@EnableWebMvc (http://spring.io/guides/gs/rest-service/)

如果@EnableWebMvc了,那么就会自动覆盖了官方给出的/static, /public, META-INF/resources, /resources等存放静态资源的目录。而将静态资源定位于src/main/webapp。当需要重新定义好资源所在目录时,则需要主动添加上述的那个配置类,来Override addResourceHandlers方法。

posted on 2018-09-28 13:24  永不宕机  阅读(667)  评论(0编辑  收藏  举报

导航