关于freemarker include文件路径的问题
2013-06-21 10:40
357 查看
在freemarker中可以使用 #include 命令引入其他的文件。
但是今天自己在写代码的时候遇到一个问题,比如我的代码目录结构是这样的:
我在freemarker下面的一个java代码里面写了如下代码:
package freemarker;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class TestInclude {
private Configuration cfg = null;
public Configuration getCfg() {
return cfg;
}
public void init() throws IOException {
cfg = new Configuration();
cfg.setDirectoryForTemplateLoading(new File("bin/freemarker"));
}
public static void main(String args[]) throws IOException, TemplateException {
TestInclude obj = new TestInclude();
obj.init();
Map root = new HashMap();
Template t = obj.getCfg().getTemplate("TestInclude.ftl");
Writer out = new StringWriter();
t.process(root, out);
System.out.println(out.toString());
}
}
然后在TestInclude.ftl 中引入如下:
<html>
<head>
<title>Testpage</title>
</head>
<body>
<h1>Testpage</h1>
<p>Blahblah...
<#include "/free/test.ftl">
</body>
</html>
结果就抛异常了,找不到free/test.ftl
郁闷。于是改成<#include "bin/free/test.ftl"> 喔,还是不行
继续,改成<#include "../bin/free/test.ftl"> ,这样也不行呀?
最后的解决方法如下:
public class TestInclude {
private Configuration cfg = null;
public Configuration getCfg() {
return cfg;
}
public void init() throws IOException {
cfg = new Configuration();
cfg.setDirectoryForTemplateLoading(new File("bin"));
}
public static void main(String args[]) throws IOException, TemplateException {
TestInclude obj = new TestInclude();
obj.init();
Map root = new HashMap();
Template t = obj.getCfg().getTemplate("/freemarker/TestInclude.ftl");
Writer out = new StringWriter();
t.process(root, out);
System.out.println(out.toString());
}
}
然后在TestInclude.ftl中如下写:
<#include "/free/test.ftl">
是不是可以这样理解,就算是include中的路径,和TestInclude。ftl 一样,都是相对于[b] cfg.setDirectoryForTemplateLoading(new File("bin")); 这个目录的吧。。。。[/b]
但是今天自己在写代码的时候遇到一个问题,比如我的代码目录结构是这样的:
我在freemarker下面的一个java代码里面写了如下代码:
package freemarker;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class TestInclude {
private Configuration cfg = null;
public Configuration getCfg() {
return cfg;
}
public void init() throws IOException {
cfg = new Configuration();
cfg.setDirectoryForTemplateLoading(new File("bin/freemarker"));
}
public static void main(String args[]) throws IOException, TemplateException {
TestInclude obj = new TestInclude();
obj.init();
Map root = new HashMap();
Template t = obj.getCfg().getTemplate("TestInclude.ftl");
Writer out = new StringWriter();
t.process(root, out);
System.out.println(out.toString());
}
}
然后在TestInclude.ftl 中引入如下:
<html>
<head>
<title>Testpage</title>
</head>
<body>
<h1>Testpage</h1>
<p>Blahblah...
<#include "/free/test.ftl">
</body>
</html>
结果就抛异常了,找不到free/test.ftl
郁闷。于是改成<#include "bin/free/test.ftl"> 喔,还是不行
继续,改成<#include "../bin/free/test.ftl"> ,这样也不行呀?
最后的解决方法如下:
public class TestInclude {
private Configuration cfg = null;
public Configuration getCfg() {
return cfg;
}
public void init() throws IOException {
cfg = new Configuration();
cfg.setDirectoryForTemplateLoading(new File("bin"));
}
public static void main(String args[]) throws IOException, TemplateException {
TestInclude obj = new TestInclude();
obj.init();
Map root = new HashMap();
Template t = obj.getCfg().getTemplate("/freemarker/TestInclude.ftl");
Writer out = new StringWriter();
t.process(root, out);
System.out.println(out.toString());
}
}
然后在TestInclude.ftl中如下写:
<#include "/free/test.ftl">
是不是可以这样理解,就算是include中的路径,和TestInclude。ftl 一样,都是相对于[b] cfg.setDirectoryForTemplateLoading(new File("bin")); 这个目录的吧。。。。[/b]
相关文章推荐
- 关于freemarker include文件路径的问题
- 关于Java文件路径问题
- 关于web项目web.xml配置文件路径的问题。
- SiteMesh:关于decorators.xml配置文件中路径设置的问题
- 关于jsp include文件时的一个乱码解决的问题
- 关于Java文件路径问题
- 关于exec系列函数的文件路径问题及延伸
- 关于Android Studio中build.gradle文件的gradle路径存在问题
- 关于java读取properties文件的路径问题
- 关于File.separator 文件路径:wind与linux下路径问题 .
- SiteMesh:关于decorators.xml配置文件中路径设置的问题
- PHP的包含(include,require)文件的路径问题
- unity 关于手机客户端的本地文件储存问题 文件路径--适用于安卓手机
- 关于java读取properties文件的路径问题
- 关于java文件路径问题
- Android中关于文件路径的问题
- Linux下gcc编译中关于头文件与库文件搜索路径相关问题
- 关于 X509Certificate2 找到文件路径的问题
- 【Java】关于文件路径问题总结
- 关于Java读取文件操作时路径中的斜杠问题