Spring新手求助:控制器可识别.html页面但无法识别.jsp页面
Hey there! Let's work through your Spring MVC issues step by step—since you're new to the Spring ecosystem, these are totally common beginner hurdles, so no stress!
First off, I noticed one easy-to-miss detail in your controller code: you're missing the @Controller annotation on your HelloWorldController class. Without this, Spring won't recognize it as a controller at all, which could be causing some of your view resolution headaches. Fix that first:
@Controller @RequestMapping("/welcome") public class HelloWorldController{ @RequestMapping(method = RequestMethod.GET) public ModelAndView helloWorld(){ ModelAndView model = new ModelAndView("MVC_First_Page"); model.addObject("msg", "hello world"); return model; } }
Now let's tackle your two main problems:
This is almost always a view resolver configuration issue or a missing dependency (especially if you're using Spring Boot). Here's how to fix it:
- For traditional Spring MVC projects (non-Boot):
Make sure you've configured anInternalResourceViewResolverin your XML config (likespring-servlet.xml) or configuration class, pointing to your JSP file path:
Double-check that your@Bean public InternalResourceViewResolver viewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); // Update this to your actual JSP folder path resolver.setSuffix(".jsp"); return resolver; }MVC_First_Page.jspis saved in the correct directory (e.g.,/WEB-INF/views/). - For Spring Boot projects:
JSP support isn't included by default, so you need to add these dependencies to yourpom.xml(Maven):
Then add these properties to<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>application.propertiesto tell Spring where to find your JSPs:spring.mvc.view.prefix=/WEB-INF/views/ spring.mvc.view.suffix=.jsp
This error happens because Spring's default exception handling tries to forward to an error template when an uncaught exception occurs—but you don't have that page in your project. You have two simple fixes:
- Option 1: Create the default error page
Add anerror.jsp(orerror.html) in your view directory (e.g.,/WEB-INF/views/). A basic example:<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Oops! Something went wrong</title> </head> <body> <h1>Sorry, we ran into an issue.</h1> </body> </html> - Option 2: Customize exception handling
If you want to use a custom error page instead of the defaulterrorone, create a global exception handler:
Just make sure you create the corresponding@ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(Exception.class) public ModelAndView handleAllExceptions(Exception ex) { ModelAndView model = new ModelAndView("custom_error"); // Name of your custom error page model.addObject("errorMessage", "Something went wrong: " + ex.getMessage()); return model; } }custom_error.jspfile in your view folder.
内容的提问来源于stack exchange,提问作者MAMMIA

