java - 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);
}
}
解决方案
使用@RestController
您的注释UserController
推荐阅读
- android - 在 SearchView 中设置 queryHint 的文本大小
- r - 如何将数据帧中的字符值与列表的字符对应起来
- c# - VS19 .NET 目标框架锁定在 2.0、3.0 和 3.5
- c - “有效程序”的定义是什么?
- flutter - 将新项目插入到列表的零索引后重建 ScrollablePositionedList
- c++ - 让 sphinx 接受无效的 C++ 签名
- c# - Linq MaxBy 与所有元素?
- php - 如何缩短通过 php 代码检索到的一个图像源的使用率
- assembly - EMU 8086 需要更长的打印时间
- javascript - javascript 如果父级中的所有元素都有类