springboot+Thymeleaf demo 每天进步百分之一

来源:互联网 发布:小说淘宝网免费阅读 编辑:程序博客网 时间:2024/05/29 02:48

1.导入架包

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

2.配置Thymeleaf

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

3.写个测试的controller

@Controllerpublic class ThymeleafTestController {    @GetMapping("/hello")    public String helloHtml(ModelMap map){            map.put("hello","thyeleaf测试");         return "/index";     }}

4.写个测试的页面

<!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"      xmlns:th="http://www.thymeleaf.org"><head>    <title>Hello World!</title></head><body>    <h1 th:inline="text">Hello</h1>    <p th:text="${hello}"></p></body></html>

注:测试页面的文件名必须与controller返回的一致
5运行项目查看测试页面数据
http://localhost:8080/hello
这里写图片描述

原创粉丝点击