springboot2.1入门系列十 springboot配置thymeleaf

本文为Spring Boot2.1系列的第十篇,代码可以从github下载 https://github.com/yinww/demo-springboot2.git

thymeleaf是springboot默认支持的模板引擎,这足以说明thymeleaf具备很多优点,在这里不做说明,相关内容大家可以参考其他资料。

但是使用springboot + thymeleaf技术后经常会思考一个问题,应用上线后可能是一个独立的部署jar包,如果想简单的改一点模板的内容或者修改css样式,就需要重新打包,这对于测试环境下和某些线上环境迅速跟踪问题可能带来不方便。

本文对Thymeleaf的模板资源和静态资源做外部化配置进行介绍。

一、创建工程demo010

pom.xml的内容为

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.yinww</groupId>
        <artifactId>demo-springboot2</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>demo010</artifactId>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

二、Java类

主类

package com.yinww.demo.springboot2.demo010;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Demo010Application {

    public static void main(String[] args) {
        SpringApplication.run(Demo010Application.class, args);
    }

}

控制类

package com.yinww.demo.springboot2.demo010.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class TestController {

    @RequestMapping(value={"", "/", "index.html"})
    public ModelAndView index() {
        ModelAndView model = new ModelAndView();
        model.addObject("hi", "hi, springboot and thymeleaf");
        model.setViewName("index");
        return model;
    }

}

三、配置

application.properties 添加配置

# spring.thymeleaf.prefix=file:src/main/resources/templates/
spring.thymeleaf.prefix=file:E:/tmp/templates/
spring.thymeleaf.cache=false

spring.resources.static-locations=file:E:/tmp/static/

这里有一个坑:模板路径和静态资源路径都必须以 “/” 结尾,否则程序会报错,提示找不到资源文件。

四、html和css

E:/tmp/templates/ 路径下 index.html文件内容:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" th:href="@{welcome.css}">
<title>hello33</title>
</head>
<body>

    <div th:text="${hi}" class="welcome">hello</div>

</body>
</html>

E:/tmp/static/ 路径下welcom.css 文件内容:

.welcome{color: #F00}

五、运行程序

启动程序后,访问 http://localhost:8080/  页面显示如下

至此实现了对thymeleaf的模板和静态资源的外部化配置,thymeleaf的其他参数配置相对比较简单,这里不做介绍。

本文内容到此结束,更多内容可关注公众号

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值