简体   繁体   English

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

[英]Unable to use Rythm template engine with servlet 3.0

I am trying to use Rythm templating engine with servlet 3.0 on tomcat7.我正在尝试在 tomcat7 上使用带有 servlet 3.0 的 Rythm 模板引擎。
I want to render template from WebContent directory to Rythm engine.我想将模板从WebContent目录渲染到Rythm引擎。 But it is not detecting the template.但它没有检测模板。

In servlet init() method I initialized Rthym engine as在 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);
    }

then I tried to render my NewFile.html with Rythm.render in doGet method as然后我尝试在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));
    }

But it is showing just "NewFile.html" in browser (Not content of NewFile.html but only String "NewFile.html"但它在浏览器中只显示“NewFile.html”(不是 NewFile.html 的内容,而只是字符串“NewFile.html”

I had a similar issue with Rythm and in my case it helped to write the directory in front of the filename:我在使用 Rhythm 时遇到了类似的问题,在我的情况下,它有助于在文件名前面写入目录:

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

Setting the home.template variable didn't work for me too.设置home.template变量对我也不起作用。

Rythm loads template files with resource manager.节奏用资源管理器加载模板文件。 The default implementation of resource manager delegates the resource loading to Thread.currentThread.getContextClassLoader() , which can not load any resources under webapp folder.资源管理器的默认实现将资源加载委托给Thread.currentThread.getContextClassLoader() ,它不能加载webapp文件夹下的任何资源。 See Resolving the root of a webapp from getResource .请参阅从 getResource 解析 webapp 的根

If you want to load templates under webapp folder, you need to create your own resource manager and delegate to ServletContext to load template.如果你想在webapp文件夹下加载模板,你需要创建自己的资源管理器并委托给ServletContext加载模板。

Fortunately you don't need to do that.幸运的是,您不需要这样做。 Just put your templates under resources folder and then it will just work as expected.只需将您的模板放在resources文件夹下,然后它就会按预期工作。 Here is a simple project proved it:这是一个简单的项目证明了这一点:

在此处输入图片说明

The project source code can be found at https://github.com/greenlaw110/rythm-gh-issue-241项目源代码可在https://github.com/greenlaw110/rythm-gh-issue-241找到

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

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