springboot中整合thymeleaf模板

http://www.cnblogs.com/moonlightL/p/7891806.html
http://www.ityouknow.com/springboot/2016/02/03/springboot(%E4%BA%8C)-web%E7%BB%BC%E5%90%88%E5%BC%80%E5%8F%91.html


一、什么是 Thymeleaf?
Thymeleaf是一款用于渲染XML/XHTML/HTML5内容的模板引擎。类似JSP,Velocity,FreeMaker等
也可以轻易的与Spring MVC等Web框架进行集成作为Web应用的模板引擎。与其它模板引擎相比,Thymeleaf最大的特点是能够直接在浏览器中打开并正确显示模板页面,而不需要启动整个Web应用。


URL
URL在Web应用模板中占据着十分重要的地位,需要特别注意的是Thymeleaf对于URL的处理是通过语法
@{…}来处理的。Thymeleaf支持绝对路径URL:


<a th:href="@{http://www.thymeleaf.org}">Thymeleaf</a>




条件求值
<a th:href="@{/login}" th:unless=${session.user != null}>Login</a>




for循环
<tr th:each="prod : ${prods}">
 
      <td th:text="${prod.name}">Onions</td>
 
      <td th:text="${prod.price}">2.41</td>
 
      <td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
<
</tr>


二、整合Thymeleaf
1、添加Thymeleaf依赖
pom.xml 文件中添加:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
    2、添加 Thymeleaf 模板配置
application.properties 中添加如下内容:


spring.thymeleaf.cache=true
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html


3、 controller 包中创建 ThymeleafController:
@Controller
@RequestMapping("thymeleaf")
public class ThymeleafController {


    @RequestMapping("hello")
    public String hello(Map<String,Object> map) {
        map.put("msg", "Hello Thymeleaf");
        return "hello";
    }
}
4、在 templates 目录下创建名为 hello.html 的文件,内容如下:
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link href="/css/index.css" rel="stylesheet"/>
</head>
<body>
    <div class="container">
        <h2 th:text="${msg}"></h2>
    </div>
</body>
</html>
5、启动后访问:http://localhost:8080/thymeleaf/hello





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring BootThymeleaf 模板引擎集成Thymeleaf 模板引擎,可以轻松地与 Spring Boot 应用程序集成Thymeleaf 是一个流行的模板引擎,它为 Web 应用程序提供了强大的视图层支持,允许您使用 HTML 模板来构建 Web 应用程序的视图层。 在 Spring Boot ,您可以使用以下步骤来集成 Thymeleaf 模板引擎: 1. 在 pom.xml 文件添加 Thymeleaf 依赖项: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> ``` 2. 在 application.properties 文件配置 Thymeleaf 模板引擎: ```properties spring.thymeleaf.mode=HTML spring.thymeleaf.cache=false ``` 3. 创建 Thymeleaf 模板文件: 在 src/main/resources/templates 文件夹下创建 HTML 文件,这些文件将作为视图层的模板文件。您可以使用 Thymeleaf 提供的标准语法来构建模板文件。 4. 创建控制器类: 创建控制器类来处理 Web 请求,并返回 Thymeleaf 模板文件作为响应。您可以使用 @Controller 注解将类标记为控制器类,并使用 @GetMapping 或 @PostMapping 注解将方法标记为处理 GET 或 POST 请求。 ```java @Controller public class MyController { @GetMapping("/") public String home(Model model) { model.addAttribute("message", "Hello, Thymeleaf!"); return "home"; } } ``` 在这个例子,控制器类将一个名为 message 的属性添加到模型,并返回名为 home 的 Thymeleaf 模板文件。 5. 在 Thymeleaf 模板文件使用模型数据: 使用 Thymeleaf 标准语法在模板文件使用模型数据。在本例,您可以使用 ${message} 表达式来访问 message 属性的值。 ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Home</title> </head> <body> <h1 th:text="${message}"></h1> </body> </html> ``` 6. 运行应用程序: 使用 mvn spring-boot:run 命令来启动应用程序。访问 http://localhost:8080/ 将显示名为 home 的 Thymeleaf 模板文件,其包含 message 属性的值。 上述是 Spring Boot 整合 Thymeleaf 的基本流程和步骤,您可以根据实际需求和业务场景进行适当的调整和修改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值