Home > database >  SpringBoot does not detect template of Thymeleaf
SpringBoot does not detect template of Thymeleaf

Time:11-14

I started learning Thymeleaf templating with SpringBoot and my learning path was blocked by some implicit issue i could not find....

The issue is: SpringBoot app does not see template, although:

  1. Controller looks like enter image description here

  2. Project structure includes /templates:

enter image description here

  1. All required dependencies are in place:

enter image description here

Spring Boot log:

2022-11-13 22:21:13.196 INFO 20644 --- [ main] com.coffeeshop.Application : Starting Application using Java 11.0.10 on LAPTOP-O6B9USVI with PID 20644 (C:\Dev\Java\Projects\coffeeshop\build\classes\java\main started by User in C:\Dev\Java\Projects\coffeeshop) 2022-11-13 22:21:13.196 INFO 20644 --- [ main] com.coffeeshop.Application : No active profile set, falling back to 1 default profile: "default" 2022-11-13 22:21:13.588 INFO 20644 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode. 2022-11-13 22:21:13.604 INFO 20644 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 23 ms. Found 1 JPA repository interfaces. 2022-11-13 22:21:14.106 INFO 20644 --- [
main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8081 (http) 2022-11-13 22:21:14.106 INFO 20644 --- [
main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2022-11-13 22:21:14.106 INFO 20644 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.64] 2022-11-13 22:21:14.184 INFO 20644 --- [
main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2022-11-13 22:21:14.184 INFO 20644 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 957 ms 2022-11-13 22:21:14.278 INFO 20644 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default] 2022-11-13 22:21:14.309 INFO 20644 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.6.9.Final 2022-11-13 22:21:14.309 INFO 20644 --- [ main] org.hibernate.cfg.Environment : HHH000205: Loaded properties from resource hibernate.properties: {hibernate.temp.use_jdbc_metadata_defaults=false, hibernate.bytecode.use_reflection_optimizer=false} 2022-11-13 22:21:14.404 INFO 20644 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final} 2022-11-13 22:21:14.466 INFO 20644 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect 2022-11-13 22:21:14.796 INFO 20644 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting... 2022-11-13 22:21:14.905 INFO 20644 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed. 2022-11-13 22:21:14.921 INFO 20644 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform] 2022-11-13 22:21:14.921 INFO 20644 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default' 2022-11-13 22:21:15.094 WARN 20644 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning 2022-11-13 22:21:15.298 INFO 20644 --- [
main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8081 (http) with context path '' 2022-11-13 22:21:15.298 INFO 20644 --- [ main] com.coffeeshop.Application
: Started Application in 2.409 seconds (JVM running for 2.7)

When i check http://localhost:8081/home I got "home" string only.

CodePudding user response:

Replace @RestController with @Controller. You cannot use @RestController because @RestController automatically adds @ResponseBody and Spring will only attempt to look up a view if @ResponseBody is not present.

CodePudding user response:

@Controller

public class AppController {

@GetMapping("/")
public String viewHomePage() {
    
    return "home";
}

}

this should fix your probleme

  • Related