Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JHipster webTemplateResolver looking for /WEB-INF/templates/error.html jar contains /WEB-INF/classes/templates/error.html

I have just built my app using the latest version of generator-jhipster. Things were great until I made some changes, causing the server to want to generate an error page. Unfortunately the out-of-the-box configuration does not resolve the error.html template:

@Bean
@Description("Thymeleaf template resolver serving HTML 5")
public ServletContextTemplateResolver webTemplateResolver() {
    ServletContextTemplateResolver webTemplateResolver = new ServletContextTemplateResolver();
    webTemplateResolver.setPrefix("/WEB-INF/templates/");
    webTemplateResolver.setSuffix(".html");
    webTemplateResolver.setTemplateMode("HTML5");
    webTemplateResolver.setCharacterEncoding(CharEncoding.UTF_8);
    webTemplateResolver.setOrder(2);
    return webTemplateResolver;
}

When deployed to tomcat, I get an endless list of error messages... Exception processing template "error": Error resolving template "error", template might not exist or might not be accessible by any of the configured Template Resolvers

Does this work for others? If not, should the Thymeleaf templates be generated in the src/main/webapp/WEB-INF/ directory to overcome this problem? Alternatively, should a different ViewResolver configuration be used?

By changing the above to this, actually solved the problem...

 public ITemplateResolver webTemplateResolver() {
        ClassLoaderTemplateResolver webTemplateResolver = new ClassLoaderTemplateResolver();
        webTemplateResolver.setPrefix("templates/");
        webTemplateResolver.setSuffix(".html");
        webTemplateResolver.setTemplateMode("HTML5");
        webTemplateResolver.setCharacterEncoding(CharEncoding.UTF_8);
        webTemplateResolver.setOrder(2);
        return webTemplateResolver;
    }

So the only question remains is whether this change should be pushed into JHipster and if so how to?

like image 209
AGV Avatar asked Nov 10 '22 04:11

AGV


1 Answers

In generated files, there is an error.html page in resources/templates/error.html. 'resources/templates' is the default template directory. I think you can solve it by copying the files into your templates dir.

The difference of the two configuration is:

When you use ServletContextTemplateResolver, the servlet context here means the root dir of web context, it is webapp dir.

If you change it to ClassLoaderTemplateResolver, the class loader here means that the root for the prefix is class path, then it is like 'resources' and java classes, which will be compiled and copied into WEB-INF/classes.

like image 94
Mavlarn Avatar answered Dec 04 '22 07:12

Mavlarn