尝试百里香没有成功

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

这是我的项目结构:

http://img.mukewang.com/60d292220001093b03480416.jpg

这些是依赖项:


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 方法只是通常的运行。


Smart猫小萌
浏览 138回答 3
3回答

波斯汪

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

喵喔喔

您可能对控制器使用了不正确的注释。用@控制器这是示例:@Controllerpublic class MyController {@RequestMapping(value="/hello", method= RequestMethod.GET)public String hello(Model model, @RequestParam(value="name", required=false, defaultValue="World") String name) {&nbsp; model.addAttribute("name", name);&nbsp; return "hello";&nbsp;}}

莫回无

您必须更改依赖项,而不是org.thymeleaf需要以下依赖项:compile&nbsp;group:&nbsp;'org.springframework.boot',&nbsp;name:&nbsp;'spring-boot-starter-thymeleaf',&nbsp;version:&nbsp;'1.5.1.RELEASE'我希望这能解决您的问题。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java