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

添加tomcat-embed-jasper后JSP白标404且项目报错、找不到主类求助

Fixes for Tomcat Embed Jasper Issues: 404 JSP, Project Errors, and Main Class Not Found

Hey there! I’ve dealt with these exact headaches when setting up JSP in Spring Boot, so let’s walk through the fixes step by step:

1. Fix Your Tomcat Embed Jasper Dependency

Spring Boot has specific requirements for JSP support. Your current dependency is missing key pieces, and the scope might be off. Update your pom.xml dependencies like this:

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <!-- Use 'compile' for embedded Tomcat; switch to 'provided' if deploying as a WAR to external Tomcat -->
    <scope>compile</scope>
</dependency>
<!-- Add JSTL (required for JSP tag libraries) -->
<!-- For Spring Boot 2.x -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>
<!-- For Spring Boot 3.x, replace the above with Jakarta EE versions -->
<!--
<dependency>
    <groupId>jakarta.servlet.jsp.jstl</groupId>
    <artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
</dependency>
<dependency>
    <groupId>org.glassfish.web</groupId>
    <artifactId>jakarta.servlet.jsp.jstl</artifactId>
</dependency>
-->

2. Double-Check Your Project Structure

Spring Boot is picky about where JSP files live. Your application.properties points to /WEB-INF/views/, so your project must follow this structure exactly:

src
├── main
│   ├── java
│   │   └── your/package/with/MainApplication.java (the one with @SpringBootApplication)
│   ├── resources
│   │   └── application.properties
│   └── webapp
│       └── WEB-INF
│           └── views
│               └── your-page.jsp
└── pom.xml
  • If you don’t have a webapp folder, create it under src/main.
  • In IDEA, right-click the webapp folder and select Mark Directory as > Web Resource Directory so the IDE recognizes it correctly.

3. Fix the "Could Not Find or Load Main Class" Error

This almost always ties to Maven configuration or project structure:

  • First, confirm your main class (with @SpringBootApplication) is in src/main/java and not in an excluded subfolder.
  • Make sure your pom.xml has the correct Spring Boot Maven plugin configuration. If your main class isn’t in the root package, explicitly set it:
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>your.package.path.MainApplication</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>
  • Also, note that Spring Boot’s JAR format has limitations with JSP—consider switching to WAR packaging by adding <packaging>war</packaging> to your pom.xml if you run into persistent issues.

4. Refresh Dependencies and Clear Caches

That red project marker often means Maven dependencies are out of sync:

  • Run mvn clean install in your terminal to rebuild dependencies and the project.
  • In your IDE, clear cached data:
    • IDEA: Go to File > Invalidate Caches... > Invalidate and Restart
    • Eclipse: Go to Project > Clean... and select your project

5. Verify Your application.properties

Your config looks right, but double-check for typos or extra spaces. If you’re on Spring Boot 2.6+, you might also want to add:

spring.mvc.view.allow-request-override=true

This isn’t always necessary, but it can fix edge-case JSP rendering issues.

Give these steps a try—they should resolve all your issues!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 04:21:54