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

相关推荐
GDAL几秒前
css实现元素居中的18种方法
前端·css·面试·html·css3·css居中
♡喜欢做梦2 分钟前
MyBatis操作数据库(进阶):动态SQL
java·数据库·sql·java-ee·mybatis
copyer_xyf3 分钟前
SQL 语法速查手册:前端开发者的学习笔记
前端·数据库·sql
lusasky5 分钟前
com.itextpdf堆外内存(Off-Heap Memory)泄露
java
.豆鲨包5 分钟前
【Android】深入理解Window和WindowManager
android·java
Dylan的码园5 分钟前
ArrayList与顺序表
java·数据结构·链表
Boop_wu6 分钟前
[Java EE] 文件操作(系统文件和字节流字符流)
java·java-ee
Aevget6 分钟前
「Java EE开发指南」如何在MyEclipse中开发EJB 2 Session Bean?(二)
java·ide·java-ee·开发工具·myeclipse
带刺的坐椅6 分钟前
Solon AI 开发学习11 - chat - 工具调用与定制(Tool Call)
java·ai·llm·solon
拾忆,想起7 分钟前
Dubbo服务版本控制完全指南:实现微服务平滑升级的金钥匙
前端·微服务·云原生·架构·dubbo·safari