为什么JSP文件没有Map到Spring控制器?

mefy6pfw  于 2023-06-03  发布在  Spring
关注(0)|答案(1)|浏览(163)

我刚刚开始学习spring,我已经添加了一个jsp文件到我的项目中,但是控制器不能Map到它。我的控制器在 src --> main --> java --> com.example.todo --> controller -->UserController.java中,我的jsp文件在src --> main --> resources --> WEB-INF --> jsp -->***index.jsp中。***我正在观看一个视频,其中有人将jsp文件添加到spring项目,控制器Map它。但在我的情况下,它不是这样做。

这是我的代码-

TodoApplication.java

package com.example.todo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TodoApplication {

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

}

UserController.java

package com.example.todo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
public class UserController {

    @GetMapping("/index")
    public String ind(){
        return "index";
    }

    @GetMapping("/index-html")
    @ResponseBody
    public String indexHTML(){
        StringBuilder body = new StringBuilder();

        body.append("<!DOCTYPE html>");
        body.append("<html>");
        body.append("<body>");
        body.append("<h1>HTML page</h1>");
        body.append("<p>This is a random text</p>");
        body.append("</body>");
        body.append("</html>");

        return body.toString();
    }
}

index.jsp

<!DOCTYPE html>

<html>
    <body>
        <h1 style="color: blue;">This is jsp file</h1>
    </body>
</html>

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>3.1.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>todo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>todo</name>
    <description>A To-Do list application</description>
    <properties>
        <java.version>19</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <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>com.h2database</groupId>
            <artifactId>h2</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>

应用.属性

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

spring.datasource.url=jdbc:h2:mem:db

我在Ubuntu 22.04上使用IntelliJ IDEA社区和JDK 19和Spring 3.1。当我后藤localhost:8080/index-html时,它呈现html代码并按预期执行,但它不是;当我转到localhost:8080/index时,就不会出现这种情况。
我还对www.example.com进行了更改application.properties,添加了前缀和后缀,并添加了tomcat-jasper依赖项。
请帮帮我也会很高兴,如果你能提供一个很好的在线资源,学习Spring Boot 。

xuo3flqw

xuo3flqw1#

最好检查创建的jsp是否在正确的路径下

您可以从HowTodoInJava了解Sping Boot

相关问题