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

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:

1. Controller recognizes .html but not .jsp pages

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 an InternalResourceViewResolver in your XML config (like spring-servlet.xml) or configuration class, pointing to your JSP file path:
    @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;
    }
    
    Double-check that your MVC_First_Page.jsp is 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 your pom.xml (Maven):
    <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>
    
    Then add these properties to application.properties to tell Spring where to find your JSPs:
    spring.mvc.view.prefix=/WEB-INF/views/
    spring.mvc.view.suffix=.jsp
    
2. Error resolving template "error"

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 an error.jsp (or error.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 default error one, create a global exception handler:
    @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;
        }
    }
    
    Just make sure you create the corresponding custom_error.jsp file in your view folder.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 09:05:31