【发布时间】:2017-01-17 10:18:39
【问题描述】:
我在 intelliJ 中创建了一个简单的 Spring Boot Web 应用程序。我在 /src/main/resources/templates/ 文件夹中放置了一个简单的 .jsp 文件,其中包含一些基本的 HTML。
我试图在控制器中返回它,但我收到了这个错误;
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri Sep 09 10:37:46 BST 2016
There was an unexpected error (type=Not Found, status=404).
No message available
我假设 Spring 无法找到 .jsp 文件,但控制台中没有出现其他错误来给我更多信息。
这是我的简单控制器;
@Controller
@RequestMapping("/test")
public class TestController {
@RequestMapping("")
public ModelAndView index() {
return new ModelAndView("test");
}
}
我在 application.properties 文件中包含以下内容;
spring.mvc.view.prefix = /templates/
spring.mvc.view.suffix = .jsp
我的 POM 文件包含以下依赖项;
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
我正在使用带有嵌入式 tomcat 的 Spring Boot。
我尝试将 application.properties 中视图的路径更改为; classpath:/templates/ 但这也没有任何区别。
最后,这是我的项目的结构;
运行应用程序时,我只是使用 IntelliJ 中的“运行”选项。
【问题讨论】:
-
如果我的回答是您的问题的解决方案,那么请在您的问题中添加
IntelliJ标签并将其重命名为“Spring boot,JSP 文件作为视图未在 IntelliJ 中加载” - 这将使这个问题对其他用户更有帮助。 -
/templates!=classpath:/templates/JSP 也不应该在静态资源中,它们应该在 webapp 文件夹中(jsp 文件仅适用于 WAR 打包 afaik)。 -
如果 jsp 文件确实不能在 JAR 文件中工作,那么这不可能是问题的一部分,并且会导致未来的问题,因为我想将此应用程序打包为 jar。
-
请看这个答案IntelliJ IDEA solution
标签: spring jsp spring-mvc intellij-idea spring-boot