2

我正在尝试在 tomcat7 上使用带有 servlet 3.0 的 Rythm 模板引擎。
我想将模板从WebContent目录渲染到Rythm引擎。但它没有检测到模板。

在 servletinit()方法中,我将 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的 with Rythm.renderindoGet方法渲染为

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”

4

2 回答 2

2

我对 Rythm 有类似的问题,在我的情况下,它有助于在文件名前面写入目录:

Rythm.render("templates/" + templateFileName, parameters);

设置home.template变量对我也不起作用。

于 2014-10-21T19:54:46.323 回答
0

Rythm 使用资源管理器加载模板文件。资源管理器的默认实现将资源加载委托给Thread.currentThread.getContextClassLoader(),它不能加载webapp文件夹下的任何资源。请参阅从 getResource 解析 webapp 的根目录

如果要加载webapp文件夹下的模板,则需要创建自己的资源管理器并委托ServletContext给加载模板。

幸运的是,您不需要这样做。只需将您的模板放在resources文件夹下,然后它就会按预期工作。这是一个简单的项目证明了这一点:

在此处输入图像描述

项目源代码可以在https://github.com/greenlaw110/rythm-gh-issue-241找到

于 2015-04-03T09:26:16.850 回答