Springboot集成Thymeleaf模版技术

1.springboot集成Thymeleaf模版技术:
(1)在application.properties中配置Thymeleaf依赖

<!-- springboot整合Thymeleaf视图层模版引擎 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

在这里插入图片描述
(2)新建Controller,增加返回ModelAndView的方法:

package com.steno.propertiestest.controller;

import com.steno.propertiestest.vo.Book;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * 测试Thymeleaf java模版引起的嵌入
 */
@Controller
public class ThymeleafController {

    /**
     * 返回Thymeleaf模版页面
     * @return
     */
    @GetMapping("/getThymeleafPage")
    public ModelAndView getThymeleafPage(){
        Book book = new Book();
        book.setName("thymeleaf");
        book.setAuthor("spring");
        book.setPrice("4.0");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("book", book);
        return new ModelAndView("thymeleafPage", map);
    }

}

在这里插入图片描述
(3)在src/main/resources/下新建templates文件夹,在该文件夹下新建.html结尾的页面

<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>springboot-thymeleaf</title>
</head>

<body>
    <!-- 返回值获取 -->
    <p th:text="'hello, ' + ${book.author} + '的' + ${book.name} + '!'" />
</body>
</html>

在这里插入图片描述
(4)访问域名http://localhost:8081/project/getThymeleafPage
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值