概述
创建一个User类
package org.hx.springboot_freemarker_demo09.model;
public class User {
private Long id;
private String username;
private String address;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
创建一个UserController类
package org.hx.springboot_freemarker_demo09;
import org.hx.springboot_freemarker_demo09.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.ArrayList;
import java.util.List;
@Controller
public class UserController {
@GetMapping("/hello")
public String hello(Model model){
List users= new ArrayList<>();
for (int i = 0; i < 10; i++) {
User u = new User();
u.setId((long)i);
u.setUsername("hx: "+i);
u.setAddress("npu: "+i);
users.add(u);
}
model.addAttribute(“users”,users);
return “hello”;
}
}
在templates下创建一个hello.ftlh文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<#assign userinfo={"name":"hx","address":"npu"}>
<#list userinfo?keys as key>
<div>
${key}-${userinfo[key]}
</div>
</#list>
<hr>
<#list userinfo?values as value>
${value}
</#list>
<hr>
<#list 1..5 as x>
${x}<br/>
</#list>
<#list 5..1 as x>
${x}<br/>
</#list>
<#list [2+2,"hx"] as x>
${x}<br/>
</#list>
<hr>
<#assign flag=true>
<div>
${flag?string("a","b")}
</div>
<hr>
<#assign price = 99>
<div>
${price?string.currency}
</div>
<div>
${price?string.percent}
</div>
<hr>
<table border="1">
<tr>
<td>用户编号</td>
<td>用户名称</td>
<td>用户地址</td>
</tr>
<#list users as user>
<tr>
<td>${user.id}</td>
<td>${user.username}</td>
<td>${user.address}</td>
</tr>
</#list>
</table>
</body>
</html>
在UserController中加入新的代码
Map<String,Object> info = new HashMap<>();
info.put("name","hx");
info.put("age",18);
model.addAttribute("info",info);
model.addAttribute("name","h_x");
在hello.ftlh中取一下
<div>
<table border="1">
<#list users as u>
<tr>
<td>${u.username}</td>
<td>${u.address}</td>
</tr>
</#list>
</table>
</div>
<hr>
<div>
${users[3].username}
</div>
<hr>
<div>
<table border="1">
<#list users[3..5] as u>
<tr>
<td>${u.username}</td>
<td>${u.address}</td>
<td>${u_index}</td>
<td>${u_has_next?string("yes","no")}</td>
</tr>
</#list>
</table>
</div>
最后
以上就是酷炫春天为你收集整理的SpringBoot学习之Freemarker(一)创建一个User类创建一个UserController类在templates下创建一个hello.ftlh文件的全部内容,希望文章能够帮你解决SpringBoot学习之Freemarker(一)创建一个User类创建一个UserController类在templates下创建一个hello.ftlh文件所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复