springboot访问webapp下的jsp页面

一,项目结构。

这是我的项目结构,jsp页面放在WEB-INF下的page目录下面。

二,file--->Project Structure,确保这两个地方都是正确的,确保Source Roots下面有webapp这个目录(正常来说,应该本来就有,但是我的一开始居然没有,导致后面访问页面404,不过有解决办法后面再说)

三,pom文件导入依赖使得jsp能够生效。

cpp 复制代码
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>

四,yaml配置视图解析器

cpp 复制代码
spring:  
   mvc:
     view:
       prefix: /WEB-INF/page/
       suffix: .jsp

五, 将webapp注册为资源目录

cpp 复制代码
            <!--注册webapp目录为资源目录-->
            <resource>
                <directory>src/main/webapp</directory>
                <targetPath>META-INF/resources</targetPath>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>

这个地方我看其他文档仅仅加上上面这段就可以了,但是我试了会报错,推测是把yaml文件的位置覆盖掉了,那么还需要加上yaml的位置,如下所示。完事之后,(二)里面说的那个问题也会随之解决。

cpp 复制代码
<build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.yaml</include>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <!--注册webapp目录为资源目录-->
            <resource>
                <directory>src/main/webapp</directory>
                <targetPath>META-INF/resources</targetPath>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <includeSystemScope>true</includeSystemScope>
                </configuration>
            </plugin>
        </plugins>
    </build>

六,右侧刷新项目依赖,然后启动。就可以访问到jsp页面了!!!

相关推荐
胚芽鞘68111 分钟前
查询依赖冲突工具maven Helper
java·数据库·maven
Charlie__ZS16 分钟前
若依框架去掉Redis
java·redis·mybatis
AverageJoe199127 分钟前
一次vite热更新不生效问题排查
前端·debug·vite
努力只为躺平29 分钟前
🔥 油猴脚本开发指南:从基础API到发布全流程
前端·javascript
bitbitDown30 分钟前
我用Playwright爬了掘金热榜,发现了这些有趣的秘密... 🕵️‍♂️
前端·javascript·vue.js
陈随易35 分钟前
VSCode v1.102发布,AI体验大幅提升
前端·后端·程序员
ma7739 分钟前
JavaScript 获取短链接原始地址的解决方案
前端
该用户已不存在39 分钟前
关于我把Mac Mini托管到机房,后续来了,还有更多玩法
服务器·前端·mac
tianchang42 分钟前
SSR 深度解析:从原理到实践的完整指南
前端·vue.js·设计模式
闲蛋小超人笑嘻嘻44 分钟前
前端面试十一之TS
前端