Spring Boot项目JSP页面404未找到问题排查与解决
Hey Ivan, let's break down why you're seeing that 404 "Page Not Found" error and how to fix it. From your project details, here are the key issues to address:
1. Incorrect JSP File Location
Spring Boot has strict conventions for where JSP files should live. By default, it looks for JSPs in src/main/webapp/WEB-INF/jsp/. If your login.jsp is placed elsewhere (like directly in src/main/webapp or another folder), Spring won't be able to locate it.
Fix:
- Move your
login.jspto thesrc/main/webapp/WEB-INF/jsp/directory (create the folders if they don't exist). - Alternatively, if you want to use a custom location, add these properties to your
application.propertiesfile to configure the view resolver:spring.mvc.view.prefix=/your/custom/path/ spring.mvc.view.suffix=.jsp
2. Wrong Scope for tomcat-embed-jasper Dependency
Your pom.xml sets the tomcat-embed-jasper dependency's scope to provided. When you run your Spring Boot app as an executable JAR (which is the default for your project setup), dependencies marked as provided aren't included in the final JAR. This means the JSP rendering engine isn't available at runtime, leading to a 404.
Fix:
Change the scope of the tomcat-embed-jasper dependency from provided to compile:
<dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>compile</scope> </dependency>
3. Verify Controller Package Structure (Quick Check)
Make sure your LoginController is in the same package or a sub-package of your WcApplication class. For example, if WcApplication is in com.wc, the controller should be in com.wc or com.wc.controllers—Spring Boot automatically scans components in these locations. From your code, this seems correct, but it's worth double-checking to rule out component scanning issues.
Final Steps to Test
After making these changes:
- Stop your running application.
- Run
mvn clean installto rebuild the project. - Start the app again with
mvn spring-boot:runor by running the JAR file. - Visit
http://localhost:8080/orhttp://localhost:8080/login—you should see the "hello" text from your login.jsp now.
内容的提问来源于stack exchange,提问作者Ivan

