thymeleaf的使用

1,新建ThymeleafContorllor类

package com.xiang.controller;

import com.xiang.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@Controller
@RequestMapping("/xiang")

    /*
    thymeleaf 的使用
    */

    @GetMapping("/user/thymeleaf")
    public String thymeleaf(Model model) {
//        传参
        model.addAttribute("name","小不点");
        return "thymeleaf";
    }

}

2,在resources 下的 templates目录下编写前端代码

<!DOCTYPE html>
<!--引命名规则-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>thymeleaf</title>
</head>
<body>
<h1>小向</h1>
<!--绑messages-->
<p th:text="#{user.name}"></p>

<!--绑变量-->
<h3 th:text="${name}"></h3>
</body>
</html>

注,绑messages 的话,需要在resources 目录下新建 messages.properties 文件 (这儿内容如下:)

user.name = 向向

3,运行结果截图

原文地址:https://www.cnblogs.com/d534/p/14887945.html