springboot整合jsp,并通过controller转发和直接访问jsp两种方式访问jsp页面

springboot默认没有提供jsp支持,所以想要支持jsp我们需要在pom里引入支持jsp的依赖包,并且需要在编译的时候将jsp页面指定到META-INF/resources目录下,下面是详细的整合步骤:

1.需要在pom里添加支持jsp的依赖包

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>

2.需要在pom里将jsp页面放入META-INF/resources目录下(这里打包方式选择的是将配置文件和依赖的jar包分离打包的方式,如果不采用这种方式打包则需要将spring-boot-maven-plugin的版本指定为1.4.2.RELEASE,建议还是用这种分离包的方式,因为配置文件和依赖的jar包都比较少更新没必要每次都进行打包,如果有更新可以增量去更新)

<build>
    <resources>
        <!-- Resource Filter -->
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
        </resource>
        <!--指定资源的位置-->
        <resource>
            <directory>src/main/resources</directory>
        </resource>
        <resource>
            <!-- 指定resources插件处理哪个目录下的资源文件 -->
            <directory>src/main/webapp</directory>
            <!--注意此次必须要放在此目录下才能被访问到 -->
            <targetPath>META-INF/resources</targetPath>
            <includes>
                <include>**/**</include>
            </includes>
        </resource>
    </resources>
    <plugins>
        <!--打包jar-->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <fork>true</fork>
                <jvmArguments>-Dfile.encoding=UTF-8</jvmArguments>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <!--MANIFEST.MF 中 Class-Path 加入前缀-->
                        <classpathPrefix>lib/</classpathPrefix>
                        <!--jar包不包含唯一版本标识-->
                        <useUniqueVersions>false</useUniqueVersions>
                        <!--指定入口类-->
                        <mainClass>com.springboot.jsp.JspApplication</mainClass>
                    </manifest>
                    <manifestEntries>
                        <!--MANIFEST.MF 中 Class-Path 加入资源文件目录-->
                        <!--本地依赖包需要手动 加入Class-Path ,否则无法找到-->
                        <Class-Path>./config/</Class-Path>
                    </manifestEntries>
                </archive>
                <outputDirectory>${project.build.directory}</outputDirectory>
            </configuration>
        </plugin>
        <!--拷贝依赖 copy-dependencies-->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>
                            ${project.build.directory}/lib/
                        </outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!--拷贝资源文件 copy-resources-->
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <resources>
                            <resource>
                                <directory>src/main/resources</directory>
                            </resource>
                        </resources>
                        <outputDirectory>${project.build.directory}/config</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>    

3.需要在application.yml添加mvc配置

spring:
  mvc:
    view:
      prefix: /views/
      suffix: .jsp

4.在views目录下创建一个index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>主页</title>
</head>
<body>
hello index!
</body>
</html>

5.写个IndexController做转发

package com.springboot.jsp.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class IndexController {
    @RequestMapping("/index")
    public String index(){
        return "index";
    }
}

项目结构:

编译结果:

因为采用了配置文件和依赖jar包分离打包的方式,在服务器上运行的时候需要拷贝config、lib和springboot-jsp-0.0.1-SNAPSHOT.jar,然后执行springboot-jsp-0.0.1-SNAPSHOT.jar

运行结果:

方式一.通过controller转发

方式二.通过jsp的URL直接访问

代码资源地址:https://download.csdn.net/download/xiaowenK/12400425

  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值