概述
新建工程 freemarker-demo
1. 导入依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.8.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency><!-- apache 对 java io 的封装工具库 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
2. 启动类
package com.freemarker; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class WebApp { public static void main(String[] args) { SpringApplication.run(WebApp.class, args); } }
3. 配置文件
server: port: 8001 spring: application: name: freemarker-demo freemarker: suffix: .ftl
4. 模板文件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>01-basic</title>
</head>
<body>
<b>普通文本 String 展示:</b><br><br>
Hello ${name} <br>
<hr>
<b>对象Student中的数据展示:</b><br/>
姓名:${stu.name}<br/>
年龄:${stu.age}
<hr>
</body>
</html>
5. 实体类
package com.freemarker.entity; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor @AllArgsConstructor public class Student { private String name; private Integer age; }
6. 控制器
package com.freemarker.controller;
import com.heima.freemarker.entity.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/test")
public class TestController {
@GetMapping("/basic")
public String basic(Model model) {
// 1.纯文本形式的参数
model.addAttribute("name", "zhangsan");
// 2.实体类相关的参数
Student student = new Student("lisi", 18);
model.addAttribute("stu", student);
return "01-basic";
}
}
******
freemarker 基础语法:
--list
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>02-list</title> </head> <body> <table> <#list stus as stu> <tr> <td>${stu_index+1}</td> <td>${stu.name}</td> <td>${stu.age}</td> </tr></#list> </table> </body> </html> 添加控制器方法 @GetMapping("/list") public String list(Model model){ List<Student> list = new ArrayList<>(); list.add(new Student("zhangsan",18)); list.add(new Student("lisi",20)); list.add(new Student("wangwu",19)); model.addAttribute("stus", list); return "02-list"; }
--map
添加模板文件 03-map.ftl
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>03-map</title> </head> <body> <ul> <#list userMap?keys as key> <li>key:${key}--value:${userMap["${key}"]}</li> </#list> </ul> </body> </html>
添加控制器方法
@GetMapping("/map") public String map(Model model){ Map<String,Object> map = new HashMap<>(); map.put("zhangsan",18); map.put("lisi",22); model.addAttribute("userMap", map); return "03-map"; }
--条件判断
添加模板 04-if.ftl
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>02-list</title> </head> <body> <table> <#list stus as stu> <#if stu.name='小红'> <tr style="color: red"> <td>${stu_index+1}</td> <td>${stu.name}</td> <td>${stu.age}</td> </tr><#else > <tr> <td>${stu_index+1}</td> <td>${stu.name}</td> <td>${stu.age}</td> </tr></#if> </#list> </table> </body> </html>
添加方法
@GetMapping("/if") public String condition(Model model){ List<Student> list = new ArrayList<>(); list.add(new Student("小红",18)); list.add(new Student("小明",20)); model.addAttribute("stus", list); return "04-if"; }
测试
最后
以上就是复杂洋葱为你收集整理的freemarker 入门******的全部内容,希望文章能够帮你解决freemarker 入门******所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复