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

相关推荐
DaHai6 分钟前
在 Windows 上安装 uv(高性能 Python 包管理器)
前端
Lee川14 分钟前
🔍 React 面试官眼中的“秘密武器”:深度剖析 useRef
前端·react.js·面试
小文大数据17 分钟前
python实现HTML转PDF
java·前端·数据库
架构师沉默22 分钟前
为什么 Dubbo 从 ZooKeeper 转向 Nacos?
java·后端·架构
用户83071968408227 分钟前
Spring Prototype Bean的四种正确使用方式
java·spring boot·后端
永恒_顺其自然29 分钟前
Java Web 传统项目异步分块上传系统实现方案
java·开发语言·前端
百撕可乐37 分钟前
NextJS官网实战01:Vue与React的区别
前端·react.js·前端框架
Можно41 分钟前
Vue 组件样式隔离完全指南:从原理到实战
前端·javascript·vue.js
赫瑞1 小时前
Java中的大数处理 —— BigInteger
java·开发语言
r_oo_ki_e_1 小时前
java25--Collection集合
java·开发语言