添加tomcat-embed-jasper后JSP白标404且项目报错、找不到主类求助
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
webappfolder, create it undersrc/main. - In IDEA, right-click the
webappfolder 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 insrc/main/javaand not in an excluded subfolder. - Make sure your
pom.xmlhas 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 yourpom.xmlif 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 installin 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

