templates基础

templates

Thymeleaf是个XML/XHTML/HTML5模板引擎,可以用于Web与非Web应用。
简单说, Thymeleaf 是一个模板引擎,它可以完全替代 JSP 。

依赖

springboot直接引入:

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

非springboot项目使用如下依赖:

<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf</artifactId>
<version>2.1.4</version>
</dependency>

默认视图解析器

#thymeleaf start
#thymeleaf配置
#模板的模式,支持如:HTML、XML、TEXT、JAVASCRIPT等
spring.thymeleaf.mode=HTML5
#编码,可不用配置
spring.thymeleaf.encoding=UTF-8
#内容类别,可不用配置
spring.thymeleaf.servlet.content-type=text/html
#开发配置为false,避免修改模板还要重启服务器
spring.thymeleaf.cache=false
#配置模板路径,默认就是templates,可不用配置
spring.thymeleaf.prefix=classpath:/templates/
server.port=80
server.servlet.context-path=/
spring.thymeleaf.suffix=.html

templates的使用

编写controller

@RequestMapping("tem")
public String index1(Model model){
    User user = userService.getById(1);
    model.addAttribute("user",user);
    return "index";
}

在这里插入图片描述
注意 html需要声明这个html是thym xmlns:th=“http://www.thymeleaf.org”

<!DOCTYPE html>
<html  lang="en"  xmlns:th="http://www.thymeleaf.org"  >
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <input th:value="${user.userName}"/>
</body>
</html>
posted @   Patrick-Rex  阅读(17)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
点击右上角即可分享
微信分享提示