gpt4 book ai didi

java - Thymeleaf 不显示值

转载 作者:行者123 更新时间:2023-11-30 06:31:04 24 4
gpt4 key购买 nike

我测试了 Thymeleaf,但遇到了问题,因为当我转到 localhost 并查看我的 html View 时,看不到我的“Test”值,该值应该显示在网站上。这是我的代码:

@Controller
public class DisplayData {

@RequestMapping("/display")
public String display(Model model){
model.addAttribute("now", "Test");
return "index.html";
}
}

html View :

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset=”utf-8″>
<title>Hello World</title>
</head>
<body>
<h1>Hello World</h1>
Now is: <b th:text="${now}"></b>
</body>
</html>

index.html 文件的路径是:resources/static/index.html

和本地主机 View : index.html

最佳答案

要使您的应用程序正常运行,需要进行三项更改:

  1. <meta charset=”utf-8″>没有正确引用。应该是<meta charset="UTF-8"/>
  2. index.html 文件的默认位置应为 resources/templates/index.html
  3. 您的返回声明应为 return "index"; ,由于您已将您的类注释为 @Controller,Spring Boot 将理解返回的字符串是要渲染的 View 的名称。然后它会自动检查上述位置以找到您的 View 文件。

您可以引用Spring Boot and Template Engines .

关于java - Thymeleaf 不显示值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46110527/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com