Spring Boot+Maven项目JSP页面404错误排查求助
Spring Boot中JSP页面404问题求助
运行Spring Boot项目时,JSP网页无法在浏览器正常显示。使用Spring MVC,通过application.properties和主控制器映射JSP页面,访问localhost:8080应返回JSP首页,但实际返回404错误。已在application.properties中启用Spring Web调试模式,仍无法定位问题。
调试命令行输出
2024-12-08T21:39:43.133-05:00 INFO 15944 --- [calculatorApp] [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2024-12-08T21:39:43.133-05:00 INFO 15944 --- [calculatorApp] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2024-12-08T21:39:43.134-05:00 DEBUG 15944 --- [calculatorApp] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Detected StandardServletMultipartResolver 2024-12-08T21:39:43.134-05:00 DEBUG 15944 --- [calculatorApp] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Detected AcceptHeaderLocaleResolver 2024-12-08T21:39:43.134-05:00 DEBUG 15944 --- [calculatorApp] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Detected FixedThemeResolver 2024-12-08T21:39:43.134-05:00 DEBUG 15944 --- [calculatorApp] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Detected org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator@1083e6a0 2024-12-08T21:39:43.134-05:00 DEBUG 15944 --- [calculatorApp] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Detected org.springframework.web.servlet.support.SessionFlashMapManager@4cf507d1 2024-12-08T21:39:43.135-05:00 DEBUG 15944 --- [calculatorApp] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : enableLoggingRequestDetails='false': request parameters and headers will be masked to prevent unsafe logging of potentially sensitive data 2024-12-08T21:39:43.135-05:00 INFO 15944 --- [calculatorApp] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 2 ms 2024-12-08T21:39:43.140-05:00 DEBUG 15944 --- [calculatorApp] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : GET "/", parameters={} 2024-12-08T21:39:43.147-05:00 DEBUG 15944 --- [calculatorApp] [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to main.controller.MainController#displayMainPage(Model) 2024-12-08T21:39:43.162-05:00 DEBUG 15944 --- [calculatorApp] [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, application/xhtml+xml, image/avif, image/webp, image/apng, application/xml;q=0.9, */*;q=0.8, application/signed-exchange;v=b3;q=0.7] 2024-12-08T21:39:43.162-05:00 DEBUG 15944 --- [calculatorApp] [nio-8080-exec-1] o.s.w.servlet.view.InternalResourceView : View name [index], model {message=Hello, welcome to the JSP web application!} 2024-12-08T21:39:43.163-05:00 DEBUG 15944 --- [calculatorApp] [nio-8080-exec-1] o.s.w.servlet.view.InternalResourceView : Forwarding to [/WEB-INF/jsp/index.jsp] 2024-12-08T21:39:43.165-05:00 DEBUG 15944 --- [calculatorApp] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : "FORWARD" dispatch for GET "/WEB-INF/jsp/index.jsp", parameters={} 2024-12-08T21:39:43.169-05:00 DEBUG 15944 --- [calculatorApp] [nio-8080-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/], ServletContext [/]] 2024-12-08T21:39:43.169-05:00 WARN 15944 --- [calculatorApp] [nio-8080-exec-1] o.s.w.s.resource.ResourceHandlerUtils : "Path with "WEB-INF" or "META-INF": [WEB-INF/jsp/index.jsp]" 2024-12-08T21:39:43.170-05:00 DEBUG 15944 --- [calculatorApp] [nio-8080-exec-1] o.s.w.s.r.ResourceHttpRequestHandler : Resource not found 2024-12-08T21:39:43.172-05:00 DEBUG 15944 --- [calculatorApp] [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.servlet.resource.NoResourceFoundException: No static resource WEB-INF/jsp/index.jsp.] 2024-12-08T21:39:43.174-05:00 DEBUG 15944 --- [calculatorApp] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Exiting from "FORWARD" dispatch, status 404 2024-12-08T21:39:43.175-05:00 DEBUG 15944 --- [calculatorApp] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed 404 NOT_FOUND 2024-12-08T21:39:43.175-05:00 DEBUG 15944 --- [calculatorApp] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : "ERROR" dispatch for GET "/error", parameters={} 2024-12-08T21:39:43.176-05:00 DEBUG 15944 --- [calculatorApp] [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse) 2024-12-08T21:39:43.181-05:00 DEBUG 15944 --- [calculatorApp] [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, text/html;q=0.8] 2024-12-08T21:39:43.184-05:00 DEBUG 15944 --- [calculatorApp] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Exiting from "ERROR" dispatch, status 404
项目目录结构
calculatorAppWeb(Maven子模块) ├── src │ ├── main │ │ ├── java │ │ │ └── main │ │ │ └── controller │ │ │ └── MainController.java │ │ ├── resources │ │ │ └── application.properties │ │ └── webapp │ │ └── WEB-INF │ │ └── jsp │ │ └── index.jsp └── pom.xml
控制器代码
package main.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.ui.Model; @Controller public class MainController { @GetMapping("/") public String displayMainPage(Model model) { model.addAttribute("message", "Hello, welcome to the JSP web application!"); return "index"; // Maps to /WEB-INF/jsp/index.jsp } }
application.properties配置
spring.application.name=calculatorApp spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp logging.level.org.springframework.web=DEBUG
Maven子模块pom.xml文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <!--The identifier of the parent POM--> <parent> <groupId>com.example</groupId> <artifactId>calculatorApp</artifactId> <version>1.0</version> </parent> <artifactId>calculatorAppWeb</artifactId> <version>1.0</version> <name>calculatorAppWeb</name> <description>Calculator Application, Controller, and View</description> <dependencies> <!-- Spring Web --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>6.1.13</version> </dependency> <!-- JSTL for JSP support --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- Apache Tomcat Embed for JSP support --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <version>10.1.33</version> </dependency> </dependencies> </project>
已尝试的排查措施
- 尝试改用web.xml替代application.properties配置Spring MVC,出现构建问题后恢复原方案;
- 将Spring MVC、javax及Tomcat依赖移至Maven父BOM,无效果;
- 最初使用Spring MVC库绑定映射未成功,改用Spring Framework实现后,Tomcat可启动并能访问localhost:8080,但JSP仍返回404。
内容的提问来源于stack exchange,提问作者CheeseBalls
相关产品推荐
相关产品推荐

