无法打开ServletContext资源[/templates/show.xsl]

时间:2017-09-28 09:03:21

标签: java spring-mvc xslt spring-boot thymeleaf

我有一个spring-boot + thymeleaf应用程序 - 所以我基本上没有使用xml文件进行配置。经典的html模板对我来说很好,但我遇到了xsl的麻烦。我按照本教程Spring MVC XstlView and XsltViewResolver Example进行了操作,但结果是setcookie('signedCookie','uneditable value here', 0, '/', 'www.example.com', TRUE , TRUE);

这是我的配置类的样子

java.io.FileNotFoundException: Could not open ServletContext resource [/templates/show.xsl]

从控制器我试图重定向到我的xsl模板,如下所示:

@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);
        }
    }
}

这就是我的项目文件的样子。 enter image description here

如果有任何建议,我将不胜感激。谢谢

1 个答案:

答案 0 :(得分:0)

如果您使用的是Spring Boot,则无需提及任何配置设置,例如TemplateMode,Prefix,Suffix等。SpringBoot会根据默认设置自动配置模板。 不要在@Configuration类或application.properties文件中提及任何配置。 试试这个,对我有用。