使用junit运行freemarker找不到模板

我们是jsp项目,使用spring装配freemarker,在跑项目的时候正常,但是在跑testcase的时候报找不到模板

[color=brown]配置文件如下[/color]

<!-- 配置模板加载路径 -->
<!-- 设置FreeMarker环境属性-->

1 <!-- 刷新模板的周期,单位为秒 -->
<!--true--> <!-- 此属性可以防止模板解析空值时的错误 -->
UTF-8<!--模板的编码格式 -->

zh_CN<!-- 本地化设置-->

UTF-8
yyyy-MM-dd HH:mm:ss
yyyy-MM-dd
HH:mm:ss

0.######

true,false

<!--"/spring.ftl" as spring

true

auto_detect -->




项目是使用myeclipse向导创建的Web项目
[color=brown]配置文件的路径为[/color] /WebRoot/WEB-INF/templates/page/index.ftl
获得template的java代码如下
Template t = freemarkerConfig.getConfiguration().getTemplate("[color=red]/WEB-INF/templates/page/index.ftl[/color]");
顺便说一下,我还尝试过
Template t = freemarkerConfig.getConfiguration().getTemplate("[color=red]/WebRoot/WEB-INF/templates/page/index.ftl[/color]");
Template t = freemarkerConfig.getConfiguration().getTemplate("[color=red]/templates/page/index.ftl[/color]");
Template t = freemarkerConfig.getConfiguration().getTemplate("[color=red]D:/myproject/WebRoot/WEB-INF/templates/page/index.ftl[/color]");

再顺便说一下,我已经诅咒过spring得freemarker插件一万遍了,也没有啥用。

任何帮助或建议都不胜感激。

[quote]
是的,使用clapass是ok的,但是这里有一个情况,目录结构已经定了。配置文件时放在WEB-INF下的。这个我是没有权利改变的。有什么变通的方法么?
[/quote]
没让你改变目录结构啊……
你不就只是用JUNIT跑个testcase么?
直接在src/test/resources下建个templates目录,把/WEB-INF/templates下的文件啊文件夹什么的都拷贝一份过去不就结了?

从工程角度讲,testcase推荐使用test的resources,使用另一个专门配置测试环境的spring配置文件,而非使用主目录里的spring配置。
在spring配置文件的路径推荐使用classpath,就是这样写:
[code="xml"]


...

[/code]
这样在java代码里的相对路径就是classpath下的templates目录了(一般放在resources里)。
之后只要用相对路径获取template就行了。

[url=http://jinnianshilongnian.iteye.com/blog/1878877]maven 多module时测试springmvc+freemarker的问题总结 [/url]