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

相关推荐
快乐非自愿11 小时前
Java垃圾收集器全解:从Serial到G1的进化之旅
java·开发语言·python
树在风中摇曳11 小时前
Java 静态成员与继承封装实战:从报错到彻底吃透核心特性
java·开发语言
wuk99814 小时前
实现ROS系统的Websocket传输,向Web应用推送sensor_msgs::Image数据
前端·websocket·网络协议
键来大师14 小时前
Android15 RK3588 修改默认不锁屏不休眠
android·java·framework·rk3588
合作小小程序员小小店15 小时前
web网页开发,在线%考试管理%系统,基于Idea,vscode,html,css,vue,java,maven,springboot,mysql
java·前端·系统架构·vue·intellij-idea·springboot
天天进步201516 小时前
CSS Grid与Flexbox:2025年响应式布局终极指南
前端·css
多多*16 小时前
maven常用的命令
java·log4j·maven
xie_pin_an16 小时前
MyBatis-Plus 实战:MPJLambdaWrapper 多表联查用法全解析
java·spring boot·spring·mybatis
ᐇ95916 小时前
Java LinkedList集合全面解析:双向链表的艺术与实战
java·开发语言·链表
luyun02020216 小时前
Windows 11操作更丝滑,绝了
java·运维·figma