Springboot2集成Jsp报404问题排查

小Demo项目中Springboot集成Jsp,访问url出现404,如下

 百度了一下,但是大多数都是复制粘贴,和我的问题原因不一致,具体集成结构此处不详细叙述(参考:springboot2.0整合jsp_穷少年的博客-CSDN博客_springboot2整合jsp

我项目问题原因:

1. 项目采用多模块,对应的项目路径为父项目的根路径,导致找不到url

解决办法

a. 修改启动配置,设置项目工作路径为子模块路径 (推荐)

b. 添加配置bean,修改工作路径

package pri.hzhu.file.config;


import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.File;

/**
 * @description:
 * @author: pp_lan
 * @date: 2022/1/4 20:05
 */
@Configuration
public class GlobalConfig {
    @Bean
    public WebServerFactoryCustomizer<TomcatServletWebServerFactory> customizer() {
        return (factory) -> {
            factory.addContextCustomizers((context) -> { //模块中webapp相对路径
                //下面是子项目的module名称,而不是你修改的application name这点需要注意;
                String relativePath = "file-system/src/main/webapp";
                File docBaseFile = new File(relativePath); // 如果路径不存在,则把这个路径加入进去
                if (docBaseFile.exists()) {
                    context.setDocBase(docBaseFile.getAbsolutePath());
                }
            });
        };
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值