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页面了!!!

相关推荐
ZK_H11 分钟前
半导体工艺流程
java·c语言·开发语言·计算机网络·金融
liliangcsdn18 分钟前
sentence-transformer如何离线加载和使用模型
开发语言·前端·php
Crazy________19 分钟前
4.10dockerfile构建镜像
java·开发语言
阿维的博客日记30 分钟前
锁消除和锁粗化
java·逃逸分析
云烟成雨TD31 分钟前
Spring AI 1.x 系列【26】结构化输出执行流程
java·人工智能·spring
朦胧之1 小时前
AI 编程工具使用浅谈
前端·后端
柳杉1 小时前
HTML-in-Canvas:让 Canvas 完美渲染 HTML 的 Web 新标准
前端·javascript
雪的季节1 小时前
qt信号槽跨线程使用时候的坑
java·开发语言·qt
cTz6FE7gA1 小时前
WebGL实战:用Three.js创建3D场景,实现沉浸式Web体验
前端·javascript·webgl
chh5631 小时前
C++--内存管理
java·c语言·c++·windows·学习·面试