SpringBoot入门:SpringBoot整合Freemarker和Thymeleaf模板

37 篇文章 2 订阅
27 篇文章 4 订阅

1、创建springboot项目

关于springboot项目的创建可以看下面这篇文章,这里不进行叙述。
SpringBoot入门:使用IDEA和Eclipse构建第一个SpringBoot项目

2、Freemarker模板

首先在pom文件中引入freemarker的依赖,代码如下:

<!-- Spring Boot Freemarker 依赖 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-freemarker</artifactId>
		</dependency>

然后在com.example.demo文件夹下新建一个controller文件夹,新建FreemarkerController类,代码如下:

package com.example.demo.controller;

import java.util.ArrayList;
import java.util.List;

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

import com.example.demo.bean.User;

@Controller
public class FreemarkerController {
	
	@RequestMapping(value="/freemarker",method = RequestMethod.GET)
	public String freemarker(Model model) {
		
		model.addAttribute("name", "李青");
		model.addAttribute("age", 22);
		model.addAttribute("id", 223);
		
		
		return "freemarker";
	}
}

在application.yml文件中配置freemarker,修改的yml文件,新建默认的是application文件,

#yml和property文件功能一样都是配置设置,比如数据库、端口号等等,但是yml的结构时树状结构比较明朗,而且相同的内容因为是树状展示不用重复写
#但是如果项目中两个文件同时存在则优先加载property,yml的配置无效
spring
  freemarker:
#  prefix: classpath:/templates/
    suffix: .ftl
    content-type: text/html
    template-loader-path: classpath:/templates

在templates文件夹下新建freemarker.ftl文件,代码如下:

<!DOCTYPE html>

<html lang="en">
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<body>
     姓名:${name}<br><br>
    年龄:${age}<br><br>
    编号:${id}<br><br>
</body>
</html>

启动项目,在浏览器地址栏输入,http://localhost:8080/freemarker,页面如下:
在这里插入图片描述
freemarker页面模板创建完成!

3、thymeleaf模板

同样在pom文件中引入thymeleaf依赖,代码如下:

<!--引入thymeleaf的依赖 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>

在controller文件夹下新建ThymeleafController类,代码如下

package com.example.demo.controller;

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

@Controller
public class ThymeleafController {
	
	@RequestMapping("/thymeleaf")
	public String test(Model model) {
		model.addAttribute("name", "李青");
		model.addAttribute("age", 22);
		model.addAttribute("id", 223);
		return "thymeleaf";
	}
}

在application.yml文件中配置thymeleaf,代码如下:

spring:
  thymeleaf:
  #注意这里是prefix,而freemarker是    template-loader-path
    prefix: classpath:/templates/
    suffix: .html
    mode: HTML5
    encoding: UTF-8
    cache: false
    servlet:
      content-type: text/html

然后在templates文件夹下新建thymeleaf.html页面文件,代码如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
     姓名:<input type="text" th:value="${name}"><br/>
    年龄:<input type="text" th:value="${age}"><br/>
    编号:<input type="text" th:value="${id}"><br/>
</body>
</html>

启动项目,在浏览器地址栏输入,http://localhost:8080/thymeleaf,页面结果如下:
在这里插入图片描述
thymeleaf页面模板创建完成!

微信公众号
微信公众号

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值