thymeleaf 选择excel_细品 Spring Boot+Thymeleaf,还有这么多好玩的细节!

原标题:细品 Spring Boot+Thymeleaf,还有这么多好玩的细节!

来自公众号: 江南一点雨

虽然现在流行前后端分离,但是后端模版在一些关键地方还是非常有用的,例如邮件模版、代码模版等。当然也不排除一些古老的项目后端依然使用动态模版。

Thymeleaf 简洁漂亮、容易理解,并且完美支持 HTML5,可以直接打开静态页面,同时不新增标签,只需增强属性,这样也降低了学习成本。

因此松哥今天花点时间和大家仔细分享一下 Thymeleaf。

1. Thymeleaf 简介

Thymeleaf 是新一代 Java 模板引擎,它类似于 Velocity、FreeMarker 等传统 Java 模板引擎,但是与传统 Java 模板引擎不同的是,Thymeleaf 支持 HTML 原型。

它既可以让前端工程师在浏览器中直接打开查看样式,也可以让后端工程师结合真实数据查看显示效果,同时,SpringBoot 提供了 Thymeleaf 自动化配置解决方案,因此在 SpringBoot 中使用 Thymeleaf 非常方便。

事实上, Thymeleaf 除了展示基本的 HTML ,进行页面渲染之外,也可以作为一个 HTML 片段进行渲染,例如我们在做邮件发送时,可以使用 Thymeleaf 作为邮件发送模板。

另外,由于 Thymeleaf 模板后缀为 .html ,可以直接被浏览器打开,因此,预览时非常方便。

2. 整合 Spring Boot2.1 基本用法

Spring Boot 中整合 Thymeleaf 非常容易,只需要创建项目时添加 Thymeleaf 即可:

创建完成后,pom.xml 依赖如下:

< 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>

当然,Thymeleaf 不仅仅能在 Spring Boot 中使用,也可以使用在其他地方,只不过 Spring Boot 针对 Thymeleaf 提供了一整套的自动化配置方案,这一套配置类的属性在 org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties 中,部分源码如下:

@ConfigurationProperties(prefix = "spring.thymeleaf")

publicclassThymeleafProperties{

privatestaticfinalCharset DEFAULT_ENCODING = StandardCharsets.UTF_8;

publicstaticfinalString DEFAULT_PREFIX = "classpath:/templates/";

publicstaticfinalString DEFAULT_SUFFIX = ".html";

privatebooleancheckTemplate = true;

privatebooleancheckTemplateLocation = true;

privateString prefix = DEFAULT_PREFIX;

privateString suffix = DEFAULT_SUFFIX;

privateString mode = "HTML";

privateCharset encoding = DEFAULT_ENCODING;

privatebooleancache = true;

//...

}

首先通过 @ConfigurationProperties 注解,将 application.properties 前缀为 spring.thymeleaf 的配置和这个类中的属性绑定。

前三个 static 变量定义了默认的编码格式、视图解析器的前缀、后缀等。

从前三行配置中,可以看出来, Thymeleaf 模板的默认位置在 resources/templates 目录下,默认的后缀是 html 。

这些配置,如果开发者不自己提供,则使用 默认的,如果自己提供,则在 application.properties 中以 spring.thymeleaf 开始相关的配置。

而我们刚刚提到的,Spring Boot 为 Thymeleaf 提供的自动化配置类,则是 org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration ,部分源码如下:

@Configuration

@EnableConfigurationProperties(ThymeleafProperties . class)

@ ConditionalOnClass( { TemplateMode . class, SpringTemplateEngine. class})

@ AutoConfigureAfter( { WebMvcAutoConfiguration . class, WebFluxAutoConfiguration. class})

publicclassThymeleafAutoConfiguration{

}

可以看到,在这个自动化配置类中,首先导入 ThymeleafProperties ,然后 @ConditionalOnClass 注解表示当当前系统中存在 TemplateMode 和 SpringTemplateEngine 类时,当前的自动化配置类才会生效,即只要项目中引入了 Thymeleaf 相关的依赖,这个配置就会生效。

这些默认的配置我们几乎不需要做任何更改就可以直接使用了。如果开发者有特殊需求,则可以在 application.properties 中配置以 spring.thymeleaf 开头的属性即可。

接下来我们就可以创建 Controller 了,实际上引入 Thymeleaf 依赖之后,我们可以不做任何配置。新建的 IndexController 如下:

@Controller

publicclassIndexController{

@GetMapping( "/index")

publicString index(Model model){

List users = newArrayList<>;

for( inti = 0; i < 10; i++) {

User u = newUser;

u.setId(( long) i);

u.setName( "javaboy:"+ i);

u.setAddress( "深圳:"+ i);

users.add(u);

}

model.addAttribute( "users", users);

return"index";

}

}

publicclassUser{

privateLong id;

privateString name;

privateString address;

//省略 getter/setter

}

在 IndexController 中返回逻辑视图名+数据,逻辑视图名为 index ,意思我们需

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值