两个模板在不同的文件夹里面,在其中的一个模板里用<#import "a/a.ftl" as kk>时,总是报错:Error reading imported file a/a.ftl到底是哪里出错了?请指教。

解决方案 »

  1.   

    如果是在同一个包下面,你可以把它改成<#import 'a.ftl' as kk>我考虑你那不仅仅报Error reading imported file a/a.ftl这个错
    可能堆栈里面还有FileNotFound
      

  2.   


    下面是LibraryLoad类的构造函数和accept方法的源码 LibraryLoad(Template template,
                Expression templateName,
                String namespace)
        {
            this.namespace = namespace;
            String templatePath1 = template.getName();
            int lastSlash = templatePath1.lastIndexOf('/');
            templatePath = lastSlash == -1 ? "" : templatePath1.substring(0, lastSlash + 1);
            this.templateName = templateName;
     } void accept(Environment env) throws TemplateException, IOException {
            String templateNameString = templateName.getStringValue(env);
            if( templateNameString == null ) {
                String msg = "Error " + getStartLocation()
                            + "The expression " + templateName + " is undefined.";
                throw new InvalidReferenceException(msg, env);
            }
            Template importedTemplate;
            try {
                if(!env.isClassicCompatible()) {
                    if (templateNameString.indexOf("://") >0) {
                        ;
                    }
                    else if(templateNameString.length() > 0 && templateNameString.charAt(0) == '/')  {
                        int protIndex = templatePath.indexOf("://");
                        if (protIndex >0) {
                            templateNameString = templatePath.substring(0, protIndex + 2) + templateNameString;
                        } else {
                            templateNameString = templateNameString.substring(1);
                        }
                    }
                    else {
                        templateNameString = templatePath + templateNameString;
                    }
                }
                importedTemplate = env.getTemplateForImporting(templateNameString);
            }
            catch (ParseException pe) {
                String msg = "Error parsing imported template "
                            + templateNameString;
                throw new TemplateException(msg, pe, env);
            }
            catch (IOException ioe) {
                String msg = "Error reading imported file "
                            + templateNameString;
                throw new TemplateException(msg, ioe, env);
            }
            env.importLib(importedTemplate, namespace);
        }关于模板的加载一定要看几句代码:    LibraryLoad(Template template,
                Expression templateName,
                String namespace)
        {
            //获取<#import>所在模板的名称
            String templatePath1 = template.getName();
            int lastSlash = templatePath1.lastIndexOf('/');
            //获取<#import>所在模板的名称的路径
            templatePath = lastSlash == -1 ? "" : templatePath1.substring(0, lastSlash + 1);
            //<#import >中的表达式,也就是另外一个模板的路径
            this.templateName = templateName;
        }然后在看accept中的代码,主要是几个if条件的判断,它们分别代表集中不同了路径的加载方式:
    1、 if (templateNameString.indexOf("://") >0) {
                        ;
                    }
        这一种是绝对路径的书写格式,不做任何处理,后面直接执行     
        env.getTemplateForImporting(templateNameString);
    2、if(templateNameString.length() > 0 && templateNameString.charAt(0) == '/')  {
                        int protIndex = templatePath.indexOf("://");
                        if (protIndex >0) {
                            templateNameString = templatePath.substring(0, protIndex + 2) + templateNameString;
                        } else {
                            templateNameString = templateNameString.substring(1);
                        }
                    }
       这一种是表达式以"/"开头的情况,表示是加载在该模板下的文件夹中的模板
       如:a.ftl中:<#import "/c/b.ftl" as kk>b.ftl和a.ftl是同级的,都在c文件夹下3、最后一个else表示通模板在该模板的下一级文件中:
       templateNameString = templatePath + templateNameString;
      如:a.ftl中:<#import "c/b.ftl" as kk>表示c文件夹和a.ftl同级,b.ftl在c文件夹中
                   
      

  3.   

    路径,应该是 <#import "/a/a.ftl" as kk>