从jar运行时,Spring Boot提供"TemplateInputException:Error resolving template"

Bob*_*own 10 spring thymeleaf spring-boot

我有一个在IntelliJ中启动或通过gradle bootRun完美运行的应用程序.

但是,如果我做gradle bootRepackage然后尝试运行生成的jar,我最终得到:

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)
Run Code Online (Sandbox Code Playgroud)

我可以看到jar中包含/ templates/**.内容看起来不错.

一个可能的(?)因素可能是我使用引用布局的html页面,因此:

  layout:decorator="layouts/main"
Run Code Online (Sandbox Code Playgroud)

我可以确认文件是否在jar中.

/ login是这样定义的:

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/login").setViewName("/login");
    registry.addViewController("/").setViewName("/login");
}
Run Code Online (Sandbox Code Playgroud)

我将spring security配置为:

@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();
}
}
Run Code Online (Sandbox Code Playgroud)

我认为这可能与这个问题有关......

我见过https://github.com/spring-projects/spring-boot/issues/112Thymeleaf对Spring的正确位置(以及其他).尽管有这些资源,我还没有成功地使模板解析工作.

任何建议都感激不尽.

到目前为止,使用Spring Boot还没遇到最后一道障碍(近终点部署)是无理取闹的.

小智 7

在默认配置下,使用百里香春季启动时遇到了同样的问题。所有的工作,直到我必须尝试.jar

消息说:... org.thymeleaf.exceptions.TemplateInputException:解决模板“ / fragments / footer”时出错

我解决了使用/的问题,问题是他们不需要斜线。

我改变了这个:

<footer class="footers" th:replace="/fragments/footer :: footer">
Run Code Online (Sandbox Code Playgroud)

为了这:

<footer class="footers" th:replace="fragments/footer :: footer">
Run Code Online (Sandbox Code Playgroud)

  • 我遇到相同的错误,由于您的回答而能够解决它,所以谢谢。 (2认同)

Bob*_*own 5

答案似乎在这里:https : //github.com/spring-projects/spring-boot/issues/1744

简而言之:如果资源路径包含“ //”,那么事情就会出错。

解决方法是这样修改application.yml:

spring:
  thymeleaf:
    prefix: classpath:/templates
Run Code Online (Sandbox Code Playgroud)

(当然,.properties文件的修复方法与此类似)

连锁效应是所有对模板的引用或任何需要以斜杠开头的内容,例如(Thymeleaf .html文件):

      layout:decorator="/layouts/main"
Run Code Online (Sandbox Code Playgroud)

或(Groovy控制器):

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

    "/home/home"
}
Run Code Online (Sandbox Code Playgroud)

认为春季靴应该解决此问题。紧急地。这确实是很糟糕的人机工程学原理,甚至在人们感觉即将完成最终部署时就严重爆炸了。

  • 感谢您发布此信息。最终对我有用的是将尾部斜杠添加到我的属性中。`spring.thymeleaf.prefix=classpath:/templates/` (2认同)
  • 不幸的是,这没有为我解决,有完全相同的问题。Eclipse工作时,可执行的jar无法解析模板... :-( (2认同)

Mic*_*ner 5

好的,我发现您必须仔细看一下。在Controller类中,当您@RequestMapping在类上使用时,请不要使用前导斜线(例如@RequestMapping("property"))。在Method上,您需要一个斜杠(例如@GetMapping("/list")),返回值没有一个斜杠(例如return "property/list";

示例片段

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

    @GetMapping("/list")
    public String list() {
        return "property/list";
    }
}
Run Code Online (Sandbox Code Playgroud)