为什么在Spring中找不到JSP ?文件存在



在一个非常基本的应用程序中,我有如下的sayHelloController.java

@Controller
public class SayHelloController {

@RequestMapping("say-hello-jsp")
public String sayHelloJsp() {
return "sayHello";
}
}

与以下application.properties:

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
logging.level.org.springframwork=debug

和sayHello.jsp:

<html>
<head>
My first JSP file!
</head>
<body>
<h1>Hello</h1>
<p>
This is the text we want to show <br/>
<p>
</body>
</html>

当我去http://localhost:8080/say-hello-jsp,我得到一个404和日志给我:

: GET "/say-hello-jsp", parameters={}
: Mapped to com.in28minutes.springboot.myfirstwebapp.hello.SayHelloController#sayHelloJsp()
: Selected 'text/html' given [text/html, application/xhtml+xml, image/avif, image/webp, image/apng, application/xml;q=0.9, application/signed-exchange;v=b3;q=0.9, */*;q=0.8]
: View name 'sayHello', model {}
: Forwarding to [/WEB-INF/jsp/sayHello.jsp]
: "FORWARD" dispatch for GET "/WEB-INF/jsp/sayHello.jsp", parameters={}
: Mapped to ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/], ServletContext [/]]
: "Path with "WEB-INF" or "META-INF": [WEB-INF/jsp/sayHello.jsp]"
: Resource not found

没有意义,因为文件存在,证明:

macbook-air:jsp gerald$ pwd
/Users/gerald/IdeaProjects/myfirstwebapp/src/main/resources/META-INF/resources/WEB-INF/jsp
macbook-air:jsp gerald$ ls
sayHello.jsp

另外,我在pom.xml

中添加了jasper作为依赖项。
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>

为什么没有检索到文件?

在这里找到了答案:https://youtrack.jetbrains.com/issue/IDEA-158125/Spring-boot-run-configuration-does-not-find-resources-in-META-INF,谢谢Andy Wilkinson:

问题是Jasper被标记为在pom中提供的,所以它是不在类路径上。我相信已经有一个IDEA问题了这个问题。它导致Spring Boot进入一个代码路径不启用各种与jsp相关的部分。你可以避免它把"提供"从你的舞会中移除,因为它是唯一的在构建将部署到servlet的war文件时是必需的容器。

我已经删除了提供的,运行了mvn清洁(它不工作只是重新运行项目),然后它工作了。

奇怪的是,这个基本问题在6年后仍然存在于IntelliJ(链接来自2016年)

最新更新