SpringBoot入门(三)SpringBoot整合Thymeleaf

前言

      本章讲解SpringBoot整合Thymeleaf的相关知识

方法

1.概念

通过前面的讲解,SpringBoot支持了JSP、FreeMarker等视图层技术。但是,SpringBoot推荐的视图层技术却是Thymeleaf。

所以,我们将重点讲解Thymeleaf的使用!

2.整合Thymeleaf步骤

1)在pom.xml中添加Thymeleaf启动器

<!-- 配置Thymeleaf启动器  -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

2)编写controller

package cn.edu.ccut.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {

	@RequestMapping("/showMsg")
	public String getMsg(Model model){
		model.addAttribute("msg", "this is thymeleaf !");
		return "index";
	}
}

3)配置Thymeleaf模板文件

这里只是简单的带大家了解一下该模板文件,实际上Thymeleaf通过其特定的语法对html做渲染。

如果要做到这一步,势必要知道Thymeleaf的相关语法,语法将会在后面的章节中进行体现!

本示例仅仅将controller中的msg值进行展示:

index.html:

<!DOCTYPE html>
<html>
<head>
	<title>Thymeleaf</title>
</head>
<body>
	<span th:text="${msg}"></span>
</body>
</html>

4)配置启动器

package cn.edu.ccut;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {

	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}

}

5)启动项目,输入http://localhost:8080/showMsg观察效果如下:

至此,一个简单的整合Thymeleaf的功能就写好了!

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值