简体   繁体   English

使用 FreeMarker 的相对路径未找到文件

[英]File not found using relative path with FreeMarker

I have the following folder structure我有以下文件夹结构

MyApp
  --WEB-INF
      --resources
        --partialFooter.jsp
        --myPartials
          --menu.jsp
      --views
        --index.jsp

and my index.jsp file和我的 index.jsp 文件

<#include "../resources/partialFooter.jsp"/>
<#include "../resources/myPartials/menu.jsp"/>

but this retruns file not found, in the both lines.但是在这两行中都找不到这个重新运行的文件。 Am I putting the right path, I also try with the absolute path but it was the same file not found error.我是否放置了正确的路径,我也尝试使用绝对路径,但它是相同的文件未找到错误。

This is how I created my freemarker view resolvers这就是我创建 freemarker 视图解析器的方式

@Configuration
@EnableWebMvc
@ComponentScan({"configuracion", "controladores", "seguridad"})
public class ConfigMVC extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/resources/");

    }



@Bean
public FreeMarkerConfigurer freemarkerConfig() {
    FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
    freeMarkerConfigurer.setTemplateLoaderPath("/WEB-INF/views/");
    return freeMarkerConfigurer;
}

@Bean
public FreeMarkerViewResolver viewResolver() {

    FreeMarkerViewResolver viewResolver = new FreeMarkerViewResolver();
    viewResolver.setPrefix("");
    viewResolver.setSuffix(".jsp");
    viewResolver.setCache(false);  

    return viewResolver;
}
}

You can't back out of the /WEB-INF/views/ directory, because with setTemplateLoaderPath you set it as the root template directory.您不能退出/WEB-INF/views/目录,因为使用setTemplateLoaderPath将其设置为根模板目录。 So instead you had to use /WEB-INF/ , and then when you refer to the view templates, you have to start their names with "/views/" ( /views/index.jsp ).因此,您必须使用/WEB-INF/ ,然后当您引用视图模板时,您必须以"/views/"/views/index.jsp/views/index.jsp (Though the more common way is to put the included files into the " lib " subdirectory of the view templates, and then you can use the directory of the view templates as the root.) (虽然更常见的方法是将包含的文件放在视图模板的“ lib ”子目录中,然后您可以将视图模板的目录用作根目录。)

BTW, you are probably using an outdated FreeMarker version, because for a while it explains this in the error message.顺便说一句,您可能使用的是过时的 FreeMarker 版本,因为有一段时间它在错误消息中解释了这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM