简体   繁体   中英

How to configure Thymeleaf template location

I want to use Thymeleaf in a Spring Boot 2.1.5 based application. But I do not need it to create html output for a RestController . Instead of that I want it to create html files that the application can store on disk.

Because of this I create two beans templateResolver and templateEngine like the docs describe. The templates are stored in the same location I would use for the RestController : src/main/resources/templates . One for example is called index.html .

But no matter which path I configure (with or without classpath ) I get the same error message:

    templateResolver.setApplicationContext(this.applicationContext);
    templateResolver.setPrefix("classpath:/resources/templates/");
    templateResolver.setSuffix(".html");

java.io.FileNotFoundException: class path resource [resources/templates/index.html] cannot be opened because it does not exist

How do I need to configure the path to make it work a) inside STS and b) inside the created jar ?

An alternative to solving this problem would be using spring-boot-starter-thymeleaf instead and "grabbing" its generated output instead of exposing it via the embedded Tomcat but I do not know how to get this to work:

@GetMapping("/noneedforthis")
public String getIndexFileContent(@RequestParam(name="name", required=false, defaultValue="World") String name, Model model) {
    model.addAttribute("name", name);
    return "index";
}

I checked both the local target directory of my Eclipse workspace and the jar created by running maven install: instead of resources/templates the templates are stored in templates .

Shortening the path in my code helped and now Thymeleaf can find the templates.

Using the Spring Boot Starter for Thymeleaf would still be an interesting alternative but so far I have not found an approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM