当前位置: 首页 > news >正文

Completed 404 NOT_FOUND,Whitelabel Error Page

写在前面,可以直接使用Ctrl+F搜“解决方法”,有两个处理方法。

一、背景

SpringBoot+JSP,找不到jsp文件,后台日志不报错,前台页面显示“Whitelabel Error Page”。

Whitelabel Error Page

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

Tue Aug 30 14:45:24 CST 2022

There was an unexpected error (type=Not Found, status=404).

二、问题描述

基于SpringBoot结合JSP搭建一个示例模块,回味一下历史的味道,网络搜索“springboot jsp”,结果是这样的。

 好的,我也不是想在新项目中使用jsp,只是,假如,或者,如果,也许,可能,有一个老的项目,需要维护,或者,需要二次开发,或者需要重构,那,还是看看吧。

按照网上搜到的资料,参考:

https://www.jianshu.com/p/4d9315f83372https://www.jianshu.com/p/4d9315f83372

很详细,一步一步操作完成后,就是打不开页面,报错404。

三、问题处理

神奇了,也没啥好配置的,该配置的都配置过了,该写的都写了。

1. 检查配置

例如:

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

个人认为有影响的都设置过了,包括:

再找其他资料,基本都差不多,各种尝试,就是不行. 

挣扎过的操作(玄学):

1. 修改application.xml为application.yml,

2.修改包路径由三级变成两级,

3.修改webapp目录到WEB-INF目录,

4.修改jsp目录到src/main目录下,

5.将webapp目录放到工程根目录,

6.更换maven为idea自带的maven,

7.设置server/tomcat/basedir,

8.设置server/servlet/context-path。

依然404。

 2. 检查日志

application.xml中设置debug = true,能看到调试日志。于是发现:

2022-08-30 11:21:10.848 DEBUG 3332 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : GET "/", parameters={}
2022-08-30 11:21:10.848 DEBUG 3332 --- [nio-8080-exec-5] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.octonary.spring.boot.jsp.controller.IndexController#index(Model)
2022-08-30 11:21:10.848 DEBUG 3332 --- [nio-8080-exec-5] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, application/xhtml+xml, image/avif, image/webp, image/apng, application/xml;q=0.9, application/signed-exchange;v=b3;q=0.9, */*;q=0.8]
2022-08-30 11:21:10.848 DEBUG 3332 --- [nio-8080-exec-5] o.s.web.servlet.view.JstlView            : View name 'index', model {bodyContent=Hello, spring boot & jsp.}
2022-08-30 11:21:10.848 DEBUG 3332 --- [nio-8080-exec-5] o.s.web.servlet.view.JstlView            : Forwarding to [index]
2022-08-30 11:21:10.848 DEBUG 3332 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : "FORWARD" dispatch for GET "/index", parameters={}
2022-08-30 11:21:10.849 DEBUG 3332 --- [nio-8080-exec-5] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped to ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/], ServletContext [/]]
2022-08-30 11:21:10.852 DEBUG 3332 --- [nio-8080-exec-5] o.s.w.s.r.ResourceHttpRequestHandler     : Resource not found
2022-08-30 11:21:10.852 DEBUG 3332 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : Exiting from "FORWARD" dispatch, status 404
2022-08-30 11:21:10.852 DEBUG 3332 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : Completed 404 NOT_FOUND
2022-08-30 11:21:10.852 DEBUG 3332 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : "ERROR" dispatch for GET "/error", parameters={}
2022-08-30 11:21:10.852 DEBUG 3332 --- [nio-8080-exec-5] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse)
2022-08-30 11:21:10.855 DEBUG 3332 --- [nio-8080-exec-5] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, text/html;q=0.8]
2022-08-30 11:21:10.855 DEBUG 3332 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : Exiting from "ERROR" dispatch, status 404

3. springboot为什么找不到jsp文件

继续分析,请求到了资源,但是在找jsp文件的时候没找到,为啥?

尝试写了个测试类,在浏览器中能正常访问

package com.octonary.spring.boot.jsp.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @GetMapping("/test")
    public String test() {
        return "test";
    }
}

那肯定是找不到jsp了,已经设置了jsp在prefix: /pages/目录,为啥还找不到呢,那我写个绝对目录试下,windows上使用D:/开头,报错,需要以/开头并以/结尾,于是设置成:/D:/XXX/XX/XX/XX/webapp/,还是不行。

再找原因,发现了一个解决方法

springboot启动项目找不到jsp文件_胸大的请先讲的博客-CSDN博客_springboot找不到jsp将springboot_jspdemo项目从github上下载到本地后,启动项目找不到jsp经过在网上查找花了半个多小时才知道没有给项目设置根目录:点击Edit Configurations打开环境(Environment)设置 Working directory为项目根目录(项目名称),点击确定。重新打开项目再次访问,可以看到成功找到了jsp页面参考:https:...https://blog.csdn.net/qq_42428264/article/details/97616175

这个方法可以解决问题,但是我在想,这是什么原因,为什么前面看了几个教程,都不需要设置这个工作目录?

4. 找源码工程

刚好去码云上看一个材料,想起来这个会不会是由于某种设置上的问题,于是想到在码云上下载一些源码来检查下是否需要进行工作目录的设置。

找到一个可执行的源码,可以直接执行,也没什么内容,就是一个src一个pom,惊呆了。springboot-jsp: 嵌入式tomcat使用jsphttps://gitee.com/sherwinliang/springboot-jsp

5.tomcat工作目录到底是什么

那为什么,我新建的工程却需要设置工作目录,而下载的示例源码不需要呢,于是,又开始玄学了,每个java类和pom文件以及application文件都换成一样的,依然是我创建的工程404,示例工程200,这到底是哪里出了问题,干脆,我的工程直接把源码工程引入进来试试,依然是404,这就神奇了,这个context目录到底是什么,于是,在controller的方法里面加了一些日志

System.out.println(ApplicationContext.CLASSPATH_ALL_URL_PREFIX);
//        System.setProperty("user.dir","D:\\MyData\\Code\\TestCollection\\springboot-jsp");
System.out.println(System.getProperty("user.dir"));
System.out.println(System.getProperty("user.dir").replace("bin", "webapps"));
System.out.println(request.getSession().getServletContext().getRealPath("/"));
System.out.println(request.getSession().getServletContext().getContextPath());
System.out.println(request.getSession().getServletContext().getServerInfo());
System.out.println(request.getSession().getServletContext().getServletContextName());
System.out.println(this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath().split("/WEB-INF")[0]);
System.out.println(Thread.currentThread().getContextClassLoader().getResource(""));
System.out.println(this.getClass().getResource("/"));

两个idea一执行,打印出来的日志,区别出来了。我创建的工程,有父子目录,父目录是一个空的啥都没有的工程,子目录才是这个工程,而示例工程是idea直接打开的。

1. 如果把jsp文件放到生成的temp临时目录中,浏览器是能正常打开的,不会404。

2. 如果在父目录下新建src/main/webapp目录,然后把jsp文件放入这个目录,也不会404.

问题找到了,那怎么解决呢,怎么设置taomcat的这个工作目录呢。

6.自定义springboot内置tomcat相关配置项

查看如下文章:springboot找不到jsp文件_qq_1473179505的博客-CSDN博客接下来演示如何运行jsp在部署好了项目以后,为了运行jsp,我们需要先导入依赖<!-- servlet 依赖包 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-ap...https://blog.csdn.net/qq_29042647/article/details/96974406

按照文中的解决方法处理后,问题解决。

不过,这里有个问题,就是在找到这个问题的原因之前,按照其他的文章设置了一个springboot的scanBasePackages 配置项,导致这个设置tomcat的配置项没有没spring加载到,没有执行,所以配置没生效。

package com.octonary.spring.boot.jsp;

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

@SpringBootApplication(scanBasePackages = "com.spring.boot.jsp.controller")
public class SpingBootJspApplication {

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

}

如果document root设置后,会有相关日志:

2022-08-30 19:29:51.980 DEBUG 11108 --- [           main] .s.b.w.e.t.TomcatServletWebServerFactory : Document root: E:\x\y\z\spring-boot-jsp\src\main\webapp

没有设置的话,是这样的日志:

2022-08-30 19:21:50.987 DEBUG 25852 --- [           main] .s.b.w.e.t.TomcatServletWebServerFactory : None of the document roots [src/main/webapp, public, static] point to a directory and will be ignored.

四、总结

正常情况,如果是独立工程的话,会按照当前工程的根目录去找src/main/webpp目录,参考上面的日志,还有public和static目录,但是如果有父子目录的话,可能会从父目录开始找,而不是当前工程所在的目录,所以需要自定义工作目录,不管是在idea中设置根目录还是代码中设置根目录,都能解决问题。

后续如果还有其他自定义内置tomcat相关配置的话,也可以参考。

附自定义代码

package com.octonary.spring.boot.jsp;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.AbstractServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.File;

@Configuration
public class TomcatConfig {
    @Value("${tomcat.doc.root}")
    private String docRoot;

    @Value("${tomcat.doc.default}")
    private String docDefault;

    @Bean
    public AbstractServletWebServerFactory embeddedServletContainerFactory() {
        TomcatServletWebServerFactory tomcatServletWebServerFactory = new TomcatServletWebServerFactory();
        File file = new File(docDefault);
        if (!file.exists()) {
            file = new File(docRoot + File.separator + docDefault);
            if (file.exists()) {
                tomcatServletWebServerFactory.setDocumentRoot(file);
            }
        }

//        TomcatServletWebServerFactory tomcatServletWebServerFactory = new TomcatServletWebServerFactory();
//        tomcatServletWebServerFactory.setDocumentRoot(new File(rootDoc));
        return tomcatServletWebServerFactory;
    }

    //ConfigurableServletWebServerFactory->AbstractServletWebServerFactory
//    @Bean
//    public WebServerFactoryCustomizer<AbstractServletWebServerFactory> webServerFactoryCustomizer() {
//        return new WebServerFactoryCustomizer<AbstractServletWebServerFactory>() {
//            @Override
//            public void customize(AbstractServletWebServerFactory factory) {
                factory.setPort(8081);
                factory.setDocumentRoot(new File("D:\\MyData\\Code\\TestCollection\\springboot-jsp\\src\\main\\webapp"));
//                System.out.println("document root: " + factory.getDocumentRoot());
                factory.setDocumentRoot(new File("springboot-jsp/src/main/webapp"));
//                factory.setDocumentRoot(new File("spring-boot-jsp/src/main/webapp"));
//                System.out.println("document root: " + factory.getDocumentRoot());
//            }
//        };
//    }
}
spring:
  mvc:
    view:
      prefix: /pages/
      suffix: .jsp
server:
  port: 8089
#  tomcat:
#    basedir: /springboot-jsp/src/main/webapp/
#  servlet:
#    context-path: /springboot-jsp/src/main/webapp/

tomcat:
  doc:
    default: src/main/webapp
    root: springboot-jsp
目录生效,启动时会有如下日志:
​​​​​​​2022-08-30 15:42:33.323  INFO 13588 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8089 (http) with context path '/MyData/Code/TestCollection/springboot-jsp/src/main/webapp'
2022-08-30 15:43:23.240  INFO 25184 --- [           main] o.s.b.a.w.s.WelcomePageHandlerMapping    : Adding welcome page template: index

相关文章:

  • 微信公众号多题库查题系统
  • Vue事件
  • TCP/IP协议专栏——ARP详解——网络入门和工程维护必看
  • python 字符串类型
  • 【canvas】-- H5新增标签canvas的简单用法
  • C#实现根据字体名称获取字体文件名
  • Android开发-视图view讲解
  • Vue样式绑定
  • 机器学习分类
  • 计算机毕业设计ssm水果库存管理系统30q2h系统+程序+源码+lw+远程部署
  • 小红书和中兴笔试
  • JavaWeb之Servlet-----(2)
  • Java中如何实现一个“读写锁“呢?
  • 线程池简介说明
  • PostgreSQL执行计划介绍
  • 【Leetcode】101. 对称二叉树
  • JavaScript-如何实现克隆(clone)函数
  • [case10]使用RSQL实现端到端的动态查询
  • 08.Android之View事件问题
  • 2019年如何成为全栈工程师?
  • canvas 高仿 Apple Watch 表盘
  • CSS实用技巧干货
  • ES6系列(二)变量的解构赋值
  • IIS 10 PHP CGI 设置 PHP_INI_SCAN_DIR
  • iOS筛选菜单、分段选择器、导航栏、悬浮窗、转场动画、启动视频等源码
  • Linux后台研发超实用命令总结
  • 包装类对象
  • 开放才能进步!Angular和Wijmo一起走过的日子
  • 面试题:给你个id,去拿到name,多叉树遍历
  • 强力优化Rancher k8s中国区的使用体验
  • 如何进阶一名有竞争力的程序员?
  • 如何利用MongoDB打造TOP榜小程序
  • 什么软件可以提取视频中的音频制作成手机铃声
  • 使用common-codec进行md5加密
  • 小程序01:wepy框架整合iview webapp UI
  • 在 Chrome DevTools 中调试 JavaScript 入门
  • ​Base64转换成图片,android studio build乱码,找不到okio.ByteString接腾讯人脸识别
  • ​决定德拉瓦州地区版图的关键历史事件
  • (1)(1.13) SiK无线电高级配置(六)
  • (52)只出现一次的数字III
  • (LeetCode 49)Anagrams
  • (pytorch进阶之路)扩散概率模型
  • (阿里巴巴 dubbo,有数据库,可执行 )dubbo zookeeper spring demo
  • (二十一)devops持续集成开发——使用jenkins的Docker Pipeline插件完成docker项目的pipeline流水线发布
  • (蓝桥杯每日一题)平方末尾及补充(常用的字符串函数功能)
  • (一)为什么要选择C++
  • (译)计算距离、方位和更多经纬度之间的点
  • (原創) 人會胖會瘦,都是自我要求的結果 (日記)
  • (转)scrum常见工具列表
  • (转)关于多人操作数据的处理策略
  • (转)一些感悟
  • ***php进行支付宝开发中return_url和notify_url的区别分析
  • .net 7 上传文件踩坑
  • .net core开源商城系统源码,支持可视化布局小程序
  • .NET/C# 解压 Zip 文件时出现异常:System.IO.InvalidDataException: 找不到中央目录结尾记录。