SpringBoot集成JSP,FreeMarker,Thymeleaf的方式

SpringBoot集成JSP

SpringBoot想要集成JSP,首先需要添加JSP的相关依赖

        <dependency>
            <groupId>org.apache.tomcat.embedgroupId>
            <artifactId>tomcat-embed-jasperartifactId>
        dependency>

在添加完依赖后,还需要像传统的JSP那样,添加webapp等文件夹,具体的文件目录参考下图
SpringBoot集成JSP,FreeMarker,Thymeleaf的方式_第1张图片
在IDEA中,可以使用快捷键F4调出Project Structure
SpringBoot集成JSP,FreeMarker,Thymeleaf的方式_第2张图片
这样可以辅助我们生成web.xml文件等操作。
同时,我们应该在配置文件中配置jsp的相关信息

spring:
  mvc:
    view:
      prefix: /WEB-INF/views/
      suffix: .jsp

我们在运行项目的时候应该选择maven中的spring-boot插件,否则在IDEA中很有可能让我们不能够访问index.jsp。
SpringBoot集成JSP,FreeMarker,Thymeleaf的方式_第3张图片

SpringBoot集成FreeMarker

SpringBoot集成FreeMarker相对于集成JSP来说,那就简单多了,只需要添加相关依赖即可

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-freemarkerartifactId>
        dependency>

但是在使用过程中,由于FreeMarker的文件类型是ftl,为了更好的支持HTML5,在SpringBoot2.2中,我们需要将文件类型由原来的ftl升级为ftlh

SpringBoot集成Thymeleaf

与SpringBoot集成FreeMarker一样,也只要添加相关的依赖即可

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-thymeleafartifactId>
        dependency>

我们在使用thymeleaf相关的HTML的时候,可以将原本的html标签改成下面这种方式,这样在使用IDEA的时候就能够出现提示标签

        <html xmlns:th="http://www.thymeleaf.org">

你可能感兴趣的:(SpringBoot,2.x,集成前端,spring,boot,jsp,freemarker)