首页 > 解决方案 > Spring Boot 2.4.3 无法识别我的“.jsp”模板

问题描述

我正在用 Eclipse 中的 Spring MVC 和其他依赖项、安全性等创建一个简单的 Spring Boot 项目。为了测试应用程序,我创建了一个测试 jsp 页面、一个 jsp 索引并通过“application.properties”文件配置了前缀和后缀。

我没有使用嵌入式服务器,只是在 Eclipse 中创建了一个 Tomcat 9.0 服务器并将其添加到我的项目中。因此,要运行该项目,我只需“右键单击”根目录并选择“运行方式 --> 在服务器上运行”。

问题是:当我启动应用程序时,Spring 识别出我的“index.jsp”页面并重定向到我完美配置的 URL,但是找不到应该在该 URL 中显示的 jsp 页面(404 错误)。

一些代码:

目录结构图片

应用程序属性

spring.datasource.url=jdbc:mysql://localhost:3306/employee_directory?useSSL=false&serverTimezone=UTC
spring.datasource.username=hbstudent
spring.datasource.password=hbstudent

spring.security.user.name=admin
spring.security.user.password=admin

spring.mvc.view.prefix=/WeatherProject/src/main/resources/templates/
spring.mvc.view.suffix=.jsp

logging.level.org.springframework=TRACE
logging.level.com=TRACE

用户控制器.java

package com.lvaqueiroworks.DemoWeatherProject_1.controller;

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

@Controller
@RequestMapping("/user")
public class UserController {

    @GetMapping("/list")
    public String listUsers() {
        return "list-users";
    }
    
}

列表用户.jsp

<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <p>Hii</p>
    </body>
</html>

索引.jsp

<% response.sendRedirect("user/list"); %>

WeatherProjectApplication.java(主文件,但不用于运行应用程序)

package com.lvaqueiroworks.WeatherProject;

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

@SpringBootApplication
public class WeatherProjectApplication {

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

}

标签: javaspringspring-bootspring-mvcjsp

解决方案


使用@RestController您的注释UserController


推荐阅读