Spring Boot,JSP 文件作为 View 在 IntelliJ 中运行时未加载

标签 spring jsp spring-mvc intellij-idea spring-boot

我在 intelliJ 中创建了一个简单的 Spring Boot Web 应用程序。我在/src/main/resources/templates/文件夹中放置了一个简单的 .jsp 文件,其中包含一些基本的 HTML。

我试图在 Controller 中返回它,但出现此错误;

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 ;
@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 中的 View 路径更改为;
classpath:/templates/但这也没有任何区别。

最后,这是我的项目结构;

Project File Structure

运行应用程序时,我只是使用 IntelliJ 中的“运行”选项。

最佳答案

我最近经历了以下依赖项的相同情况:-

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>

如果我将“范围”更改为“默认”,使用 Intellij 运行应用程序,它工作正常。

但是如果我想将“范围”保持为“提供”,我必须使用命令行(终端)并执行以下操作:-
mvn clean spring-boot:run

这个对我有用。

关于Spring Boot,JSP 文件作为 View 在 IntelliJ 中运行时未加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39408610/

相关文章:

java - 如何从请求对象获取客户端 url?

java - 无法访问 Spring Boot 的请求映射

java - Spring 3中保存内部对象id

java - 我不明白为什么不执行 post 方法的原因

java.io.NotSerializableException 和实现 Serializable

java - 捕获 Spring MVC 最大上传大小错误

java - 具有 Spring Security 的 Spring MVC Controller 继承

java - Spring+JPA+Hibernate : persist is updating the entity surprisingly. 请看详细

spring - 为 Spring Validator 实现编写 JUnit 测试

java - 将请求从 spring controller 重新发送到 spring controller