SpringBoot访问webapp下面的html等静态资源文件出现404解决方案

Terwer...大约 1 分钟实用技巧经验分享springbootwebapp404

这里的前提是在src/main/目录下(跟src/main/resources同级)建好了webapp目录,并且在webapp下建好了WEB-INF。如图:

image-20220506152354284
image-20220506152354284

在做SpringBoot集成页面的时候,发现不能直接访问页面或者静态资源,都是报404,可以试试在pom.xml的build里面加入如下代码: (不想用jsp,只想用html或者其他的可以采用这种方式)

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
    <!-- 添加访问静态资源文件 -->
    <!-- 代码的作用是让src/main/webapp在编译的时候在resoureces路径下也生成webapp的文件 -->
    <resources>
        <resource>
            <directory>src/main/webapp</directory>
            <targetPath>META-INF/resources</targetPath>
            <includes>
                <include>**/**</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

代码的作用是让src/main/webapp在编译的时候在resoureces路径下也生成webapp的文件

resourece路径下没有META-INF/resources文件夹也可以访问

添加好后,右键maven updae一下项目,重启项目,输入:http://localhost:8080open in new window(我的项目用的是8080端口),发现可以访问

image-20220506152314044
image-20220506152314044
评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v2.14.9