
1 2 3 4 5 6 7 8 9 10 11 12 13
| server:
port: 8088 #服务端口
spring:
application:
name: test‐freemarker #指定服务名 freemarker:
cache: false #关闭模板缓存,方便测试 settings:
template_update_delay: 0 #检查模板更新延迟时间,设置为0表示立即检查,如果时间大于0会有缓存不方便 进行模板测试
|

1
| <artifactId>test-freemarker</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> </dependencies> </project>
|
List 指令

Map指令


IF指令


2 使用模板文件静态化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| @SpringBootTest @RunWith(SpringRunner.class) public class FreemarkerTest {
@Test public void testGenerateHtml() throws IOException, TemplateException { Configuration configuration=new Configuration(Configuration.getVersion()); String classpath = this.getClass().getResource("/").getPath(); configuration.setDirectoryForTemplateLoading(new File(classpath + "/templates/")); configuration.setDefaultEncoding("utf-8"); Template template = configuration.getTemplate("test1.ftl"); Map map = getMap(); String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, map); System.out.println(content); InputStream inputStream = IOUtils.toInputStream(content); FileOutputStream fileOutputStream = new FileOutputStream(new File("d:/test1.html")); int copy = IOUtils.copy(inputStream, fileOutputStream); } private Map getMap(){ Map<String, Object> map = new HashMap<>(); map.put("name","黑马程序员"); Student stu1 = new Student(); stu1.setName("小明"); stu1.setAge(18); stu1.setMondy(1000.86f); stu1.setBirthday(new Date()); Student stu2 = new Student(); stu2.setName("小红"); stu2.setMondy(200.1f); stu2.setAge(19);
List<Student> friends = new ArrayList<>(); friends.add(stu1); stu2.setFriends(friends); stu2.setBestFriend(stu1); List<Student> stus = new ArrayList<>(); stus.add(stu1); stus.add(stu2); map.put("stus",stus); HashMap<String,Student> stuMap = new HashMap<>(); stuMap.put("stu1",stu1); stuMap.put("stu2",stu2); map.put("stu1",stu1); map.put("stuMap",stuMap); return map; } }
|
⭐️基于模板字符串生成静态化文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| @Test public void testGenerateHtmlByString() throws IOException, TemplateException { Configuration configuration=new Configuration(Configuration.getVersion()); String templateString="" + "<html>\n" + " <head></head>\n" + " <body>\n" + " 名称:${name}\n" + " </body>\n" + "</html>";
StringTemplateLoader stringTemplateLoader = new StringTemplateLoader(); stringTemplateLoader.putTemplate("template",templateString); configuration.setTemplateLoader(stringTemplateLoader); Template template = configuration.getTemplate("template","utf-8");
Map map = getMap(); String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, map); System.out.println(content); InputStream inputStream = IOUtils.toInputStream(content); FileOutputStream fileOutputStream = new FileOutputStream(new File("d:/test1.html")); IOUtils.copy(inputStream, fileOutputStream); }
private Map getMap(){ Map<String, Object> map = new HashMap<>(); map.put("name","黑马程序员"); Student stu1 = new Student(); stu1.setName("小明"); stu1.setAge(18); stu1.setMondy(1000.86f); stu1.setBirthday(new Date()); Student stu2 = new Student(); stu2.setName("小红"); stu2.setMondy(200.1f); stu2.setAge(19);
List<Student> friends = new ArrayList<>(); friends.add(stu1); stu2.setFriends(friends); stu2.setBestFriend(stu1); List<Student> stus = new ArrayList<>(); stus.add(stu1); stus.add(stu2); map.put("stus",stus); HashMap<String,Student> stuMap = new HashMap<>(); stuMap.put("stu1",stu1); stuMap.put("stu2",stu2); map.put("stu1",stu1); map.put("stuMap",stuMap); return map; } }
|
Author:
John Doe
Permalink:
http://yoursite.com/2019/05/22/其他/Freemark/
License:
Copyright (c) 2019 CC-BY-NC-4.0 LICENSE
Slogan:
Do you believe in DESTINY?