关于freemarker import 错误的问题 两个模板在不同的文件夹里面,在其中的一个模板里用<#import "a/a.ftl" as kk>时,总是报错:Error reading imported file a/a.ftl到底是哪里出错了?请指教。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 如果是在同一个包下面,你可以把它改成<#import 'a.ftl' as kk>我考虑你那不仅仅报Error reading imported file a/a.ftl这个错可能堆栈里面还有FileNotFound 下面是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文件夹中 路径,应该是 <#import "/a/a.ftl" as kk> AOP切不了 解析一个xml文件的时候怎样自动打印其中的几行内容 如何制作印章 如何发送图片 struts2显示列表 为什么数据库连接断了之后重新启动才行? (100分求解,解决了再送100分) 我想写J2EE与分布式计算方面的论文,望高手给点意见? resin2.1不能用jstl??? java里如何表示16进制? 设计EJB的方法,请各位老大赐教 有人了解 wooki 的吗 ? 关于酒店分房的算法
可能堆栈里面还有FileNotFound
下面是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文件夹中