日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

springboot 視圖集成

作者:龍兄你好呀 更新時間: 2022-07-09 編程語言

一、freemarker

SpringBoot內部支持Freemarker視圖技術的集成,并提供了自動化配置類FreeMarkerAutoConfiguration,借助自動化配置可以很方便的集成Freemarker基礎到 SpringBoot環境中。

1.引入依賴

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

2.查找配置類

在idea上雙擊shift搜索FreeMarkerProperties,
在這里插入圖片描述
可以看到,freemareker默認加載路徑是類路徑下一個叫templates的文件夾,并且后綴默認為.ftlh。

3.更換配置

如果我們不想用springboot的默認配置,那么可以在application.yml文件中修改這些配置。

spring:
  freemarker:
    #加載路徑
    template-loader-path: classpath:/view/
    ##后綴
    suffix: .ftl
    ##字符
    charset: UTF-8

4.訪問視圖

在resources目錄下新建目錄views,同時新建一個以后綴.ftl結尾的文件:

<h1>牛馬</h1>
<h2>${msg}</h2>

在controller層編寫方法,訪問視圖:

 @RequestMapping("index")
    public String index(Model model) {
        model.addAttribute("msg", "hello springboot");
        return "index";
    }

瀏覽器輸入http://localhost:8080/index,看到如下頁面:
在這里插入圖片描述

二、thymeleaf

1.引入依賴

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

2.查找配置類

在idea上雙擊shift搜索ThymeleafProperties,
在這里插入圖片描述

可以看到,thymeleaf默認加載路徑是類路徑下一個叫templates的文件夾,并且后綴默認為.html。

3.更換配置

如果我們不想用springboot的默認配置,那么可以在application.yml文件中修改這些配置。

spring:
  thymeleaf:
    # 加載路徑
    prefix: classpath:/views/
    suffix: .html

4.訪問視圖

在resources目錄下新建目錄views,同時新建一個以后綴.html結尾的文件:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1 th:text="${msg}"></h1>
</body>
</html>

在controller層編寫方法,訪問視圖:

 @RequestMapping("index")
    public String index(Model model) {
        model.addAttribute("msg", "hello springboot");
        return "index";
    }

瀏覽器輸入http://localhost:8080/index,看到如下頁面:
在這里插入圖片描述

原文鏈接:https://blog.csdn.net/rqt1013_/article/details/125658725

欄目分類
最近更新