SpringBoot框架集成Jsp开发-详细配置步骤


SpringBoot默认支持的前端页面是thymeleaf,如果要使用jsp页面,则需要完成以下几个步骤:

第1步:

在main目录下创建一个webapp目录,并把它设置为web资源文件夹,用来装载jsp文件 设置完之后的webapp文件夹样式如下图:

第2步:

在pom.xml文件中引入SpringBoot内嵌Tomcat对jsp的解析包:

<dependencies>
    <!--内置tocat对Jsp支持的依赖,用于编译Jsp-->
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
    </dependency>
    <!--jstl的支持,c标签-->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>javax.servlet.jsp-api</artifactId>
        <version>2.3.1</version>
    </dependency>
</dependencies>

第3步:

在pom.xml文件的中手动指定jsp最后编译的路径,SpringBoot集成jsp编译jsp的路径是规定好的位置:META-INF/resources

<build>
    <resources>
        <resource>
            <!--源文件夹-->
            <directory>scr/main/webapp</directory>
            <!--指定编译到META-INF/resources-->
            <targetPath>META-INF/resources</targetPath>
            <!--指定源文件夹中哪个资源要进行编译-->
            <includes>
                <include>*.*</include>
            </includes>
        </resource>
    </resources>
</build>

第4步:

在Application.propertices文件中配置视图解析器

# SpringBoot视图配置
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp

至此,springboot整合jsp就完成啦!

经验分享 程序员 微信小程序 职场和发展