使用模板和数据模型 动态生成资源 只能在并发小的情况实现
    对于大并发的使用环境 需要使用 模板和数据模型 提前生成好静态的html资源 用户访问的时候 直接访问静态html资源即可

    生成方法:

    1. @Test
    2. public void testHtml01() throws IOException, TemplateException {
    3. // 1.创建配置类信息
    4. Configuration configuration = new Configuration(Configuration.getVersion());
    5. // 2.对配置类进行配置
    6. String path = this.getClass().getResource("/templates/").getPath();
    7. configuration.setDirectoryForTemplateLoading(new File(path));
    8. configuration.setDefaultEncoding("utf-8");
    9. // 3.获得模板文件对象
    10. Template template = configuration.getTemplate("index.ftl");
    11. // 4.获得数据模型
    12. Map data = getData();
    13. // 5.静态化操作
    14. String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, data);
    15. InputStream inputStream = IOUtils.toInputStream(content);
    16. FileOutputStream outputStream = new FileOutputStream(new File("d:/index.html"));
    17. IOUtils.copy(inputStream, outputStream);
    18. }

    生成数据模型map

    1. private Map getData() {
    2. Map<String, Object> map = new HashMap<>();
    3. Student student = new Student();
    4. student.setName("小明");
    5. student.setAge(18);
    6. //1.纯文本形式的参数
    7. map.put("name", "freemarker");
    8. //2.实体类相关的参数
    9. map.put("stu1", student);
    10. //2.1 小强对象模型数据
    11. Student stu1 = new Student();
    12. // stu1.setName("小强");
    13. stu1.setAge(18);
    14. stu1.setMoney(1000.86f);
    15. stu1.setBirthday(new Date());
    16. //2.2 小红对象模型数据
    17. Student stu2 = new Student();
    18. stu2.setName("小红");
    19. stu2.setMoney(200.1f);
    20. stu2.setAge(19);
    21. //2.3 将两个对象模型数据存放到List集合中
    22. List<Student> stus = new ArrayList<>();
    23. stus.add(stu1);
    24. stus.add(stu2);
    25. //2.4 向map中存放List集合数据
    26. map.put("stus", stus);
    27. //3. 创建Map数据
    28. HashMap<String, Student> stuMap = new HashMap<>();
    29. stuMap.put("stu1", stu1);
    30. stuMap.put("stu2", stu2);
    31. // 3.1 向map中存放Map数据
    32. map.put("stuMap", stuMap);
    33. HashMap<String, Student> stuMapTest = new HashMap<>();
    34. stuMapTest.put("stu.name", stu1);
    35. map.put("stuMapTest", stuMapTest);
    36. //返回Map
    37. return map;
    38. }