java - 尝试 thymeleaf 没有成功

标签 java spring-boot gradle thymeleaf

我正在尝试使用 thymeleaf,因为它在所有教程中都有描述,但不知何故我的 HTML 无法加载。

这是我的项目结构:

structure 这些是依赖项:

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile("org.springframework.boot:spring-boot-starter-web:2.0.4.RELEASE")
    compile group: 'org.thymeleaf', name: 'thymeleaf', version: '2.0.5'
}

除了打印出“Hello”消息外,它什么也没做,但是,没有使用资源文件夹中的 HTML。我错过了什么?

HelloController.java 只有一个方法:

@RequestMapping("/hello")
  public String hello(Model model, @RequestParam(value="name", 
    required=false, defaultValue="World") String name) {
    model.addAttribute("name", name);
    return "hello " + name;
  }

main 方法只是通常的运行。

最佳答案

model.addAttribute 可以获取 html 文件中的数据。 方法中的返回值应返回所需模板的名称。例如你的 hello.html

在你的 hello.html 中放置这样的东西:

<p th:text="${name}"></p>

那么它应该可以工作了。

你的 Controller 看起来像这样,所以返回包含你的模板名称 hello 来自 hello.html:

@RequestMapping(value="/hello", method= RequestMethod.GET)
public String hello(Model model, @RequestParam(value="name", required=false, defaultValue="World") String name) {
model.addAttribute("name", name);
return "hello";
}

关于java - 尝试 thymeleaf 没有成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51632276/

相关文章:

spring-boot - 使用 Spring Boot Actuator 的 Junit 测试给出异常

java - 如何使用 Spring Boot 在 REST Web 服务中的 POST 请求期间修复 'HTTP-415' 错误

android - 如何在 Android Studio 中授予对 registry.bin 的访问权限?

Spring 启动@Configurable

gradle - 为什么MyEclipse不将项目识别为Gradle项目?

groovy - Gradle任务创建

java - Spring OAuth2/OIDC - OAuth2AuthorizedClientService 未注册用户主体 (authroizedClient)

java - Flying Saucer Batik 和 running footer 不能一起使用?

java - 如何使用 setter 和 getter 函数实现线程安全类,其中使用 setter 的频率远高于 getter

java - 将 PDF 附加到电子邮件