我试图在tomcat7上使用Servlet3.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);
}然后,我尝试在NewFile.html方法中使用Rythm.render呈现我的doGet
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”)。
发布于 2014-10-21 19:54:46
Rythm也有类似的问题,在我的例子中,它有助于在文件名前面编写目录:
Rythm.render("templates/" + templateFileName, parameters);设置home.template变量对我也不起作用。
发布于 2015-04-03 09:26:16
使用资源管理器加载模板文件。资源管理器的默认实现将资源加载委托给Thread.currentThread.getContextClassLoader(),后者无法在webapp文件夹下加载任何资源。见Resolving the root of a webapp from getResource。
如果要在webapp文件夹下加载模板,则需要创建自己的资源管理器并委托ServletContext加载模板。
幸运的是你不需要那样做。只要把你的模板放在resources文件夹下,它就会像预期的那样工作。这里有一个简单的项目证明了这一点:

项目源代码可在https://github.com/greenlaw110/rythm-gh-issue-241上找到。
https://stackoverflow.com/questions/26494075
复制相似问题