template might not exist or might not be accessible by any of the configured Template Resolvers

The spring boot error template might not exist or might not be accessible by any of the configured Template Resolvers occurs when the template files are not available in src/main/resources/templates folder or the file name is incorrect. The exception org.thymeleaf.exceptions.TemplateInputException: Error resolving template is thrown if the template file can not be found in the template folder..

When the spring boot application is configured as a view resolver, the HTML files in the template folder will be used to resolve them. If the thymeleaf can not locate the html file in the template folder, the error will be thrown. The reason may be either that the file is not available in the folder, or that the file name is incorrect.

In Spring boot, template files are added in src/main/resources/templates folder. If a file is not available in this folder and configured in controller class, this exception template might not exist or might not be accessible by any of the configured Template Resolvers will be thrown as the resolver could not identify the file



Exception

2020-07-24 07:17:35.163 ERROR 13535 --- [nio-8080-exec-2] org.thymeleaf.TemplateEngine             : [THYMELEAF][http-nio-8080-exec-2] Exception processing template "helloworld": Error resolving template [helloworld], template might not exist or might not be accessible by any of the configured Template Resolvers

org.thymeleaf.exceptions.TemplateInputException: Error resolving template [helloworld], template might not exist or might not be accessible by any of the configured Template Resolvers
	at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:869) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
	at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:607) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
	at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098) [thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
	at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072) [thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
	at org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:362) [thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]


How to reproduce this issue

If a html file is not available in templates folder, when a controller requires this html file, this exception will be thrown. Add thymeleaf dependency in pom.xml

pom.xml

<dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency> 

HelloWorldController.java

package com.yawintutor;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloWorldController {

	@RequestMapping("/HelloWorld")
	public ModelAndView firstPage() {
		return new ModelAndView("helloworld");
	}

}

Open a browser and call this url http://localhost:8080/HelloWorld



Root Cause

When a url is invoked, controller searches the html file in the template folder. If the file is not available then this exception will be thrown. This could be either due to wrong view name in controller or the file is not available in template folder.



Solution 1

Check the calling controller method and verify the view name is correct or not. Change the correct view name in the controller method.

@RequestMapping("/HelloWorld")


Solution 2

Check the html file is available in src/main/resources/templates folder. If the file is not available, add a new file. If the file exists in the folder, check the spelling mistake of the file name.



Solution 3

The template location can be configured in application.properties. Check the default folder location is customized to other folder in application.properties.

application.properties

spring.thymeleaf.prefix=classpath:/mytemplate/
spring.thymeleaf.suffix=.html



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *