Freemarker和springboot推荐的Thymeleaf相似,都是模版识图
模版视图和jsp的区别:
jsp → 在tomcat 中实现:jsp→servlet→执行→打印显示→输出页面
模版视图 → 在客户端进行 → 渲染的数据在客户端浏览器完成数据与页面渲染过程较-jsp来说效率高一些
<!--Freemarker 启动器依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
@Controller
public class UsersController {
/**
* 处理请求,返回数据
*/
@GetMapping("/showUsers")
public String showUsers(Model model){
List<Users> list = new ArrayList<>();
list.add(new Users("admin","F","32"));
list.add(new Users("Lisi","M","23"));
list.add(new Users("xiaoli","F","23"));
model.addAttribute("list",list);
return "userList";
}
}
public class Users {
private String username;
private String usersex;
private String userage;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUsersex() {
return usersex;
}
public void setUsersex(String usersex) {
this.usersex = usersex;
}
public String getUserage() {
return userage;
}
public void setUserage(String userage) {
this.userage = userage;
}
public Users(String username, String usersex, String
userage) {
this.username = username;
this.usersex = usersex;
this.userage = userage;
}
public Users() {
}
}
存放在sesources>templates目录下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table border="1" align="center" width="50%">
<tr>
<th>Name</th>
<th>Sex</th>
<th>Age</th>
</tr>
<#list list as user>
<tr>
<td>${user.username}</td>
<td>${user.usersex}</td>
<td>${user.userage}</td>
</tr>
</#list>
</table>
</body>
spring.freemarker.suffix=.ftl
网站声明:如果转载,请联系本站管理员。否则一切后果自行承担。
添加我为好友,拉您入交流群!
请使用微信扫一扫!