简体   繁体   中英

Unable to use Rythm template engine with servlet 3.0

I am trying to use Rythm templating engine with servlet 3.0 on tomcat7.
I want to render template from WebContent directory to Rythm engine. But it is not detecting the template.

In servlet init() method I initialized Rthym engine as

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

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"

I had a similar issue with Rythm and in my case it helped to write the directory in front of the filename:

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

Setting the home.template variable didn't work for me too.

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. See Resolving the root of a webapp from getResource .

If you want to load templates under webapp folder, you need to create your own resource manager and delegate to ServletContext to load template.

Fortunately you don't need to do that. Just put your templates under resources folder and then it will just work as expected. Here is a simple project proved it:

在此处输入图片说明

The project source code can be found at https://github.com/greenlaw110/rythm-gh-issue-241

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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