码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot 与 Web开发

时间:2018-06-05 23:13:53      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:control   模板   jar包   ppi   目录   meta   oca   and   addm   

1. SpringBoot 静态资源映射规则

  • webjars:以JAR包的方式引入静态资源;
  • 所有/webjars/**,都去classpath:/META-INF/resources/webjars/目录下加载资源;
  • /**访问当前项目的任何资源(即静态资源文件夹)
    • classpath:/META-INF/resources/
    • classpath:/resources/,
    • classpath:/static/,
    • classpath:/public/,
    • /:当前项目的根路径
// WebMvcAutoConfiguration.java
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    if (!this.resourceProperties.isAddMappings()) {
        logger.debug("Default resource handling disabled");
        return;
    }
    Integer cachePeriod = this.resourceProperties.getCachePeriod();
    if (!registry.hasMappingForPattern("/webjars/**")) {
        customizeResourceHandlerRegistration(registry
                .addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/")
                .setCachePeriod(cachePeriod));
    }
    String staticPathPattern = this.mvcProperties.getStaticPathPattern();
    if (!registry.hasMappingForPattern(staticPathPattern)) {
        customizeResourceHandlerRegistration(
                registry.addResourceHandler(staticPathPattern)
                        .addResourceLocations(
                                this.resourceProperties.getStaticLocations())
                        .setCachePeriod(cachePeriod));
    }
}

2. Thymeleaf 模板引擎

  • SpringBoot 引入 Thymeleaf 模板引擎
// pom.xml

<properties>
    <!-- 切换 thymeleaf 版本 --
    <thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
    <thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
</properties>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>


// classpath:/templates/, 存放HTML页面
success.html

// 编写Controller
@RequestMapping("/success")
public String success(){
    return "success";
}

参考资料:

SpringBoot 与 Web开发

标签:control   模板   jar包   ppi   目录   meta   oca   and   addm   

原文地址:https://www.cnblogs.com/linkworld/p/9142306.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!