Spring Boot无法渲染动态页面问题求助
Spring Boot无法渲染动态页面问题求助
我折腾Spring Boot渲染动态页面好一阵了,一直没进展,从YouTube到AI工具都试过了,现在只能来这儿求助了。
每次访问应该显示动态页面的端点时,它只返回一个字符串(就是我控制器里return的"home"),根本不渲染页面。
我的控制器代码
package com.demo.student1.api; import com.demo.student1.model.Student; import com.demo.student1.service.StudentService; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; import java.util.Optional; @RequestMapping("/student") @RestController public class StudentController { private final StudentService studentService; public StudentController(StudentService studentService) { this.studentService = studentService; } @GetMapping("/home") public String home(Model model) { model.addAttribute("username", "John Doe"); return "home"; } @PostMapping("/register") public void registerStudent(@RequestBody Student student) { studentService.registerStudent(student); } @GetMapping("/get_student/{id}") public Optional<Student> getStudent(@PathVariable Long id) { return studentService.getStudent(id); } @GetMapping("/getStudents") public ArrayList<Student> getStudents() { return studentService.getStudents(); } }
动态页面代码(Thymeleaf)
<!DOCTYPE html> <html xmlns:th="http://thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Welcome Homepage For Students</title> </head> <body> <h1>Welcome to University, <span th:text="${username}"></span> to University!</h1> </body> </html>
Gradle依赖配置
dependencies { implementation("org.springframework.boot:spring-boot-starter-data-jpa") implementation("org.springframework.boot:spring-boot-starter-web") implementation("org.springframework.security:spring-security-web") implementation("org.springframework.boot:spring-boot-starter-security:3.4.1") implementation("io.jsonwebtoken:jjwt-api:0.12.6") implementation("org.springframework:spring-webmvc:6.2.2") implementation("org.apache.tomcat:tomcat-jasper:10.1.34") // implementation("org.thymeleaf:thymeleaf:3.1.3.RELEASE") implementation("org.springframework.boot:spring-boot-starter-thymeleaf") compileOnly("org.projectlombok:lombok:1.18.36") runtimeOnly("io.jsonwebtoken:jjwt-impl:0.12.6") runtimeOnly("io.jsonwebtoken:jjwt-jackson:0.12.6") runtimeOnly("org.postgresql:postgresql") testImplementation("org.springframework.boot:spring-boot-starter-test") testRuntimeOnly("org.junit.platform:junit-platform-launcher") }
应用运行日志(访问端点时的输出)
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v3.4.1) 2025-02-08T09:24:16.263+01:00 INFO 23567 --- [student-1] [ main] com.demo.student1.Student1Application : Starting Student1Application using Java 21.0.5 with PID 23567 (/home/isla-jr/Documents/se-workspace/learn-java/roadmap/4-web-frameworks/1-basic/student-1/build/classes/java/main started by isla-jr in /home/isla-jr/Documents/se-workspace/learn-java/roadmap/4-web-frameworks/1-basic/student-1) 2025-02-08T09:24:16.265+01:00 INFO 23567 --- [student-1] [ main] com.demo.student1.Student1Application : No active profile set, falling back to 1 default profile: "default" 2025-02-08T09:24:16.814+01:00 INFO 23567 --- [student-1] [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode. 2025-02-08T09:24:16.867+01:00 INFO 23567 --- [student-1] [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 44 ms. Found 2 JPA repository interfaces. 2025-02-08T09:24:17.313+01:00 INFO 23567 --- [student-1] [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port 8080 (http) 2025-02-08T09:24:17.325+01:00 INFO 23567 --- [student-1] [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2025-02-08T09:24:17.325+01:00 INFO 23567 --- [student-1] [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.34] 2025-02-08T09:24:17.457+01:00 INFO 23567 --- [student-1] [ main] org.apache.jasper.servlet.TldScanner : At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. 2025-02-08T09:24:17.460+01:00 INFO 23567 --- [student-1] [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2025-02-08T09:24:17.460+01:00 INFO 23567 --- [student-1] [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1154 ms 2025-02-08T09:24:17.617+01:00 INFO 23567 --- [student-1] [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default] 2025-02-08T09:24:17.660+01:00 INFO 23567 --- [student-1] [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 6.6.4.Final 2025-02-08T09:24:17.684+01:00 INFO 23567 --- [student-1] [ main] o.h.c.internal.RegionFactoryInitiator : HHH000026: Second-level cache disabled 2025-02-08T09:24:17.923+01:00 INFO 23567 --- [student-1] [ main] o.s.o.j.p.SpringPersistenceUnitInfo : No LoadTimeWeaver setup: ignoring JPA class transformer 2025-02-08T09:24:17.945+01:00 INFO 23567 --- [student-1] [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting... 2025-02-08T09:24:18.012+01:00 INFO 23567 --- [student-1] [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Added connection org.postgresql.jdbc.PgConnection@7b3a6e95 2025-02-08T09:24:18.013+01:00 INFO 23567 --- [student-1] [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed. 2025-02-08T09:24:18.060+01:00 INFO 23567 --- [student-1] [ main] org.hibernate.orm.connections.pooling : HHH10001005: Database info: Database JDBC URL [Connecting through datasource 'HikariDataSource (HikariPool-1)'] Database driver: undefined/unknown Database version: 16.3 Autocommit mode: undefined/unknown Isolation level: undefined/unknown Minimum pool size: undefined/unknown Maximum pool size: undefined/unknown 2025-02-08T09:24:18.834+01:00 INFO 23567 --- [student-1] [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000489: No JTA platform available (set 'hibernate.transaction.jta.platform' to enable JTA platform integration) 2025-02-08T09:24:18.881+01:00 INFO 23567 --- [student-1] [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default' 2025-02-08T09:24:19.173+01:00 INFO 23567 --- [student-1] [ main] eAuthenticationProviderManagerConfigurer : Global AuthenticationManager configured with AuthenticationProvider bean with name authenticationProvider 2025-02-08T09:24:19.173+01:00 WARN 23567 --- [student-1] [ main] r$InitializeUserDetailsManagerConfigurer : Global AuthenticationManager configured with an AuthenticationProvider bean. UserDetailsService beans will not be used by Spring Security for automatically configuring username/password login. Consider removing the AuthenticationProvider bean. Alternatively, consider using the UserDetailsService in a manually instantiated DaoAuthenticationProvider. If the current configuration is intentional, to turn off this warning, increase the logging level of 'org.springframework.security.config.annotation.authentication.configuration.InitializeUserDetailsBeanManagerConfigurer' to ERROR 2025-02-08T09:24:19.182+01:00 WARN 23567 --- [student-1] [ 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 2025-02-08T09:24:19.636+01:00 INFO 23567 --- [student-1] [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path '/' 2025-02-08T09:24:19.645+01:00 INFO 23567 --- [student-1] [ main] com.demo.student1.Student1Application : Started Student1Application in 3.747 seconds (process running for 4.303) 2025-02-08T09:24:45.131+01:00 INFO 23567 --- [student-1] [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2025-02-08T09:24:45.131+01:00 INFO 23567 --- [student-1] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2025-02-08T09:24:45.133+01:00 INFO 23567 --- [student-1] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms Hibernate: select u1_0.id,u1_0.password,u1_0.username from...
备注:内容来源于stack exchange,提问作者Isla
相关产品推荐
相关产品推荐

