Spring Thymeleaf TemplateResolver错误

时间:2016-10-20 21:17:34

标签: java spring spring-mvc thymeleaf template-engine

我试图在春天使用百里香叶来定位文本字符串。我的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;
    }
}

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

  

解析模板错误"索引",模板可能不存在或任何已配置的模板解析器可能无法访问

我怀疑这是因为该行中的路径不正确:templateResolver.setPrefix("classpath:/templates/");但我无法找到应该去哪个前缀。

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

2 个答案:

答案 0 :(得分:9)

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

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

答案 1 :(得分:0)

没有必要在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/")