Spring 引导未映射 JSP 页 Intellij

Spring boot not mapping JSP pages Intellj

我正在尝试从控制器映射 index1.jsp。

这是我的控制器方法代码:

    @GetMapping({"/", "/hello"})
public String hello(Model model, @RequestParam(value="name", required=false, defaultValue="World") String name) {
    model.addAttribute("name", name);
    return "index1";
}

这是WebConfiguration:

    @Configuration
public class WebConfiguration   implements WebMvcConfigurer {

    @Bean
    public UrlBasedViewResolver viewResolver() {
        UrlBasedViewResolver resolver
                = new UrlBasedViewResolver();
        resolver.setPrefix("/WEB-INF/jsp/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
    }
}

这是index.jsp

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Quiz</title>
</head>
<body>
 <h2>Hello ${name}!</h2>
</body>
</html>

这是application.properties:

spring.application.name=quiz
server.port=8090
server.error.whitelabel.enabled=false
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

这是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.4.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <packaging>war</packaging>
    <groupId>com.test</groupId>
    <artifactId>quiz</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>quiz</name>
    <description>Quiz app</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>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
     </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

我收到错误:HTTP 状态 500 – 内部服务器错误

这是来自控制台:

org.thymeleaf.exceptions.TemplateInputException: Error resolving template [index1], template might not exist or might not be accessible by any of the configured Template Resolvers
...
org.thymeleaf.exceptions.TemplateInputException: Error resolving template [index1], template might not exist or might not be accessible by any of the configured Template Resolvers

我认为它会查找模板文件夹,而不是 WEB-INF/jsp。如果我尝试从模板映射 html 文件,它就可以工作。但是 JSP 会产生问题。

这是应用程序的结构:

Link of picture of project structure

如果您想使用 Spring boot + JSP 而不是 thymeleaf,则需要执行以下步骤。

  1. 从 pom.xml
  2. 移除 thymeleaf 依赖
  3. 在 pom.xml 中添加以下依赖项(Jasper 编译 JSPs)
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>

    <!-- Need this to compile JSP -->
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>

参考https://mkyong.com/spring-boot/spring-boot-hello-world-example-jsp/了解更多信息。