<!-- freemarker https://mvnrepository.com/artifact/org.freemarker/freemarker --> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.31</version> </dependency> <!-- 让spring兼容freemarker https://mvnrepository.com/artifact/org.springframework/spring-context-support --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>5.3.5</version> </dependency>
@Controller public class IndexController { @Autowired private BookMapper bookdao; @GetMapping("//m1") public String m1(Model model){ model.addAttribute("name","jack"); model.addAttribute("age","18"); model.addAttribute("isstu","true"); model.addAttribute("birth", new Date());//new Date.LocalDate.now() model.addAttribute("money", "100.985"); model.addAttribute("loves", List.of("java","javascript","html")); return "test";//templates/test.ftl } }
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <title>freemarker</title> <meta name="keywords" content="关键字"> <meta name="description" content="简介"> </head> <body> <h2>${name!‘没有值‘}</h2> <h2>${age}</h2> <h2>${birth?date}----${birth?string["yyyy-MM-dd"]}</h2> <#list loves as loves> <!--as 循环体--> <p>${loves}</p> </#list> <#if name??> yes <#else > no </#if> </body> </html>
原文:https://www.cnblogs.com/Gu1015/p/14655152.html