Skip to content

How to resolve org.thymeleaf.exceptions.TemplateInputException: Error resolving template, template might not exist or might not be accessible by any of the configured Template Resolvers when using spring framework with thymeleaf

Problem

When running a Spring Boot or Spring MVC application with Thymeleaf, you might encounter the following error when visiting a URL:

Terminal window
curl http://127.0.0.1:8080/hello2

The error message is:

Terminal window
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Wed Feb 24 11:10:36 CST 2021
There was an unexpected error (type=Internal Server Error, status=500).
Error resolving template [hello2 bswen], template might not exist or might not be accessible by any of the configured Template Resolvers
org.thymeleaf.exceptions.TemplateInputException: Error resolving template [hello2 bswen], template might not exist or might not be accessible by any of the configured Template Resolvers

The core exception is:

Terminal window
There was an unexpected error (type=Internal Server Error, status=500).
Error resolving template [hello2 bswen], template might not exist or might not be accessible by any of the configured Template Resolvers
org.thymeleaf.exceptions.TemplateInputException: Error resolving template [hello2 bswen], template might not exist or might not be accessible by any of the configured Template Resolvers

Spring framework is complaining that it cannot find the view resolver for the specified URL.

Environment

  • Spring Boot 1.x and 2.x

The Codes and Configurations

The project layout is as follows:

Terminal window
.
└── app/
├── build.gradle
└── src/
└── main/
├── java/
└── com.bswen.app9/
├── MvcConfig
├── HelloController
└── Main
└── resources/
├── templates/
└── hello.html
└── application.properties

The build.gradle file includes the necessary dependencies:

build.gradle
dependencies {
...
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
....
}

The HelloController defines a method to handle HTTP GET requests for the /hello2 URL:

HelloController.java
@Controller
public class HelloController {
@GetMapping("/hello2")
public String hello2(Authentication authentication) {
return String.format("hello2 %s", authentication.getName());
}
}

There is also a configuration class to configure Spring MVC view controllers:

MvcConfig.java
package com.bswen.app9.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MvcConfig implements WebMvcConfigurer {
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/home").setViewName("home");
registry.addViewController("/").setViewName("home");
registry.addViewController("/hello").setViewName("hello");
registry.addViewController("/login").setViewName("login");
}
}

However, running the application results in the following exception:

Terminal window
There was an unexpected error (type=Internal Server Error, status=500).
Error resolving template [hello2 bswen], template might not exist or might not be accessible by any of the configured Template Resolvers
org.thymeleaf.exceptions.TemplateInputException: Error resolving template [hello2 bswen], template might not exist or might not be accessible by any of the configured Template Resolvers

Solution

Step 1: Remove the WebMvcConfigurer Subclass

Since Spring MVC has a default view resolver for Thymeleaf, you do not need to configure the views explicitly. Remove the MvcConfig class. The directory structure now looks like this:

Terminal window
.
└── app/
├── build.gradle
└── src/
└── main/
├── java/
└── com.bswen.app9/
├── HelloController
└── Main
└── resources/
├── templates/
└── hello.html
└── application.properties

Step 2: Modify the Controller

Update the HelloController to include the @ResponseBody annotation:

HelloController.java
@Controller
public class HelloController {
@GetMapping("/hello2")
public @ResponseBody String hello2(Authentication authentication) {
return String.format("hello2 %s", authentication.getName());
}
}

The @ResponseBody annotation indicates that the method returns a plain string and should not be resolved as a view template.

After making these changes, rerun the application. The error should disappear:

Terminal window
bswen-springboot23 git:(main) curl http://127.0.0.1:8080/hello2
hello2 bswen%

It works!

About the View Resolver in Spring MVC

Spring relies on the view resolver to resolve the view name from the controller. Thymeleaf is a Java library that acts as a template engine for XML/XHTML/HTML5. It applies transformations to template files to display data or text produced by your applications.

If you want to use Thymeleaf, simply add the dependency to your project.

All the example code and configuration files can be found in this GitHub project.

Summary

In this post, we explored how to resolve the TemplateInputException in Spring Boot applications using Thymeleaf. The key steps include removing unnecessary view configurations and using the @ResponseBody annotation to indicate that a method returns a plain string. By following these steps, you can avoid common pitfalls related to view resolution in Spring MVC.

Final Words + More Resources

My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!