问题遇到的现象和发生背景
后端代码百分百没问题,但前端我想在html.ftl文件中引入div.ftl文件,并传递不同的数据让它渲染三次,大家看html.ftl的代码应该能清楚我的意图。下面是我错误写法的代码,请问怎么改造这俩ftl文件才能实现这个功能?
问题相关代码,请勿粘贴截图
后端代码
@RequestMapping
@Controller
public class FatherSonController {
@GetMapping("fatherSon")
public String fatherSon(HttpServletRequest request){
People son = new People();
son.setName("阿斗");
son.setAge(20);
People father = new People();
father.setName("刘备");
father.setAge(50);
father.setSon(son);
request.setAttribute("father",father);
return "html";
}
}
class People{
private String name;
private int age;
private People son;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public People getSon() {
return son;
}
public void setSon(People son) {
this.son = son;
}
}
前端div.ftl的代码
<div>
姓名:${people.name}
年龄:${people.age}
</div>
前端html.ftl的代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test</title>
</head>
<body>
<h1>父与子</h1>
<#assign people=father>
父亲:<#include "./div.ftl">
<#if father.son !=null>
<#assign people=father.son>
儿子:<#include "./div.ftl">
</#if>
<#assign people=father>
父亲重复:<#include "./div.ftl">
</body>
</html>