[英]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
文件夾下,然后它就會按預期工作。 這是一個簡單的項目證明了這一點:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.