spring-boot - 如何在 Spring 启动时覆盖默认的 thymeleaf 解析器

标签 spring-boot thymeleaf

嗨,我想将默认的 thymeleaf 配置覆盖到Web应用程序的自定义文件夹。 Thymeleaf依赖项已添加到pom.xml文件。
我想映射到WEB-INF/views/文件夹。

我当前的配置

@Bean
public ThymeleafViewResolver thymeleafViewResolver() {
    ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();
    thymeleafViewResolver.setTemplateEngine(templateEngine());
    thymeleafViewResolver.setOrder(1);
    return thymeleafViewResolver;
}

@Bean
public SpringTemplateEngine templateEngine() {
    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.setTemplateResolver(templateResolver());
    templateEngine.addDialect(new LayoutDialect());
    return templateEngine;
}

@Bean
public ServletContextTemplateResolver templateResolver() {
    ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
    templateResolver.setPrefix("/WEB-INF/views/");
    templateResolver.setSuffix(".html");
    templateResolver.setTemplateMode("HTML5");
    templateResolver.setOrder(1);
    templateResolver.setCacheable(false);
    return templateResolver;
}

当我运行该应用程序时,它显示以下异常。
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration$DefaultTemplateResolverConfiguration': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:136)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:408)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1558)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:762)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:321)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:961)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:950)
    at com.jo.dsdemo.app.MainApp.main(MainApp.java:17)
Caused by: java.lang.IllegalStateException: Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
    at org.springframework.util.Assert.state(Assert.java:385)
    at org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration$DefaultTemplateResolverConfiguration.checkTemplateLocationExists(ThymeleafAutoConfiguration.java:82)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:349)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:300)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:133)
    ... 17 more

最佳答案

我认为最简单的方法应该是将spring.thymeleaf.prefix定义为"/WEB-INF/templates/"。否则,请确保您的Bean具有名称defaultTemplateResolver

未经测试-但希望会有所帮助。请提供反馈,以便其他读者知道它是否有用。

关于spring-boot - 如何在 Spring 启动时覆盖默认的 thymeleaf 解析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32860366/

相关文章:

Java Spring Boot - 使用下拉选择作为变量

java - SpringBoot、Kafka : java. lang.NoSuchMethodError : org. apache.kafka.clients.producer.Producer.close(Ljava/time/Duration;)V

java - Spring-boot thymeleaf 与@pahtvariable 有问题想从数据库中提取一条记录

Java Rest 应用程序在 HTML 中打印结果时出现问题

html - Thymeleaf - 如何有条件地向输入添加选中的属性

spring-mvc - 用于显示列表错误的表单绑定(bind)

java - 使用 Spring Boot 进行 ANSI 日志记录

spring-boot - 在 Spring Boot 中定义 logback 关闭钩子(Hook)

spring-boot - 如何禁用 configserver Health 页面检查默认标签 git

spring - 如何在 thymeleaf 模板中生成 svg