Skip to content

Commit 895cce5

Browse files
committed
add freemarker configure temple util sample in springboot
1 parent ff2b60c commit 895cce5

File tree

4 files changed

+67
-7
lines changed

4 files changed

+67
-7
lines changed
+15-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
package com.ipman.freemarker.sample.controller;
22

33
import com.ipman.freemarker.sample.entity.Student;
4+
import freemarker.template.Template;
5+
import freemarker.template.TemplateException;
6+
import org.springframework.beans.factory.annotation.Autowired;
47
import org.springframework.stereotype.Controller;
8+
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
59
import org.springframework.web.bind.annotation.GetMapping;
10+
import org.springframework.web.bind.annotation.RestController;
611
import org.springframework.web.servlet.ModelAndView;
12+
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
713

14+
import java.io.IOException;
815
import java.util.ArrayList;
916
import java.util.List;
1017

@@ -17,14 +24,14 @@
1724
* @date 2021/9/23 5:28 下午
1825
*/
1926
@Controller
20-
public class HelloWorld {
27+
public class Demo {
2128

2229
/**
2330
* 测试Freemarker模版
2431
*/
25-
@GetMapping("/hello")
26-
public ModelAndView hello() {
27-
ModelAndView model = new ModelAndView("hello");
32+
@GetMapping("/demo")
33+
public ModelAndView demo() {
34+
ModelAndView model = new ModelAndView("demo");
2835
model.addObject("userName", "ipman");
2936
// 返回模板名称
3037
return model;
@@ -33,9 +40,9 @@ public ModelAndView hello() {
3340
/**
3441
* 测试Freemarker对象与列表渲染
3542
*/
36-
@GetMapping("/hello1")
37-
public ModelAndView hello1() {
38-
ModelAndView model = new ModelAndView("hello1");
43+
@GetMapping("/demo1")
44+
public ModelAndView demo1() {
45+
ModelAndView model = new ModelAndView("demo1");
3946
List<Student> list = new ArrayList<>();
4047
Student s1 = new Student();
4148
s1.setIdNo("No1");
@@ -49,4 +56,5 @@ public ModelAndView hello1() {
4956
return model;
5057
}
5158

59+
5260
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.ipman.freemarker.sample.controller;
2+
3+
import com.ipman.freemarker.sample.entity.Student;
4+
import freemarker.template.Template;
5+
import freemarker.template.TemplateException;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
8+
import org.springframework.web.bind.annotation.GetMapping;
9+
import org.springframework.web.bind.annotation.RestController;
10+
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
11+
12+
import java.io.IOException;
13+
import java.util.ArrayList;
14+
import java.util.HashMap;
15+
import java.util.List;
16+
import java.util.Map;
17+
18+
/**
19+
* Created by ipipman on 2021/9/23.
20+
*
21+
* @version V1.0
22+
* @Package com.ipman.freemarker.sample.controller
23+
* @Description: (用一句话描述该文件做什么)
24+
* @date 2021/9/23 6:17 下午
25+
*/
26+
@RestController
27+
public class Demo1 {
28+
29+
@Autowired
30+
private FreeMarkerConfigurer freeMarkerConfigurer;
31+
32+
/**
33+
* 根据Freemarker模版渲染引擎,渲染HTML代码
34+
*/
35+
@GetMapping("/demo2")
36+
public String demo2() throws IOException, TemplateException {
37+
List<Student> list = new ArrayList<>();
38+
Student s1 = new Student();
39+
s1.setIdNo("No1");
40+
s1.setName("ipman");
41+
list.add(s1);
42+
Student s2 = new Student();
43+
s2.setIdNo("No2");
44+
s2.setName("ipipman");
45+
list.add(s2);
46+
Map<String, Object> param = new HashMap<>();
47+
param.put("students", list);
48+
// 根据Freemarker模版渲染引擎,渲染HTML代码
49+
Template template = freeMarkerConfigurer.getConfiguration().getTemplate("demo1.ftl");
50+
return "html=[" + FreeMarkerTemplateUtils.processTemplateIntoString(template, param) + "]";
51+
}
52+
}

0 commit comments

Comments
 (0)