SpringBoot使用thymeleaf的简单记录

来源:互联网 发布:js对象深拷贝 编辑:程序博客网 时间:2024/06/05 10:02

一、引入jar包

compile('org.springframework.boot:spring-boot-starter-thymeleaf')

二、thymeleaf配置

在application.xml中配置如下

#模板的配置#关闭缓存,实时显示开发效果spring.thymeleaf.cache=false#指定类型spring.thymeleaf.content-type=text/html#指定编码spring.thymeleaf.encoding=UTF-8#指定模板的位置,默认/template/spring.thymeleaf.prefix=classpath:/templates/spring.thymeleaf.mode=HTML5#指定后缀spring.thymeleaf.suffix=.html

三、编写代码和页面

示例controller

import org.springframework.ui.Model;@Controllerpublic class IndexController {    @GetMapping(value = "/index")    public String index(Model model) {        String name = "百度";        String url = "http://www.baidu.com";        model.addAttribute("name", name);        model.addAttribute("url", url);        return "index";    }}

页面:

<!DOCTYPE HTML><html xmlns:th="http://www.thymeleaf.org">    <head>        <title>Index</title>    </head>    <body>        <a th:text="${name}" th:href="${url}"></a>    </body></html>

标签使用方法比较简单,教程一堆,用的时候去看很快就学会了。

原创粉丝点击