4
我正在嘗試使用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文件夾,但視圖仍未解決。
工作正常,謝謝!我在訪問靜態內容時遇到了一些問題,但如果找不到解決方案,我會回覆。 – user3170702
@ user3170702好吧,聽起來不錯! – geoand
我在嘗試使用模板時遇到了訪問靜態內容的相同問題。你有沒有找到解決辦法? – kgreenek