springboot+springsecurity 静态资源路径问题以及Bootstrap失效问题


import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
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.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


import javax.servlet.MultipartConfigElement;

/**
 * @author Loggyf
 */

@Configuration
//@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry){
        registry.addViewController("/").setViewName("home");
        registry.addViewController("/new").setViewName("new");
        registry.addViewController("/login").setViewName("login");
        registry.addViewController("/about/new/update").setViewName("about/new/update");
        registry.addViewController("/shareContent").setViewName("shareContent");
    }

    /**
     * 添加对webjars的支持,
     * 将/webjars/** 自动解析到resources/webjars/,
     * 
     * 最重要的是 把static 映射到 classpath:/META-INF/resources/ 
     * 
     */
  
         @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/META-INF/resources/static/");

        //是classpath:/META-INF,不是/classpath:META-INFO/resoureces/webjars,区别很大的!!
    }

springboot+springsecurity 静态资源路径问题以及Bootstrap失效问题_第1张图片
image.png

城市

热门城市

最重要的是 把static 映射到 classpath:/META-INF/resources/

static映射之后,使用/static/**下面的资源是可以直接使用资源路径,不要写出static,否则引入失败
像下面一样使用即可


热门城市

前面定义了webjars的位置
在html中可以这样使用


  
  
  Title

如果不使用th:href.就需要使用相对路径。

如果你的html位于多个目录里,就有可能出现

  
  
  

这样不但很难读,而且增加了维护成本

在Thymeleaf中使用Bootstrap的modal("show")显示 modal is not a function...

需要先引入jquery.js
此外,还要注意版本的搭配。以Bootstrap3.3.1为例,最好搭配jqueery1.9.1

你可能感兴趣的:(springboot+springsecurity 静态资源路径问题以及Bootstrap失效问题)