当前位置: 首页 > 工具软件 > Web-Return > 使用案例 >

记录一下,关于Spring Boot在写web时初次加载jsp的一个坑。(Path with "WEB-INF" or "META-INF": [WEB-INF/pages/login.jsp])

商畅
2023-12-01

刚开始接触SpringBoot ;

所用编辑器为:IDEA;

想用SpringBoot创建web工程,第一步就遇到了坑,具体如下:

添加webapp文件夹,创建目录/WEB-INF/pages/,将jsp文件放入下方;

在application.yml文件中配置:

spring:
  mvc:
    view:
      prefix: /WEB-INF/pages/
      suffix: .jsp

程序正常启动,在访问时会报出以下警告:

WARN 14836 --- [nio-8080-exec-1] o.s.w.s.r.ResourceHttpRequestHandler     : Path with "WEB-INF" or "META-INF": [WEB-INF/pages/login.jsp]

页面出现一下错误:

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Wed Mar 13 10:37:19 CST 2019
There was an unexpected error (type=Not Found, status=404).
No message available

查看原码,看到后好像是不能有WEB-INF或META-INF的两个文件夹(有点绝望):

protected boolean isInvalidPath(String path) {
        if (!path.contains("WEB-INF") && !path.contains("META-INF")) {
            if (path.contains(":/")) {
                String relativePath = path.charAt(0) == '/' ? path.substring(1) : path;
                if (ResourceUtils.isUrl(relativePath) || relativePath.startsWith("url:")) {
                    if (logger.isWarnEnabled()) {
                        logger.warn("Path represents URL or has \"url:\" prefix: [" + path + "]");
                    }

                    return true;
                }
            }

            if (path.contains("..") && StringUtils.cleanPath(path).contains("../")) {
                if (logger.isWarnEnabled()) {
                    logger.warn("Path contains \"../\" after call to StringUtils#cleanPath: [" + path + "]");
                }

                return true;
            } else {
                return false;
            }
        } else {
            if (logger.isWarnEnabled()) {
                logger.warn("Path with \"WEB-INF\" or \"META-INF\": [" + path + "]");
            }

            return true;
        }
}

然后再百度,找到以下结果,算是解决了问题;

在pom.xml文件添加以下代码,再运行就OK:

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
</dependency>

具体实现还没有弄明白,不过在此记录一下。

 类似资料: