Spring XsltViewResolver前缀路径

时间:2019-01-13 15:33:00

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

我想在SpringBoot应用程序中使用XSLT作为我的视图技术。 我正在使用XsltViewResolver创建bean,但不确定如何为此ViewResolver设置前缀。我在网上看到了将xsl文件放在WEB-INF/xsl中的示例,如下所示:

  @Bean
  public ViewResolver xsltViewResolver() {
      XsltViewResolver viewResolver = new XsltViewResolver();
      viewResolver.setPrefix("/WEB-INF/xsl/");
      viewResolver.setSuffix(".xslt");
      return viewResolver;
  }

但是由于我使用JAR包装,没有WEB-INF文件夹,所以将它们放在src/main/resources/templates中,并且我的前缀看起来像这样:

    viewResolver.setPrefix("/resources/templates/");

它不起作用,当我的控制器被调用时,我有FileNotFound异常:

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

如何设置正确的前缀?

1 个答案:

答案 0 :(得分:0)

您的视图解析器bean应该是

 @Bean
      public ViewResolver xsltViewResolver() {
          XsltViewResolver viewResolver = new XsltViewResolver();
          viewResolver.setPrefix("classpath:/templates/");
          viewResolver.setSuffix(".xslt");
          return viewResolver;
      }