首页 > 解决方案 > Spring Boot控制器不返回jsp页面

问题描述

编辑:下面是 LatherupApplication 应用程序类:

package latherup.com.latherup;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication(scanBasePackages = "latherup.com")
public class LatherupApplication extends SpringBootServletInitializer{

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(LatherupApplication.class);
    }
    public static void main(String[] args) {
        SpringApplication.run(LatherupApplication.class, args);
    }

}

下面是我的家庭控制器:

package latherup.com.latherup;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/home")
public class HomeController {
    @GetMapping("/home")
    public String navigateToHomePage() {
        return "home.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.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>latherup.com</groupId>
    <artifactId>latherup</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>latherup</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.release>13</maven.compiler.release>
    </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-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>5.0.0</version>
        </dependency>

        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap-datepicker</artifactId>
            <version>1.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>3.6.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->
        

    </dependencies>

    <build>
        <!--plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins-->
         <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

</project>

下面是 home.jsp :

    <html>
    <head>
        <title>Welcome</title>
        <link href="webjars/bootstrap/3.3.6/css/bootstrap.min.css"
            rel="stylesheet">
    </head>
    <body>
        <div class="container">
            <table class="table table-striped">
                <caption>Your todos are</caption>
                <thead>
                    <tr>
                        <th>Description</th>
                        <th>Target Date</th>
                        <th>Is it Done?</th>
                        <th>Edit</th>
                        <th>Delete</th>
                    </tr>
                </thead>
                <tbody>
                        <tr>
                            <td>Todo 1</td>
                            <td>10/12/2017</td>
                            <td>No</td>
                            <td><a class="btn btn-warning" href="/edit-todo">Edit Todo</a></td>
                            <td><a class="btn btn-warning" href="/delete-todo">Delete Todo</a></td>
                        </tr>
                </tbody>
            </table>
            <div>
                <a class="btn btn-default" href="/add-todo">Add a Todo</a>
                
            </div>
            <script src="webjars/jquery/1.9.1/jquery.min.js"></script>
            <script src="webjars/bootstrap/3.3.6/js/bootstrap.min.js"></script>
        </div>
    </body>
    </html>

控制器返回字符串“home.jsp”而不是页面。我已经添加了所有依赖项,但不确定是什么问题。是依赖版本的问题吗?如果是,我该如何决定如何选择相互兼容的版本。我一直面临这个问题。有时它有效,有时则无效。请指导我,因为我是初学者。

编辑: 下面是项目结构的图像:

应用程序属性:

spring.mvc.view.prefix=/LATHER-UP/jsp/
spring.mvc.view.suffix=.jsp
logging.level.org.springframework.web=INFO

标签: javaspring-bootmaven

解决方案


你已经定义了spring.mvc.view.suffix = .jsp

所以你的控制器不应该home返回home.jsp

@Controller
@RequestMapping("/home")
public class HomeController {
    @GetMapping("/home")
    public String navigateToHomePage() {
        return "home";
    }
}

推荐阅读