我正在尝试使用 thymeleaf,因为它在所有教程中都有描述,但不知何故我的 HTML 无法加载。
这是我的项目结构:
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/