FreeMarker入门

FreeMarker入门

freemarker文件以.ftl为后缀

1.pom.xml中配置依赖,利用maven管理jar

<dependency>

  <groupId>org.springframework.boot</groupId>

  <artifactId>spring-boot-starter-freemarker</artifactId>

</dependency>

 

2.controller

@Controller
public class ProductController {
	@RequestMapping(method = RequestMethod.GET, value = "/products")
	public String showAll(Model model) {
		List<Customer> customers = new ArrayList<Customer>();
		customers.add(new Customer(1L, "c1", "a1", "133..."));
		customers.add(new Customer(2L, "c2", "a2", "133..."));
		customers.add(new Customer(3L, "c3", "a3", "133..."));
		model.addAttribute("products", customers);
		return "products";
	}
}

3.创建freemarker

在项目中的src/main/resources中的templates中创建*.ftl文件

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

放在<head>中,定义编码

Freemarker (https://freemarker.apache.org/)中有template模板,操作如下


点击红色框中的链接,选择下面的模板


将之copy*.ftl文件中

eg.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  	<title>Welcome</title>
</head>
<body>
  	<h1>Hello, FreeMarker!</h1>
  	<p>成员:</p>
  	<#list products as product>
  	<li>${product.id}/${product.name}</li>
  	</#list>
</body>
</html>


4.测试

运行项目中src/main/java下的XXXApplication,使用测试工具Restlet Client或直接在页面上测试



*:补充

FreeMarker 自有的指令,不需要引标签库

条件:<if></if>

循环:<#list></#list>


springMVC 封装了jspfreemarker,写好控制器,将数据放入model,页面获取即可

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值