第二步 thymeleaf 模版引擎

107 阅读1分钟

一 最简单的传值。

控制器代码 。

package com.xuxing.study2;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class IndexController {

    @RequestMapping("/index")
    public String index(Model model)
    {
        model.addAttribute("msg", "hello world");
        return "index";
    }

}

视图层代码

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>

</head>
<body>
3333333333
<div th:text="${msg}"></div>

44444444444

</body>
</html>

image.png

!!坑!! 注意浏览器缓存的坑,代码改了,然后没效果。

二 遍历。

//控制器代码 
model.addAttribute("users", Arrays.asList("xuxing", "hello"));

//视图代码 
<h3 th:each="user:${users}" th:text="${user}"></h3>

其它的东西。

  1. #{} 国际化消息
  2. ~{} 片断化表达示
  3. @{} 链接。
  4. *{} 选项

数字直接写,文本操作用 + 参考官网。 www.thymeleaf.org/documentati…