Spring Thymeleaf TemplateResolver 错误

Spring Thymeleaf TemplateResolver error

我正在尝试在 spring 中使用 thymeleaf 对文本字符串进行本地化。我的 html 模板位于 /src/main/resources/templates/

所以我有:

@Configuration
@EnableWebMvc
public class MvcConfig extends WebMvcConfigurerAdapter {

    public MvcConfig() {
        super();
    }

    @Override
    public void addViewControllers(final ViewControllerRegistry registry)             {
        super.addViewControllers(registry);
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/sorting").setViewName("sorting");
    }

    @Override
    public void configureDefaultServletHandling(final DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Override
    public void addResourceHandlers(final ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/", "/resources/","/resource/*");
        registry.addResourceHandler("/assets/**")
    .addResourceLocations("classpath:/assets/");
        registry.addResourceHandler("/css/**")
    .addResourceLocations("/css/");
    }


    @Bean
    public ServletContextTemplateResolver templateResolver() {
        ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
        templateResolver.setCacheable(false);
        templateResolver.setPrefix("classpath:/templates/");
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode("HTML5");
        return templateResolver;
    }

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

    @Bean
    public ViewResolver viewResolver() {
        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setTemplateEngine(templateEngine());
        viewResolver.setCharacterEncoding("UTF-8");
        viewResolver.setOrder(1);
        return viewResolver;
    }
}

但是模板解析器找不到我的模板,我收到一条错误消息:

Error resolving template "index", template might not exist or might not be accessible by any of the configured Template Resolvers

我怀疑这是由于以下行中的路径不正确造成的:templateResolver.setPrefix("classpath:/templates/"); 但我无法找到应该去那里的前缀。

似乎大多数示例都将模板放在WEB-INF 下。这也是我应该遵循的推荐方式吗?

您需要使用ClassLoaderTemplateResolver。您的 templateResolver() 方法应该如下所示。

@Bean
public TemplateResolver templateResolver() {
        TemplateResolver templateResolver = new ClassLoaderTemplateResolver();
        templateResolver.setPrefix("templates/");
        templateResolver.setCacheable(false);
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode("HTML5");
        return templateResolver;
    }

不需要将模板放在WEB-INF/templates/

If your templates are under resources/templates/ folder  do this
         templateResolver.setPrefix("/templates/")

If your templates inside WEB-INF/templates/ it should be like
         templateResolver.setPrefix("/WEB-INF/templates/")