Spring Boot无法解析视图页面:访问localhost:8080出现404错误求助
Hey there, let's figure out why your Spring Boot app is throwing that 404 error when accessing http://localhost:8080/. Based on your description, there are a few common pitfalls that could be causing this view resolution issue. Let's go through them one by one:
1. Check Your Dependency Setup
First, make sure you have the necessary dependencies in your pom.xml (or build.gradle):
- For basic web functionality, you need the
spring-boot-starter-webdependency. - If you're packaging your app as a WAR file (instead of JAR), you'll also need to include the
spring-boot-starter-tomcatdependency withprovidedscope to avoid conflicts with the server's Tomcat instance.
Here's what the relevant pom.xml entries should look like:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Only needed for WAR packaging --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> </dependencies>
2. Fix Your View Path Configuration
Your application.properties has a small but critical issue: the prefix is missing a leading slash. Without it, Spring will resolve the path incorrectly relative to the application context. Update your config to:
spring.mvc.view.prefix=/WEB-INF/html/ spring.mvc.view.suffix=.html
3. Verify Your HTML File Location
The path you configured (/WEB-INF/html/) requires your HTML files to be placed in a specific directory, which depends on your packaging type:
- WAR packaging: Place your HTML files in
src/main/webapp/WEB-INF/html/(e.g.,src/main/webapp/WEB-INF/html/index.html). Thewebappdirectory is standard for WAR files and will be recognized by the servlet container. - JAR packaging: The
src/main/webappdirectory is not included in JAR files by default, so this path won't work. For JARs, it's better to use a template engine like Thymeleaf (more on that below) or move your HTML files to thesrc/main/resources/static/directory (for static content that doesn't need view resolution).
4. Ensure You Have a Controller to Handle the Request
A 404 error often happens when there's no controller mapping to handle the root path (/). You need a @Controller class that returns the view name corresponding to your HTML file. For example:
@Controller public class HomeController { @RequestMapping("/") public String home() { // This will map to /WEB-INF/html/index.html with your current config return "index"; } }
Without this controller, Spring won't know to route the / request to your HTML file (unless it's in the static resources directory).
5. Consider Using Thymeleaf (For JAR Packaging)
If you're using JAR packaging (the default for Spring Boot), switching to Thymeleaf simplifies view management. Here's how:
- Add the Thymeleaf dependency to your pom.xml:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> - Move your HTML files to
src/main/resources/templates/(e.g.,templates/index.html). - Remove the
spring.mvc.view.prefixandspring.mvc.view.suffixfrom yourapplication.properties—Thymeleaf uses sensible defaults for these. - Your controller can stay the same (returning "index" will map to
templates/index.htmlautomatically).
Quick Recap of Common Fixes
- Add the leading slash to your
spring.mvc.view.prefix - Place HTML files in the correct directory based on your packaging type
- Add a controller to map the
/path to your view - For JARs, use Thymeleaf instead of the traditional WEB-INF path setup
内容的提问来源于stack exchange,提问作者hellzone

