SpringBoot学习笔记

静态资源导入

通过源码 WebMvcAutoConfiguation 源码我们可以看出

public void addResourceHandlers(ResourceHandlerRegistry registry) {
   if (!this.resourceProperties.isAddMappings()) {
      logger.debug("Default resource handling disabled");
      return;
   }
   Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
   CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
   if (!registry.hasMappingForPattern("/webjars/**")) {
      customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**")
            .addResourceLocations("classpath:/META-INF/resources/webjars/")
            .setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
   }
   String staticPathPattern = this.mvcProperties.getStaticPathPattern();
   if (!registry.hasMappingForPattern(staticPathPattern)) {
      customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern)
            .addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
            .setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
   }
}
  • 可以通过自定义来设置静态资源位置

  • 可以通过webjar访问静态资源

  • 查看getStaticLocations(),我们可以发现

  • private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
          "classpath:/resources/", "classpath:/static/", "classpath:/public/" };
    
  • 通过这几个路径都可以访问到静态资源

    private Resource getIndexHtml(String location) {
       return this.resourceLoader.getResource(location + "index.html");
    }
    
  • 全局搜索,双shift

  • 找到indexhtml源码后,我们发现老几样的位置+“index,html”就可以实现访问首页了

    Thymeleaf模板引擎

  • 和jsp一样,thymeleaf模板引擎可以让我们在HTML页面中取值等操作

  • <!DOCTYPE html>
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>Title</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <link rel="stylesheet" type="text/css" media="all"
              href="../../css/gtvg.css" th:href="@{/css/gtvg.css}" />
    
    </head>
    
  • 页面约束

  • <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    
  • pom约束

  • 在controller中传值

  • @Controller
    public class TestController {
        @RequestMapping("/a")
        public String test(Model model){
            model.addAttribute("msg","message");
            return "test";
        }
    }
    
<body>
    <div th:text="${msg}"></div>
</body>
  • 取值,收工

  • 注意,template中的页面只能有controller访问

    拓展以及装配SpringMVC

    Spring Boot provides auto-configuration for Spring MVC that works well with most applications.

    The auto-configuration adds the following features on top of Spring’s defaults:

    • Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.
    • Support for serving static resources, including support for WebJars (covered later in this document)).
    • Automatic registration of Converter, GenericConverter, and Formatter beans.
    • Support for HttpMessageConverters (covered later in this document).
    • Automatic registration of MessageCodesResolver (covered later in this document).
    • Static index.html support.
    • Custom Favicon support (covered later in this document).
    • Automatic use of a ConfigurableWebBindingInitializer bean (covered later in this document).

    If you want to keep those Spring Boot MVC customizations and make more MVC customizations (interceptors, formatters, view controllers, and other features), you can add your own @Configuration class of type WebMvcConfigurer but without @EnableWebMvc.

    If you want to provide custom instances of RequestMappingHandlerMapping, RequestMappingHandlerAdapter, or ExceptionHandlerExceptionResolver, and still keep the Spring Boot MVC customizations, you can declare a bean of type WebMvcRegistrations and use it to provide custom instances of those components.

    If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc, or alternatively add your own @Configuration-annotated DelegatingWebMvcConfiguration as described in the Javadoc of @EnableWebMvc.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值