You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Spring Boot整合JSP搭建欢迎页遭遇404错误求助

Spring Boot JSP Welcome Page 404 Error Troubleshooting

I'm trying to build a simple welcome page using JSP with Spring Boot. Here's my project structure:
Project Structure

Application Class

@SpringBootApplication 
public class Application { 
    public static void main(String[] args) { 
        SpringApplication.run(Application.class, args); 
    } 
    public SpringApplicationBuilder configure(SpringApplicationBuilder application) { 
        return application.sources(Application.class); 
    } 
}

APIController Class

@Controller 
public class APIController { 
    @RequestMapping("/") 
    public String home() { 
        System.out.println("testing"); 
        return "welcome"; 
    } 
}

When I access http://localhost:8080/, I get this error:

This application has no explicit mapping for /error, so you are seeing this as a fallback.
Wed May 23 15:31:51 MYT 2018 There was an unexpected error (type=Not Found, status=404). No message available

Curl response:

{"timestamp":1527061233703,"status":404,"error":"Not Found","message":"No message available","path":"/"}

pom.xml Configuration

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <artifactId>spring-boot-web-jsp</artifactId> 
    <packaging>war</packaging> 
    <name>Spring Boot Web JSP Example</name> 
    <description>Spring Boot Web JSP Example</description> 
    <version>1.0</version> 
    <parent> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-starter-parent</artifactId> 
        <version>1.4.2.RELEASE</version> 
    </parent> 
    <properties> 
        <java.version>1.8</java.version> 
    </properties> 
    <dependencies> 
        <!-- This is a web application --> 
        <dependency> 
            <groupId>org.springframework.boot</groupId> 
            <artifactId>spring-boot-starter-web</artifactId> 
        </dependency> 
        <dependency> 
            <groupId>org.apache.tomcat.embed</groupId> 
            <artifactId>tomcat-embed-jasper</artifactId> 
            <scope>provided</scope> 
        </dependency> 
        <dependency> 
            <groupId>javax.servlet</groupId> 
            <artifactId>jstl</artifactId> 
        </dependency> 
        <!-- Optional, test for static content, bootstrap CSS--> 
        <dependency> 
            <groupId>org.webjars</groupId> 
            <artifactId>bootstrap</artifactId> 
            <version>3.3.7</version> 
        </dependency> 
    </dependencies> 
    <build> 
        <plugins> 
            <!-- Package as an executable jar/war --> 
            <plugin> 
                <groupId>org.springframework.boot</groupId> 
                <artifactId>spring-boot-maven-plugin</artifactId> 
            </plugin> 
        </plugins> 
    </build> 
</project>

I need help troubleshooting this 404 error.


Solution

Let's work through this 404 issue step by step. Based on your setup, these are the key fixes you need to apply:

1. Add View Resolver Configuration

Spring Boot doesn't automatically know where to find your JSP files unless you specify the view resolver's prefix and suffix. Create or update src/main/resources/application.properties with these lines:

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

Ensure your welcome.jsp is placed in src/main/webapp/WEB-INF/jsp/ – this matches the prefix we just configured, so Spring can locate the file correctly.

2. Fix the tomcat-embed-jasper Dependency Scope

You set <scope>provided</scope> for the tomcat-embed-jasper dependency. This tells Maven not to include it in the runtime classpath, but the embedded Tomcat in Spring Boot needs this library to compile JSPs at runtime. Change the scope to compile:

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>compile</scope> <!-- Updated from provided -->
</dependency>

3. Validate Your Project Structure

Double-check that:

  • src/main/webapp is marked as a web resource root in your IDE (most IDEs do this automatically, but if not, you'll need to configure it manually)
  • Your JSP files are under WEB-INF (this prevents users from accessing them directly via URL, which is a best practice)

4. Simplify the Application Class (Optional)

The configure method in your Application class isn't required when running with Spring Boot's embedded Tomcat. You can remove it to keep your code clean:

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

After making these changes, restart your application and visit http://localhost:8080/ again. Your controller should now successfully resolve to the welcome.jsp page.

内容的提问来源于stack exchange,提问作者Cyndi

火山引擎 最新活动