我有一个集成了bootstrap + thymeleaf + spring boot的项目(1)和一个spring boot正在开发的Restful API的项目(2)。现在,我想收集它们。当我只运行project(1)时:
2019-04-12 10:11:40.165 INFO 23188 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1180 ms
2019-04-12 10:11:40.290 INFO 23188 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2019-04-12 10:11:40.290 INFO 23188 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2019-04-12 10:11:40.290 INFO 23188 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2019-04-12 10:11:40.290 INFO 23188 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2019-04-12 10:11:40.290 INFO 23188 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2019-04-12 10:11:40.337 WARN 23188 --- [ restartedMain] org.thymeleaf.templatemode.TemplateMode : [THYMELEAF][restartedMain] Template Mode 'HTML5' is deprecated. Using Template Mode 'HTML' instead.
2019-04-12 10:11:40.687 INFO 23188 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5d364324: startup date [Fri Apr 12 10:11:38 ICT 2019]; root of context hierarchy
2019-04-12 10:11:40.734 INFO 23188 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/company-department-new],methods=[GET]}" onto public java.lang.String net.crunchdroid.PageController.companyDepartmentNew()
2019-04-12 10:11:40.734 INFO 23188 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/company-departments],methods=[GET]}" onto public java.lang.String net.crunchdroid.PageController.companyDepartments()
当我收集它们时,我看不到映射servlet就像
2019-04-12 10:11:40.290 INFO 23188 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]...
,访问“ http://localhost:8080/dashboard”时出现此错误:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri Apr 12 09:59:25 ICT 2019
There was an unexpected error (type=Internal Server Error, status=500).
Exception processing template (template: "/dashboard")
org.thymeleaf.exceptions.TemplateProcessingException: Exception processing template (template: "/dashboard")
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1132)
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1061)
at org.thymeleaf.spring4.view.ThymeleafView.renderFragment(ThymeleafView.java:335)
at org.thymeleaf.spring4.view.ThymeleafView.render(ThymeleafView.java:189)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1370)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1116)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1055)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897) [...etc]
Caused by: java.lang.IllegalArgumentException: Application Context cannot be null
at org.thymeleaf.util.Validate.notNull(Validate.java:37)
at org.thymeleaf.spring4.templateresource.SpringResourceTemplateResource.<init>(SpringResourceTemplateResource.java:64)
at org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver.computeTemplateResource(SpringResourceTemplateResolver.java:69)
at org.thymeleaf.templateresolver.AbstractConfigurableTemplateResolver.computeTemplateResource(AbstractConfigurableTemplateResolver.java:1221)
at org.thymeleaf.templateresolver.AbstractTemplateResolver.resolveTemplate(AbstractTemplateResolver.java:356)
at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:840)
at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:608)
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1087)
... 49 more
这是project(1)的pom.xml:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Thymeleaf -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>3.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
<version>2.0.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
这是project(2)的pom.xml,然后收集它们:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-net/commons-net -->
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.hateoas/spring-hateoas -->
<dependency>
<groupId>org.springframework.hateoas</groupId>
<artifactId>spring-hateoas</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--Thymeleaf -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>3.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
<version>2.0.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
当我收集它们时,我选择了2.1.4 这是PageController.java:
@Controller
public class PageController {
// Dashboard
@GetMapping("/dashboard")
public String dashBoard() {
return "/dashboard";
}
/// Some get mapping but I simplify
}
这是ThymeleafWebMvcConfig.java:
@Configuration
public class ThymeleafWebMvcConfig implements WebMvcConfigurer {
@Bean
public ViewResolver viewResolver() {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
return viewResolver;
}
@Bean
public ITemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
templateEngine.addDialect(new LayoutDialect());
return templateEngine;
}
private ITemplateResolver templateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setPrefix("classpath:/templates/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode(TemplateMode.HTML);
templateResolver.setCharacterEncoding("UTF-8");
templateResolver.setCacheable(false);
return templateResolver;
}
}
这是主要的:
@SpringBootApplication
@EnableJpaAuditing
@EnableConfigurationProperties({
FileStorageProperties.class ///-> That related my project to deploy API send file
})
public class PostgresDemoApplication {
public static void main(String[] args) {
SpringApplication.run(PostgresDemoApplication.class, args);
}
}
答案 0 :(得分:0)
对于非Web(非Spring MVC项目),您可以尝试使用ClassLoaderTemplateResolver,如下所示:
private ITemplateResolver templateResolver() {
ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
//Ensure the path is right or you work without the logic using the ClassLoaderTemplateResolver
templateResolver.setPrefix("classpath:/templates/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode(TemplateMode.HTML);
templateResolver.setCharacterEncoding("UTF-8");
templateResolver.setCacheable(false);
return templateResolver;
}
就我而言,我的模板直接位于springboot资源文件夹中,并且按如下方式引用它:
//TemplateEngine should be auto wired
TemplateEngine templateEngine = templateEngine();
templateEngine.process("Test-Template.html", ctx, stringWriter);