thymeleaf/thymeleafexamples-stsm

Error resolving template “home”, template might not exist or might not be accessible by any of the configured Template Resolvers

vdnhat opened this issue · 4 comments

I'm using mvc + java HttpServlet + Thymeleaf template but it's error. File home.html has exist.

My code bellow:

    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);

With Freemarker I used similar to and it's well done.

Image code attach in file
done

Thank you very much

The ServletContextTemplateResolver tries to resolve your templates from the web application's root (or servlet context root), so prefix usually looks something like /WEB-INF/templates/thymeleaf/, for example.

But you have /src/main/resources/thymeleaf/, which looks more like the path to your templates at your project's source code.

hi,how to fill the templateResolver.setPrefix ,thanks a lot.

Xikl commented

me too

private final ThymeleafProperties thymeleafProperties;

@Autowired
public ThymeleafConfig(ThymeleafProperties thymeleafProperties) {
    this.thymeleafProperties = thymeleafProperties;
}

...
resolver.setPrefix(this.thymeleafProperties.getPrefix());

or you could use "classpath:/thymeleaf" . that will work too :)