Spring boot 配置 webapp 下资源文件

spring boot 在 1.5.x以后已经不支持 jar 打包使用 jsp 了,必须使用 war 打包,引入 static 下静态资源文件可以使用 classpath:/static/,如果指定到 webapp 下静态资源文件不能使用 classpath:,直接以 / 开头就可以。

import java.util.List;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.filter.HttpPutFormContentFilter;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
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.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import com.znv.framework.common.error.ServiceExceptionHandler;
import com.znv.framework.interceptors.CommonInterceptor;
import com.znv.framework.spring.SpringContextUtil;

/**
 * @author Chenfei
 */
@EnableWebMvc
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:/views/default");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    } 

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        /**
         * 自定义拦截器
         */
        registry.addInterceptor(new CommonInterceptor()).addPathPatterns("/**").excludePathPatterns("/resources/**",
            "/main/uscc/**", "/lang/**", "/**/js/**", "/**/css/**", "/**/*.xml", "/**/bootstrap/**", "/**/plugins/**",
            "/**/404", "/**/500", "/**/error", "/webcontent/**", "/files/**", "/main/mobile/**", "/mobile/**");
    }

    /**
     * 关于PUT方法获取不到参数的处理
     */
    @Bean
    public FilterRegistrationBean hiddenHttpMethodFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        //注入过滤器
        registration.setFilter(new HiddenHttpMethodFilter());
        //拦截规则
        registration.addUrlPatterns("/");
        //过滤器名称
        registration.setName("HiddenHttpMethodFilter");
        //过滤器顺序
        registration.setOrder(1);
        return registration;
    }

    /**
     * 关于PUT方法获取不到参数的处理
     */
    @Bean
    public HttpPutFormContentFilter httpPutFormContentFilter() {
        return new HttpPutFormContentFilter();
    }

    /**
     * 自定义静态资源映射
     */
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/",
            "classpath:/resources/", "classpath:/static/", "classpath:/public/");
        registry.addResourceHandler("/views/**").addResourceLocations("classpath:/static/main/");
        // 指到 webapp 目录下
        registry.addResourceHandler("/webcontent/**").addResourceLocations("/webcontent/");
    }

    /**
     * 视图配置
     * @return
     */
    @Bean
    public InternalResourceViewResolver resourceViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/main/");
        resolver.setSuffix("");
        return resolver;
    }

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.viewResolver(resourceViewResolver());
//        registry.jsp("", ".jsp");
    }

你可能感兴趣的:(Spring,Java技术,学习笔记)