Spring Boot模板未解析 [英] Spring Boot templates not resolved

查看:129
本文介绍了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?

这篇关于Spring Boot模板未解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆