SpringBoot整合JSP(支持return和直接访问jsp)

SpringBoot整合JSP(支持return和直接访问jsp)

环境

SpringBoot版本,java版本1.8,spring-boot-maven-plugin:1.4.2.RELEASE

<parent>  
    <groupId>org.springframework.boot</groupId>  
    <artifactId>spring-boot-starter-parent</artifactId>  
    <version>2.4.5</version>
    <relativePath/>  
</parent>
<packaging>jar</packaging> # 这里也可以不写

修改pom文件

添加依赖

<dependency>  
    <groupId>org.springframework.boot</groupId>  
    <artifactId>spring-boot-starter-web</artifactId>  
    <scope>compile</scope>  
</dependency>
<dependency>  
    <groupId>org.apache.tomcat.embed</groupId>  
    <artifactId>tomcat-embed-jasper</artifactId>  
</dependency>  
<!--配置jsp jstl的支持-->  
<dependency>  
    <groupId>javax.servlet</groupId>  
    <artifactId>jstl</artifactId>  
</dependency>

添加编译插件

<!-- springboot 编译插件-->  
<build>  
    <plugins>  
        <plugin>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-maven-plugin</artifactId>  
            <version>1.4.2.RELEASE</version>  
            <configuration>  
                <fork>true</fork>  
                <addResources>true</addResources>  
                <mainClass>com.mirac.AppServerApplication</mainClass><!--主类的路径-->  
            </configuration>  
            <executions>  
                <execution>  
                    <goals>  
                        <goal>repackage</goal>  
                    </goals>  
                </execution>  
            </executions>  
        </plugin>  
    </plugins>  
    <!-- 指定资源文件的位置,否则maven打包的时候不会jsp页面打包进jar -->  
    <resources>  
        <resource>  
            <directory>src/main/resources</directory>  
            <includes>  
                <include>**/**</include>  
            </includes>  
        </resource>  
        <resource>  
            <directory>src/main/webapp</directory>  
            <targetPath>META-INF/resources</targetPath>  
            <includes>  
                <include>**/**</include>  
            </includes>  
        </resource>  
    </resources>  
</build>

创建webapp

在这里插入图片描述在这里插入图片描述
在这里插入图片描述

配置文件

配置文件默认不写,也可以通过配置spring.mvc.view.prefix/suffix并调整文件位置。

运行并访问

return方式:直接输入:localhost:port进入index.jsp页面
在这里插入图片描述

直接访问方式:浏览器输入:http://localhost:port/index.jsp

注意

注意检查打包后的资源文件,META-INF/resources文件夹和static文件夹都是springboot支持的直接访问资源的路径
在这里插入图片描述

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值