Spring无法找到HTML页面求助(Spring新手)
Hey there! Let's work through this together—since you're new to Spring Boot, it's totally normal to hit small snags when trying to serve HTML pages. Most issues boil down to structure, dependencies, or controller tweaks, so let's go step by step.
1. Double-Check Your Project Directory Structure
Spring Boot has strict default rules for where to put resources—don't overlook this!
- Plain static HTML/CSS/JS: Drop your files in the
src/main/resources/staticfolder. For example, if you havestatic/index.html, you can access it directly athttp://localhost:8080/index.htmlwithout even writing a controller. - Using a template engine (like Thymeleaf, recommended for beginners): Place your HTML files in
src/main/resources/templates. Spring Boot integrates seamlessly with Thymeleaf, so this is the easiest way to add dynamic content later too.
2. Verify Your Dependencies
Make sure your build file has the right bits to serve HTML:
For Maven (pom.xml):
If using Thymeleaf:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
For plain static HTML, you only need the core web starter:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
For Gradle (build.gradle):
Thymeleaf dependency:
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
Plain static HTML just needs:
implementation 'org.springframework.boot:spring-boot-starter-web'
3. Fix Up Your Controller Code
From your snippet, you're using @Controller—that's correct! (Never use @RestController here, because it returns raw text/JSON instead of resolving to an HTML view.) Here's a complete, corrected controller example:
package com.springbootrecipes.controllers; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller // Note: If your main application class has @SpringBootApplication, you don't need @EnableAutoConfiguration here—it's already included and will cause conflicts if duplicated! public class HomeController { // @GetMapping is more specific than @RequestMapping for GET requests @GetMapping("/") public String showHomePage() { // If using Thymeleaf, return the name of your HTML file (without .html) in templates/ // For static HTML, you can also use return "forward:/index.html" if you want to route through the controller return "index"; } }
4. Test the Right URL
- Static HTML in
static/index.html: Visithttp://localhost:8080/index.htmldirectly - Thymeleaf template in
templates/index.html: Visithttp://localhost:8080/(if your controller maps to/)
5. Common Pitfalls to Check
- Filename casing: On Linux/macOS,
Index.htmlandindex.htmlare different—stick to lowercase to avoid issues. - Extra slashes in return value: Don't return
"/index"; just return"index"for Thymeleaf to find the template. - Port conflicts: If your app won't start, change the port in
application.propertieswithserver.port=8081. - Missing main class annotation: Make sure your main application class has
@SpringBootApplicationso Spring Boot scans your controllers.
If you're still stuck, share your full project directory structure (exactly where your HTML file lives) and complete controller code, and I'll help you dig deeper!
内容的提问来源于stack exchange,提问作者Pedro Resende

