freemarker demo

 <!--freemarker-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>
/**
 * Freemarker实例Controller
 */
@RequestMapping("/freemarker")
@Controller
public class FreemarkerController {
    @Autowired
    RestTemplate restTemplate;
    @RequestMapping("/test1")
    public String test1(Map<String,Object> map){
        map.put("name","fly");
        Student stu2 = new Student();
        Student stu1 = new Student("小明",18,new Date(),1000.22f,null,stu2);
        stu2.setName("小红");
        stu2.setMoney(222.1f);
        stu2.setAge(12);
        //list数据
        List<Student> stus = new ArrayList<>();
        stus.add(stu1);
        stus.add(stu2);
        map.put("stus",stus);
        //map数据
        HashMap<String,Student> stuMap = new HashMap<>();
        stuMap.put("stu1",stu1);
        stuMap.put("stu2",stu2);
        map.put("stu1",stu1);
        map.put("stuMap",stuMap);
        map.put("today",new Date());
        return "test1";
    }
}

templates/test1.ftl:

<#--
freemarker实例
-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h4>hello ${name}!</h4>
<table border="1">
    <tr>
        <td>序号</td>
        <td>姓名</td>
        <td>年龄</td>
        <td>钱包</td>
    </tr>
    <#--遍历list-->
    <#list stus as stu>
        <tr>
            <td>${stu_index + 1}</td> <#-- _index,循环下标,从0开始-->
            <td>${stu.name}</td>
            <td>${stu.age}</td>
            <td>${stu.money}</td>
        </tr>
    </#list>
</table>
<#--遍历map-->
stu1: <br>
姓名: ${stuMap['stu1'].name} <br>
年龄: ${stuMap['stu1'].age} <br>
stu1: <br>
姓名: ${stuMap.stu1.name} <br>
年龄: ${stuMap.stu1.age} <br>
遍历map
<table border="1">
    <tr>
        <td>序号</td>
        <td>姓名</td>
        <td>年龄</td>
        <td>钱包</td>
    </tr>
    <#list stuMap?keys as k>
        <tr>
            <td>${k_index + 1}</td>
            <#--if 指令-->
            <td <#if stuMap[k].name=='小明'>style="background: red;"</#if>>
                ${stuMap[k].name}
            </td>
            <td>${stuMap[k].age}</td>
            <td>${stuMap[k].money}</td>
        </tr>
    </#list>
</table>
<#--空值处理-->
<#if stu1??>
    <p>${stu1.name}</p>
</#if>
<br>
<#--内建函数-->
<#--集合大小-->
集合大小${stus?size}
年月日${today?date}
时分秒${today?time}
年月日+时分秒${today?datetime}
自定义格式${today?string("yyyy年MM月")}
<#--内建函数c 三位分割-->
${1111112221?c}
<#--json转对象-->
<#assign text="{'bank':'工商银行','account':'101010101010001010'}"/>
<#assign data=text?eval/>
户行${data.bank} 账号${data.account}
</body>
</html>

/**
 * freemarker静态化测试
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ShuJuApplication.class)
public class FreemarkerTest {
    @Test
    public void testGenerateHtml() throws Exception {
        Configuration configuration = new Configuration(Configuration.getVersion());
        String path = this.getClass().getResource("/").getPath();
        configuration.setDirectoryForTemplateLoading(new File(path+"/templates"));
        configuration.setDefaultEncoding("utf-8");
        Template template = configuration.getTemplate("test1.ftl");
        Map<String,Object> map = new HashMap<>();
        map.put("name","fly");
        String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
        System.out.println(content);
        InputStream inputStream = IOUtils.toInputStream(content, "utf-8");
        FileOutputStream fileOutputStream = new FileOutputStream(new File("C:\uploads\"+"test1.html"));
        IOUtils.copy(inputStream,fileOutputStream);
    }
    @Test
    public void testGenerateHtml2() throws Exception {
        Configuration configuration = new Configuration(Configuration.getVersion());
        String templateString = "<!DOCTYPE html>
" +
                "<html lang="en">
" +
                "<head>
" +
                "    <meta charset="UTF-8">
" +
                "    <title>Title</title>
" +
                "</head>
" +
                "<body>
" +
                "<h4>hello ${name}!</h4>
" +
                "</body>
" +
                "</html>";
        StringTemplateLoader stringTemplateLoader = new StringTemplateLoader();
        stringTemplateLoader.putTemplate("template",templateString);
        configuration.setTemplateLoader(stringTemplateLoader);
        Template template = configuration.getTemplate("template","utf-8");
        Map<String,Object> map = new HashMap<>();
        map.put("name","fly");
        String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
        System.out.println(content);
        InputStream inputStream = IOUtils.toInputStream(content, "utf-8");
        FileOutputStream fileOutputStream = new FileOutputStream(new File("C:\uploads\"+"test1.html"));
        IOUtils.copy(inputStream,fileOutputStream);
    }
}
原文地址:https://www.cnblogs.com/fly-book/p/10642466.html