springboot+thymeleaf学习笔记1

1.学习thymeleaf

了解什么是thymeleaf?

Thymeleaf是一个用于web和独立环境的现代服务器端Java模板引擎。

Thymeleaf的主要目标是将优雅的自然模板引入到您的开发工作流程中-HTML可以在浏览器中正确显示,也可以作为静态原型工作,从而在开发团队中实现更强的协作。

有了Spring框架的模块、与您喜爱的工具的大量集成以及插入您自己的功能的能力,Thymeleaf是现代HTML5JVM web开发的理想选择——尽管它可以做更多的事情。

2.在文章的基础上编写一个thymeleaf 例子

spring2.2.2 + mybatis-spring-boot-starter 2.1.1 + mysql 8.0.18的例子

2.1 新建一个页面 如图1

图1 建user.html

 

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>获取用户名</title>
</head>
<body>
<p th:text="${username}+'的例子'">我的例子</p> 
<p th:text="'当前时间为 :'+ ${currentDate}">2020-01-15 20:38:35.000715</p>
<p th:text="'Hello! ' + ${username} + '!'"></p>
<p th:text="|Hello! ${username}!|">我的例子传送username变量</p>
</body>
</html>

2.2建立一个User

package com.dachan.crm.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;

import static com.dachan.crm.demo.util.DateUtils.getFormatDateHour;

@Controller
public class UserController {
    @RequestMapping("/getUser") //请求地址
    public String hello(Model model) { //如果为model 
        model.addAttribute("currentDate", getFormatDateHour(new Date(),"")); //定义一个日期 返回日期时间
        model.addAttribute("username", "keny风清扬"); //定义username 为me
        return "user"; //注意::::这个要和user.html 一样。
    }
}

2.3 图2 运行效果
 

图2 运行效果

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值