You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot无法解析视图页面:访问localhost:8080出现404错误求助

Troubleshooting Spring Boot 404 Error for HTML View Resolution

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-web dependency.
  • If you're packaging your app as a WAR file (instead of JAR), you'll also need to include the spring-boot-starter-tomcat dependency with provided scope 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). The webapp directory is standard for WAR files and will be recognized by the servlet container.
  • JAR packaging: The src/main/webapp directory 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 the src/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:

  1. Add the Thymeleaf dependency to your pom.xml:
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    
  2. Move your HTML files to src/main/resources/templates/ (e.g., templates/index.html).
  3. Remove the spring.mvc.view.prefix and spring.mvc.view.suffix from your application.properties—Thymeleaf uses sensible defaults for these.
  4. Your controller can stay the same (returning "index" will map to templates/index.html automatically).

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:20:24