【问题标题】:Spring Boot 2.4.3 doesn't recognize my ".jsp" templatesSpring Boot 2.4.3 无法识别我的“.jsp”模板
【发布时间】:2021-05-30 05:29:07
【问题描述】:

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

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

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

一些代码:

Directory Structure Image

application.properties

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

UserController.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";
    }
    
}

list-users.jsp

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

index.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);
    }

}

【问题讨论】:

    标签: java spring spring-boot spring-mvc jsp


    【解决方案1】:

    UserController 中使用@RestController 注释

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-06
      • 2013-06-27
      • 2021-08-16
      • 1970-01-01
      • 2021-09-07
      • 2015-09-25
      • 2019-02-26
      相关资源
      最近更新 更多