Could not open ServletContext resource [/templates/show.xsl]

Viewed 6139

I have a spring-boot + thymeleaf application - so im basicly using no xml files for configuration. Classic html templates work fine for me, but i am having troubles with xsl. I followed this tutorial Spring MVC XstlView and XsltViewResolver Example but ended up with java.io.FileNotFoundException: Could not open ServletContext resource [/templates/show.xsl]

This is how my configuration class looks like

@Configuration
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter {

    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
            "classpath:/META-INF/resources/", "classpath:/resources/",
            "classpath:/static/", "classpath:/public/" };

    @Bean
    @Description("Thymeleaf template resolver serving HTML 5")
    public ClassLoaderTemplateResolver htmlTemplateResolver() {
        ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();

        templateResolver.setPrefix("templates/");
        templateResolver.setCacheable(false);
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode("HTML5");
        templateResolver.setCharacterEncoding("UTF-8");

        return templateResolver;
    }

    @Bean
    @Description("Thymeleaf template resolver serving XML")
    public ClassLoaderTemplateResolver xmlTemplateResolver() {
        ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();

        templateResolver.setPrefix("templates/");
        templateResolver.setCacheable(false);
        templateResolver.setSuffix(".xml");
        templateResolver.setTemplateMode("XML");
        templateResolver.setCharacterEncoding("UTF-8");

        return templateResolver;
    }

    @Bean
    @Description("Thymeleaf template engine with Spring integration")
    public SpringTemplateEngine templateEngine() {

        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.addTemplateResolver(htmlTemplateResolver());
        templateEngine.addTemplateResolver(xmlTemplateResolver());

        return templateEngine;
    }

    @Bean
    @Description("Thymeleaf view resolver")
    public ViewResolver viewResolver() {

        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();

        viewResolver.setTemplateEngine(templateEngine());
        viewResolver.setCharacterEncoding("UTF-8");

        return viewResolver;
    }

    @Bean
    public ViewResolver getXSLTViewResolver(){

        XsltViewResolver xsltViewResolver = new XsltViewResolver();
        xsltViewResolver.setOrder(1);
        xsltViewResolver.setSourceKey("xmlSource");

        xsltViewResolver.setViewClass(XsltView.class);
        xsltViewResolver.setViewNames(new String[] {"show"});
        xsltViewResolver.setPrefix("templates/");
        xsltViewResolver.setSuffix(".xsl");

        return xsltViewResolver;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        if (!registry.hasMappingForPattern("/webjars/**")) {
            registry.addResourceHandler("/webjars/**").addResourceLocations(
                    "classpath:/META-INF/resources/webjars/");
        }
        if (!registry.hasMappingForPattern("/**")) {
            registry.addResourceHandler("/**").addResourceLocations(
                    CLASSPATH_RESOURCE_LOCATIONS);
        }
    }
}

From controller i am trying to redirect to my xsl template like this:

    @RequestMapping(value="/form", method=RequestMethod.POST, params="action=show")
public ModelAndView show(@ModelAttribute Team team) throws MarshalException {
    Source source = new StreamSource(new ByteArrayInputStream(xmlService.getXmlStream(team).toByteArray()));
    // adds the XML source file to the model so the XsltView can detect
    ModelAndView model = new ModelAndView("show");
    model.addObject("xmlSource", source);
    return model;
}

This is how my project files look like. enter image description here

I would appreciate any suggestions how to make it work. Thank you

1 Answers

If you are using spring boot, then, no need to mention any configuration setting like, TemplateMode, Prefix, Suffix, etc. Spring boot automatically configures the template as per default setting. Do no mention any configuration in @Configuration class or application.properties file. Try this, it worked for me.

Related