Spring Boot中RequestMappingHandlerMapping找不到处理方法及404错误求助
Hey there, let's work through this issue step by step—this is a super common pitfall for Spring Boot newbies, so you're not alone!
1. First, Make Sure You Have JSP Dependencies
Spring Boot doesn't include JSP support out of the box, so if you haven't added the required dependencies, your app won't be able to render JSP files at all.
For Maven (add to pom.xml):
<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>
For Gradle (add to build.gradle):
implementation 'javax.servlet:jstl' providedRuntime 'org.apache.tomcat.embed:tomcat-embed-jasper'
2. Verify Your View Resolver Configuration
Double-check your application.properties to ensure the view prefix and suffix are set correctly. The values should point to where your JSP files are stored, without typos:
# Correct configuration example spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp
Note: Make sure the prefix ends with a slash (/) and the suffix starts with a dot (.). Missing these can break the path resolution.
3. Fix Your Controller Mapping (The Big One!)
The log message RequestMappingHandlerMapping : Did not find handler method for [/WEB-INF/jsp/home.jsp] tells us exactly what's wrong here: you're trying to access the JSP file directly via its physical path (/WEB-INF/jsp/home.jsp) instead of going through a controller that forwards to it.
Spring Boot uses controllers to map user requests to views. You need a controller method that maps to the URL you're visiting, and returns the logical view name (not the full JSP path).
Example Controller:
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller public class HomeController { // Maps to http://localhost:8080/home @GetMapping("/home") public String showHomePage() { // Returns the logical view name "home" // View resolver will turn this into /WEB-INF/jsp/home.jsp return "home"; } }
Now, instead of visiting http://localhost:8080/WEB-INF/jsp/home.jsp, go to http://localhost:8080/home—this should trigger the controller and render your JSP correctly.
4. Check Your Project Structure
Ensure your JSP files are in the right place. For a standard Maven/Gradle Spring Boot project, the structure should look like this:
src/ └── main/ ├── java/ │ └── [your package]/ │ └── HomeController.java ├── resources/ │ └── application.properties └── webapp/ └── WEB-INF/ └── jsp/ └── home.jsp
If the webapp or WEB-INF/jsp folders don't exist, create them manually—Spring Boot won't generate them by default.
5. Understanding the ContentNegotiatingViewResolver Logs
That related ContentNegotiatingViewResolver log is just telling you it's trying to find a matching view for your request, but since the earlier steps were misconfigured, it couldn't locate the right one. Once you fix the above issues, these logs should disappear (or show a successful view resolution).
内容的提问来源于stack exchange,提问作者Ng Sharma

