gpt4 book ai didi

spring boot jsp文件无法查看

转载 作者:行者123 更新时间:2023-12-01 06:09:58 24 4
gpt4 key购买 nike

我正在使用 Spring Boot 应用程序。我已经为它设置了 MvcConfig 类并将 tomcat-embed-jasper 和 jSTL 依赖项添加到 pom.xml。但是,我无法在 'WEB-INF' 文件夹中查看我的 jsp 文件,我会收到 404 错误(白标错误页面)。我已经设置了 Application.properties。这是我的 application.properties:

#
## View resolver
#
spring.view.prefix: /WEB-INF/jsp/
spring.view.suffix: .jsp

这是我的 MvcConfig 类:

package com.goodvideotutorials.spring.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {

@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("home");
}

}

这是我的 home.jsp:

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Up and running with Spring Framework quickly</title>
</head>
<body>
<h2>Hello, world!</h2>
</body>
</html>

它被插入到 src > main > webapp > WEB-INF > jsp > home.jsp

我已将这些依赖项添加到 pom.xml:

<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>

这是我的 Application.java 类:

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Application.class, args);
}
}

和我的 Controller 类:

@Controller
public class RootController {

// @RequestMapping("/")
// public String home() {
//
// return "home";
//
// }

}

无论如何,如果我在 Controller 中注释代码并且不使用 MvcConfig,它就不起作用。如果我注释该代码并使用 MvcConfig 类,则效果不佳。这是网址:本地主机:8080

我刚刚测试了很多东西,但它显示“白标错误页面”而不是 JSP。我还在 JEE 环境中安装了 Tomcat 服务器。这会导致问题吗?

最佳答案

只是我的 2 美分。

对我来说,我在服务器日志中收到以下警告:

WARN  : ResourceHttpRequestHandler : Path with "WEB-INF" or "META-INF": [WEB-INF/jsp/welcome.jsp]

并且显示了白标错误页面,为了修复它,我修改了我的 JSP 位置


src/main/webapp/WEB-INF/jsp/welcome.jsp


src/main/webapp/templates/jsp/welcome.jsp

为什么它对我有用?

Spring 文档明确指出 ResourceHttpRequestHandler将拒绝任何包含 WEB-INF 的 URL或 META-INF .

ResourceHttpRequestHandler doc link

From DOCS: Identifies invalid resource paths. By default rejects: Paths that contain "WEB-INF" or "META-INF"



因此,一旦我用模板替换了 WEB-INF 文件夹名称,它就开始工作。

注意:
我还为我的内部 View 解析器配置了所需的前缀和后缀。
我正在使用 spring-boot-starter-parent - 2.1.2.RELEASE

关于spring boot jsp文件无法查看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33751668/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com