5

我刚刚使用最新版本的 generator-jhipster 构建了我的应用程序。一切都很好,直到我做了一些改变,导致服务器想要生成一个错误页面。不幸的是,开箱即用的配置无法解析 error.html 模板:

@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;
}

部署到 tomcat 时,我收到无穷无尽的错误消息列表... 异常处理模板“错误”:解析模板“错误”时出错,模板可能不存在或任何配置的模板解析器都无法访问

这对其他人有用吗?如果没有,是否应该在 src/main/webapp/WEB-INF/ 目录下生成 Thymeleaf 模板来解决这个问题?或者,是否应该使用不同的 ViewResolver 配置?

把上面的改成这个,居然解决了问题……

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

所以剩下的唯一问题是这种变化是否应该被推送到 JHipster 中,如果是的话怎么做?

4

1 回答 1

1

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.

于 2014-11-28T03:53:28.560 回答