繁体   English   中英

无法在 servlet 3.0 中使用 Rythm 模板引擎

[英]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文件夹下,然后它就会按预期工作。 这是一个简单的项目证明了这一点:

在此处输入图片说明

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM