我使用的是mvc + java HttpServlet + Thymeleaf模板,但是它的错误。文件home.html已存在。
我的代码如下:
ServletContextTemplateResolver templateResolver
= new ServletContextTemplateResolver();
// XHTML is the default mode, but we will set it anyway for better understanding of code
templateResolver.setTemplateMode("HTML5");
templateResolver.setPrefix("D:\\development\\projects\\fpt.eclick.framework.web.thyme\\views\\");
templateResolver.setSuffix(".html");
templateResolver.setCacheTTLMs(3600000L);
TemplateEngine templateEngine = new TemplateEngine();
templateEngine.setTemplateResolver(templateResolver);
WebContext ctx = new WebContext(request, response, request.getServletContext(), request.getLocale());
try {
templateEngine.process("home", ctx, response.getWriter());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.setContentType("text/html;charset=UTF-8");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 1000);
使用Freemarker,我使用了相似的功能并且做得很好。
非常感谢
答案 0 :(得分:1)
问题在于模板目录的位置(前缀)。将其更改为Web应用程序的上下文根目录中可用的那个,例如/WEB-INF/templates
。