Skip to content

Commit ff2b60c

Browse files
committed
add freemarker list and object type render sample in spring boot
1 parent 3a04c1c commit ff2b60c

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

springboot-freemarker-sample/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@
4444
<groupId>org.springframework.boot</groupId>
4545
<artifactId>spring-boot-starter-freemarker</artifactId>
4646
</dependency>
47+
<dependency>
48+
<groupId>com.github.andrewoma.dexx</groupId>
49+
<artifactId>dexx-collections</artifactId>
50+
<version>0.2</version>
51+
<scope>compile</scope>
52+
</dependency>
4753
</dependencies>
4854

4955
<build>

springboot-freemarker-sample/src/main/java/com/ipman/freemarker/sample/controller/HelloWorld.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package com.ipman.freemarker.sample.controller;
22

3+
import com.ipman.freemarker.sample.entity.Student;
34
import org.springframework.stereotype.Controller;
45
import org.springframework.web.bind.annotation.GetMapping;
56
import org.springframework.web.servlet.ModelAndView;
67

8+
import java.util.ArrayList;
9+
import java.util.List;
10+
711
/**
812
* @author ipipman
913
* @version V1.0
@@ -26,4 +30,23 @@ public ModelAndView hello() {
2630
return model;
2731
}
2832

33+
/**
34+
* 测试Freemarker对象与列表渲染
35+
*/
36+
@GetMapping("/hello1")
37+
public ModelAndView hello1() {
38+
ModelAndView model = new ModelAndView("hello1");
39+
List<Student> list = new ArrayList<>();
40+
Student s1 = new Student();
41+
s1.setIdNo("No1");
42+
s1.setName("ipman");
43+
list.add(s1);
44+
Student s2 = new Student();
45+
s2.setIdNo("No2");
46+
s2.setName("ipipman");
47+
list.add(s2);
48+
model.addObject("students", list);
49+
return model;
50+
}
51+
2952
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.ipman.freemarker.sample.entity;
2+
3+
/**
4+
* Created by ipipman on 2021/9/23.
5+
*
6+
* @version V1.0
7+
* @Package com.ipman.freemarker.sample.entity
8+
* @Description: (用一句话描述该文件做什么)
9+
* @date 2021/9/23 5:59 下午
10+
*/
11+
public class Student {
12+
13+
private String idNo;
14+
15+
private String name;
16+
17+
public String getIdNo() {
18+
return idNo;
19+
}
20+
21+
public void setIdNo(String idNo) {
22+
this.idNo = idNo;
23+
}
24+
25+
public String getName() {
26+
return name;
27+
}
28+
29+
public void setName(String name) {
30+
this.name = name;
31+
}
32+
33+
@Override
34+
public String toString() {
35+
return "Student{" +
36+
"idNo='" + idNo + '\'' +
37+
", name='" + name + '\'' +
38+
'}';
39+
}
40+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>demo列表</title>
6+
</head>
7+
<body>
8+
<table border="1">
9+
<tr>
10+
<td>编号</td>
11+
<td>名称</td>
12+
</tr>
13+
<#list students as student>
14+
<tr>
15+
<td>${student.idNo}</td>
16+
<td>${student.name}</td>
17+
</tr>
18+
</#list>
19+
</table>
20+
</body>
21+
</html>

0 commit comments

Comments
 (0)