我试图从一个非常简单的 Controller 重定向到位于资源/模板中的jsp页面,但我不想使用ViewResolver java配置,而是想使用应用程序属性前缀和后缀!
Controller
@GetMapping("/")
public String redirectToMainPage() {
return "main-page";
}
这些是我的应用程序属性:
spring.mvc.view.prefix = /templates/
spring.mvc.view.suffix = .jsp
jsp位置是:
/src/main/resources/templates/main-page.jsp
但我总是收到此错误:
白标错误页面 此应用程序没有/error 的显式映射,因此您将其视为后备。
2019 年东部时间 11 月 13 日星期三 13:38:55 出现意外错误(类型=未找到,状态=404)。 没有可用的消息
编辑:我刚刚从 Springinitializr 创建了该项目。下面是重要的部分,其他只是自动生成的依赖项
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>web.site</groupId>
<artifactId>cookBook</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cookBook</name>
<description>Demo project for Spring Boot</description>
<packaging>war</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
感谢您提前提供的帮助!
最佳答案
请添加依赖项:
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
需要嵌入的 jasper 依赖项来渲染 jsp 文件。另外,当您使用 jsp
作为 View 渲染器时,请将您的打包更改为 war
。
关于java - Spring Boot Controller 重定向到/模板jsp页面不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58836412/