我正在尝试在我的 Spring 引导应用程序中添加一个 jsp 页面。我的问题是,每次我尝试转到该页面时,我都会看到以下内容:



白标签错误页

此应用程序没有对/error的显式映射,因此您可以将其视为回退。

Tue Apr 26 16:10:15 IRDT 2022
有一个意外错误(type=Not Found, status=404)。
JSP文件[/WEB-INF/JSP/home.jsp]未找到

我已经在application.properties中添加了前缀和后缀:

spring.view.prefix: /WEB-INF/jsp/
spring.view.suffix: .jsp
这是我的控制器类:
@Controller
@RequestMapping("/")
public class HomeController {
@GetMapping("/")
public String index(){
return "home";
}
}

我在控制器类中有一个警告:

Cannot resolve MVC view 'home'

我的项目文件夹图片

My Application class:

@SpringBootApplication
public class OnlineShopApplication {
public static void main(String[] args) {
SpringApplication.run(OnlineShopApplication.class, args);
}}

和home.jsp:

<h1> hello world from jsp</h1>

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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.13</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>largesize.shop.app</groupId>
<artifactId>OnlineShop</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>OnlineShop</name>
<description>Online shopping web application</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

似乎您缺少名为home的模板。您需要创建此模板并将其存储在srcmainresourcestemplates下模板是一个html文件,您将从spring向其返回响应。应该是这样的:

project-directory
->src
->main
->java->com->...etc
->resources
->static
->templates
home.html
other_file.html
application.properties

->test

也停止使用特定的版本。使用:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

也不支持回滚到TomCat 9作为10。

<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>9.0.19</version>
</dependency>

你可以从MVN Repository

获得这些依赖项

修改以下内容:

spring.view.prefix: /WEB-INF/jsp/
spring.view.suffix: .jsp

:

spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp

当我打开项目时,我打开了包含项目的文件夹,它比主项目文件夹高一个卷。

相关内容

  • 没有找到相关文章

最新更新