为了账号安全,请及时绑定邮箱和手机立即绑定

尝试百里香没有成功

尝试百里香没有成功

Smart猫小萌 2021-06-15 09:52:03
我正在尝试使用 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 方法只是通常的运行。
查看完整描述

3 回答

?
波斯汪

TA贡献1811条经验 获得超4个赞

model.addAttribute 可以在您的 html 文件中获取数据。您方法中的 return 应该返回您想要的模板的名称。例如你的 hello.html


在你的 hello.html 中放置如下内容:


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

那么它应该工作。


您的控制器看起来像这样,因此返回包含来自 hello.html 的模板名称 hello:


@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";

}


查看完整回答
反对 回复 2021-06-23
?
喵喔喔

TA贡献1735条经验 获得超5个赞

您可能对控制器使用了不正确的注释。



@控制器


这是示例:


@Controller

public class MyController {


@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";

 }

}


查看完整回答
反对 回复 2021-06-23
?
莫回无

TA贡献1865条经验 获得超7个赞

您必须更改依赖项,而不是org.thymeleaf需要以下依赖项:

compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf', version: '1.5.1.RELEASE'

我希望这能解决您的问题。


查看完整回答
反对 回复 2021-06-23
  • 3 回答
  • 0 关注
  • 137 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信