ThymeLeaf-HelloWorld

Controller

@Slf4j
@Controller
public class Controller {

    @GetMapping("/")
    public String index(Model model) {
        model.addAttribute("message", "Hello from Spring Boot!");
        return "index";
    }
}

注意这里要使用@Controller,别写习惯了还使用@RestController,那样的话返回的就是index字符串了

resource/templates/index.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Thymeleaf Demo</title>
    <link rel="stylesheet" href="/styles.css">
</head>
<body>
<header>
    <h1>Welcome to Thymeleaf Demo</h1>
</header>
<main>
    <p th:text="${message}">Hello, Thymeleaf!</p>
</main>
<footer>
    <p>© 2024 Thymeleaf Demo</p>
</footer>
</body>
</html>

resources/static/style.css

body {
    font-family: Arial, sans-serif;
    margin: 0;
    padding: 0;
    background-color: #f4f4f4;
}

header {
    background-color: #333;
    color: #fff;
    padding: 10px;
    text-align: center;
}

main {
    padding: 20px;
}

footer {
    background-color: #333;
    color: #fff;
    text-align: center;
    padding: 10px;
    position: fixed;
    bottom: 0;
    width: 100%;
}

最后访问 http://localhost:8080 不出意外能看到页面

posted @ 2024-09-03 18:07  iiiiiiiivan  阅读(23)  评论(0)    收藏  举报