Spring Boot模板未解析

问题描述:

我正在尝试使用Spring Boot和Thymeleaf构建一个独立的Web应用程序.该应用程序可以从IntelliJ IDEA正常运行,但是我无法自行运行jar.显然,不包括模板.

I'm trying to build a standalone web application using Spring Boot and Thymeleaf. The application runs fine from IntelliJ IDEA, but I'm not able to run the jar on its own. Apparently the templates are not included.

我的项目的结构如下:

├── src
│   └── main
│       ├── java
│       │   └── my
│       │       └── domain
│       │           └── application
│       │               ├── domain
│       │               ├── service
│       │               ├── web
│       │               ├── ApplicationConfig.java
│       │               ├── SecurityConfig.java
│       │               ├── ThymeleafConfig.java
│       │               └── WebConfig.java
│       ├── resources
│       │   ├── application.properties
│       │   └── log4j.properties
│       └── webapp
│           ├── resources
│           │   ├── jquery.js
│           │   └── style.css
│           └── WEB-INF
│               └── views
│                   ├── layout.html
│                   ├── login.html
│                   └── menu.html
└── pom.xml

ApplicationConfig:

ApplicationConfig:

@Bean
public DataSource dataSource() {
    ...
}

@Bean
public SessionBean sessionBean() {
    return new SessionBean();
}

public static void main(String[] args) {
    SpringApplication.run(ApplicationConfig.class, args);
}

ThymeleafConfig:

ThymeleafConfig:

@Bean
public TemplateResolver templateResolver(){
    ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
    templateResolver.setPrefix("/WEB-INF/views/");
    templateResolver.setSuffix(".html");
    templateResolver.setTemplateMode("HTML5");
    return templateResolver;
}

@Bean
public SpringTemplateEngine templateEngine(){
    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.setTemplateResolver(templateResolver());
    templateEngine.addDialect(new LayoutDialect());
    return templateEngine;
}

@Bean
public ViewResolver viewResolver(){
    ThymeleafViewResolver viewResolver = new ThymeleafViewResolver() ;
    viewResolver.setTemplateEngine(templateEngine());
    viewResolver.setOrder(1);
    return viewResolver;
}

WebConfig:

WebConfig:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**")
            .addResourceLocations("/resources/");
}

我通过将WEB-INF文件夹添加为pom.xml中的资源来设法将其包括在内,但是视图仍然无法解析.

I managed to include the WEB-INF folder by adding it as resource in pom.xml, but the views are still not resolved.

您是否尝试过完全删除WEB-INF文件夹并将所有内容粘贴到资源中,就像在

Have you tried removing the WEB-INF folder completely and sticking everything in resources as is done in this guide?