Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot gives "TemplateInputException: Error resolving template" when running from jar

I have an app that works perfectly when started within IntelliJ or via gradle bootRun.

however, if I do gradle bootRepackage and then try and run the resulting jar, I end up with:

2014-12-02 21:46:14.086 ERROR 9839 --- [nio-2014-exec-2] org.thymeleaf.TemplateEngine             : [THYMELEAF][http-nio-2014-exec-2] Exception processing template "/login": Error resolving template "/login", template might not exist or might not be accessible by any of the configured Template Resolvers
2014-12-02 21:46:14.087 ERROR 9839 --- [nio-2014-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template "/login", template might not exist or might not be accessible by any of the configured Template Resolvers] with root cause

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "/login", template might not exist or might not be accessible by any of the configured Template Resolvers
    at org.thymeleaf.TemplateRepository.getTemplate(TemplateRepository.java:245)
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1104)
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1060)
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1011)
    at org.thymeleaf.spring4.view.ThymeleafView.renderFragment(ThymeleafView.java:335)

I can see that the jar has /templates/** contained in it. the content looks OK to me.

One possible(?) factor may be that I use an html page referring to a layout, thus:

  layout:decorator="layouts/main"

I can confirm that the file IS in the jar.

/login is defined thusly:

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/login").setViewName("/login");
    registry.addViewController("/").setViewName("/login");
}

and I have spring security configured as:

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Override
public void configure(WebSecurity security) {
    security.ignoring().antMatchers("/assets/**");
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .csrf().disable();
    http
            .authorizeRequests()
            .antMatchers("/").permitAll()
            .anyRequest().authenticated();
    http
            .formLogin()
            .loginPage("/login")
            .defaultSuccessUrl("/home")
            .failureUrl("/login?error")
            .permitAll()
            .and()
            .logout()
            .invalidateHttpSession(true)
            .logoutSuccessUrl("/login?logout")
            .permitAll();
}
}

I think that's all that might be relevant to this issue...

I have seen https://github.com/spring-projects/spring-boot/issues/112 and Proper location of Thymeleaf views for Spring (amongst others). These resources notwithstanding, I have not been successful at getting template resolution working.

Any suggestions gratefully recieved.

To have come so far with Spring Boot yet to have stumbled at the last hurdle (near-final deployment) is vexatious.

like image 619
Bob Brown Avatar asked Dec 02 '14 12:12

Bob Brown


4 Answers

I had same problem using spring boot with thymeleaf by default configuration. All worked until I have to try .jar

message says: ... org.thymeleaf.exceptions.TemplateInputException: Error resolving template "/fragments/footer"

I solved playing with / , problem was that they don't need the slash.

I Changed this:

<footer class="footers" th:replace="/fragments/footer :: footer">

for this:

<footer class="footers" th:replace="fragments/footer :: footer">
like image 102
user2822874 Avatar answered Oct 19 '22 21:10

user2822874


The answer seems to be here: https://github.com/spring-projects/spring-boot/issues/1744

In a nutshell: if a resouce path contains '//', things go awry.

The fix is to modify application.yml thusly:

spring:
  thymeleaf:
    prefix: classpath:/templates

(the fix is similar for a .properties file, of course)

The knock-on effect is that all references to templates or whatever need to be preceeded by a slash, as in (Thymeleaf .html file):

      layout:decorator="/layouts/main"

or (Groovy controller):

@RequestMapping(value = "/home", method = RequestMethod.GET)
def home(Model model, Principal principal) {

    "/home/home"
}

I think that spring boot should fix this. Urgently. This is REALLY bad ergonomics that blows up badly, just at the point where one feels like one is nearing final deployment.

like image 38
Bob Brown Avatar answered Oct 19 '22 21:10

Bob Brown


Okay, I found where you have to take a close look. In your Controller classes, when you use @RequestMapping on class, no leading slash (e.g. @RequestMapping("property")). On Method you need a leading slash (e.g. @GetMapping("/list")) and the return value no leading slash (e.g. return "property/list";)

An Example Snippet

@Controller
@RequestMapping("property")
public class PropertyController {
    @NonNull PropertyService service;

    @GetMapping("/list")
    public String list() {
        return "property/list";
    }
}
like image 6
Michael Hegner Avatar answered Oct 19 '22 21:10

Michael Hegner


I faced the issue when using the spring-boot-starter-thymeleaf dependency

So I have used the below dependency instead,

        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf</artifactId>
            <version>3.0.9.RELEASE</version>
        </dependency>
like image 2
firstpostcommenter Avatar answered Oct 19 '22 20:10

firstpostcommenter