[英]Unable to use Rythm template engine with servlet 3.0
我正在尝试在 tomcat7 上使用带有 servlet 3.0 的 Rythm 模板引擎。
我想将模板从WebContent
目录渲染到Rythm
引擎。 但它没有检测模板。
在 servlet init()
方法中,我将 Rthym 引擎初始化为
public void init(ServletConfig config) throws ServletException {
Map <String, Object> context = new HashMap <String, Object> ();
//String filePath = new File("").getAbsolutePath();
//filePath.concat("WebContent");
context.put("home.template", "WebContent");
Rythm.init(context);
}
然后我尝试在doGet
方法中使用Rythm.render
呈现我的NewFile.html
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Map <String, Object> args = new HashMap <String, Object> ();
args.put("a", "World");
PrintWriter out = response.getWriter();
out.println(Rythm.render("NewFile.html", args));
}
但它在浏览器中只显示“NewFile.html”(不是 NewFile.html 的内容,而只是字符串“NewFile.html”
我在使用 Rhythm 时遇到了类似的问题,在我的情况下,它有助于在文件名前面写入目录:
Rythm.render("templates/" + templateFileName, parameters);
设置home.template
变量对我也不起作用。
节奏用资源管理器加载模板文件。 资源管理器的默认实现将资源加载委托给Thread.currentThread.getContextClassLoader()
,它不能加载webapp
文件夹下的任何资源。 请参阅从 getResource 解析 webapp 的根。
如果你想在webapp
文件夹下加载模板,你需要创建自己的资源管理器并委托给ServletContext
加载模板。
幸运的是,您不需要这样做。 只需将您的模板放在resources
文件夹下,然后它就会按预期工作。 这是一个简单的项目证明了这一点:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.