2

I am trying to use thymeleaf as it is described in all tutorials but somehow my HTML doesn't get loaded.

Here is my project structure:

structure And these are dependencies:

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'
}

It does nothing much but prints out "Hello" message, however, the HTML from resources folder is not used. What am I missing?

The HelloController.java has only 1 method:

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

And main method is just the usual run.

L_J
  • 2,351
  • 10
  • 23
  • 28
Marty
  • 117
  • 9

3 Answers3

1

model.addAttribute makes it possible to get the data in your html file. The return in your method should return the name of the template you want. For example your hello.html

In your hello.html place something like this:

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

Then it should work.

Your controller look something like this, so the return contains your template name hello from 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";
}
Kimberley
  • 359
  • 1
  • 6
  • I actually have that line in html but it still doesn't work. I changed return of my method to just return "hello" and still nothing. On the web page i just see "hello" string. – Marty Aug 01 '18 at 12:28
  • @marti6addams Have you tried: `@RequestMapping(value="/hello", method= RequestMethod.GET)` or `@GetMapping("/hello")` – Kimberley Aug 01 '18 at 12:47
0

You might be using incorrect annotation for controller.

Use

@Controller

Here is example:

@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";
 }
}
VK321
  • 5,768
  • 3
  • 44
  • 47
  • hmmm, i tried it and got exception: 2018-08-01 16:11:30.528 ERROR 10908 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Circular view path [hello]: would dispatch back to the current handler URL [/hello] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)] with root cause – Marty Aug 01 '18 at 14:13
  • I renamed my html but now nothing is displayed at all – Marty Aug 01 '18 at 14:23
  • Please be aware that if you're using other's content, you *must* include proper attribution. – Rob Sep 18 '18 at 09:25
0

You have to change your dependencies, instead of org.thymeleaf you need the following dependency:

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

I hope this resolves your issue.

Source

Sven Hakvoort
  • 3,543
  • 2
  • 17
  • 34