V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iv8d
V2EX  ›  Java

springboot 子模块启动无法访问子模块 webapp 目录下静态文件

  •  1
     
  •   iv8d · 62 天前 · 1239 次点击
    这是一个创建于 62 天前的主题,其中的信息可能已经有所发展或是发生改变。

    springboot 子模块启动无法访问子模块 webapp 目录下静态文件。

    项目 A ,子模块 a ,子模块 a 下的 src/main/webapp 目录有 login.html 文件。 启动子模块 java -cp 子模块 a com.xxxMain ,通过 localhost:8080/login.html 无法访问到提示 404 。这种可能是什么原因。

    如果 login.html 在单一层次的项目中,能正确访问到。只有作为子模块启动时无法访问呢。

    13 条回复
    iv8d
        1
    iv8d  
    OP
       62 天前
    补充,<spring-boot.version>2.7.6</spring-boot.version>,使用 Spring Initializer 创建,只加了 web 依赖。
    0x9527
        2
    0x9527  
       62 天前 via Android
    看下打出来的包有没有这个文件,可能 maven 配置的 resources 没有包括进来
    iv8d
        3
    iv8d  
    OP
       62 天前
    @0x9527 在 idea 里用 springboot 方式也就是 java -cp mainClass 方式启动的,没使用 maven 打包。
    yidinghe
        4
    yidinghe  
       62 天前
    运行时的静态文件默认应该放在 `target/classes/public` 目录下,src 目录不在运行时的 classpath 中。你不要按 maven 的默认路径来放,而要按照 springboot 的规范。
    yidinghe
        5
    yidinghe  
       62 天前   ❤️ 1
    #4 补充下,src/main/webapp 这个目录是 Maven Webapp 的规范,是打包成 war 文件用的,然后将 war 文件放在 Tomcat 等 Servlet 容器中运行。现在几乎没人这么做了,而是直接用 SpringBoot ,因为 springboot-starter-web 自带了 Servlet 容器。按照 SpringBoot 的规范,静态文件要放在 src/main/resources/public 目录下,编译后输出到 target/classes/public 。我不知道你能看懂没,如果有疑问,请将上面的内容发给大语言模型,让它详细跟你解释。
    kaizceo8
        6
    kaizceo8  
       61 天前
    <resource>
    <directory>src/main/webapp</directory>
    <targetPath>META-INF/resources</targetPath>
    <includes>
    <include>**/**</include>
    </includes>
    </resource> 试试
    kaizceo8
        7
    kaizceo8  
       61 天前
    ```xml
    <resource>
    <!-- 指定 resources 插件处理哪个目录下的资源文件 -->
    <directory>src/main/webapp</directory>
    <!--注意此次必须要放在此目录下才能被访问到 -->
    <targetPath>META-INF/resources</targetPath>
    <includes>
    <include>**/**</include>
    </includes>
    </resource>

    ```
    kaizceo8
        8
    kaizceo8  
       61 天前
    不能贴代码吗
    Chinsung
        9
    Chinsung  
       61 天前
    随便写个类访问,打印下 classpath 和 resourcepath 这些,然后看下你那个 html 在不在下面就行了,不在又想强行打包过去,要么 resource 配置下,要么找个 maven 插件去打包。
    话说为什么不先用 maven 命令编译完再去执行,难道你最终打包也不用 maven 打包而是把 java 文件丢到服务器上去用这种命令执行吗
    iv8d
        10
    iv8d  
    OP
       55 天前
    @yidinghe 您好,你回复的确实很明白。打 war 包后,目录结构如下:
    /WEB-INF/classes/* # classpath 内容
    / # 静态内容
    按理不管内部容器,外部容器,都能直接访问到/静态对应的静态内容吧。
    iv8d
        11
    iv8d  
    OP
       55 天前
    @yidinghe 以子模块方式运行的 boot 项目无法访问到/静态资源,但是独立项目(非多模块)的运行起来确能访问。
    iv8d
        12
    iv8d  
    OP
       55 天前
    结帖,经过多日测试,idea 启动项目时选中 working directiory 为当前模块名即可,否则无法访问到!!
    yidinghe
        13
    yidinghe  
       55 天前
    @iv8d #11 现在是看 SpringBoot 源码的最佳时机
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1780 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 00:29 · PVG 08:29 · LAX 17:29 · JFK 20:29
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.