java视图未在Spring引导应用程序中呈现
我正在使用Spring工具套装,并创建了新的Spring Starter项目
视图未在我的spring boot应用程序中呈现。我试着在各种网站上搜索,但没有得到任何有效的答案来解决这个问题
我不知道视图的默认位置,但据我在谷歌上搜索的结果,它应该在resources文件夹中
这是密码
主控制器
package com.example;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class MainController {
@GetMapping("/")
public String init() {
return "index";
}
}
应用程序。性质
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
Application class
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
波姆。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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-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>
# 1 楼答案
两天后我终于得到了答案
添加了上述答案建议的依赖项
<dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <scope>provided</scope> </dependency>
二,。找到一个解决方案here
根据这个答案,我创建了一个源文件夹
src/main/webapp
,并在这个源文件夹中添加了/WEB-INF/jsp/index
# 2 楼答案
您需要编译jsp
# 3 楼答案
Spring引导通常不使用
WEB-INF
。将JSP移动到resources/templates
,并从应用程序属性中删除行# 4 楼答案
如果您正在使用thymeleaf,请将您的。资源/模板中的html文件 但是,如果您使用的是jsp视图,请将。src/webapp/WEB-INF/jsp中的jpa文件
用于jsp 依赖关系-----------------------------------------
应用程序。性质