春季启动MVC - HTML文件工作,但JSP不?



我一直在尝试使用JSP作为我正在做的事情的视图,但由于某种原因,我在使用JSP文件时一直得到白标签错误。我使用一个普通的jsp文件,带有一些文本,看看它是否工作,没有特定的映射或任何东西

如果我使用html文件"spring.mvc.view.suffix=.html"属性文件,然后它工作。但是由于某些原因,jsp文件不能工作。

我想使用jsp,所以我可以使用jstl标签用于表单,我不确定如何用html文件做到这一点。

我把我的视图文件放在static文件夹下。

我的属性文件中唯一的东西是db源

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.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springboot.application</groupId>
<artifactId>safespot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>safespot</name>
<description>Chat 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>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>
</dependency>

<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>7.0.0.Final</version>
</dependency>


<!-- JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- To compile JSP files -->
<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>

控制器

@Controller
public class DemoController {
@GetMapping("/")
public String welcome() {
return "index";
}
}

index . jsp

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>

</head>
<body>
Hello World
</body>
</html>

文件夹结构

  1. 如果您将spring引导应用程序作为独立应用程序启动,但不将war部署在现有的tomcat中,则tomcat-embed-jasper依赖项必须not有范围provided,但compile(或runtime),与jstl相同(顺便说一句,范围compile是默认的,所以你可以省略它)

  2. setprefixandpostfix

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

最新更新