我正在尝试使用Spring Boot和Thymeleaf构建一个独立的Web应用程序。 IntelliJ IDEA的应用程序运行良好,但我无法自行运行jar。显然,模板不包括在内。
我的项目结构如下:
├── 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:
@Bean
public DataSource dataSource() {
...
}
@Bean
public SessionBean sessionBean() {
return new SessionBean();
}
public static void main(String[] args) {
SpringApplication.run(ApplicationConfig.class, args);
}
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:
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**")
.addResourceLocations("/resources/");
}
我设法通过将其添加为pom.xml中的资源来包含WEB-INF文件夹,但视图仍未解析。