温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

FreeMarker的原理和应用

发布时间:2021-06-29 10:37:03 来源:亿速云 阅读:277 作者:chen 栏目:大数据

这篇文章主要介绍“FreeMarker的原理和应用”,在日常操作中,相信很多人在FreeMarker的原理和应用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”FreeMarker的原理和应用”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

FreeMarker是一个用Java开发的模版引擎,即一种基于模版和要改变的数据,并用来生成输出文本(HTML网页,电子邮件,配置文件,源代码等)的通用工具。它不是面向最终用户的,而是一个java类库,是一款程序员可以嵌入他们所开发产品的组件。

FreeMarker并不关心数据的来源,只是根据模版的内容,将数据模版在模版中显示并输出文件(通常是html,也可以生成其他格式的文本文件)

数据模型:数据模型在java中可以是基本类型也可以说List,Map,Pojo等复杂类型

快速入门:

一、添加依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-freemarker</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.8</version>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-io</artifactId>
        <version>1.3.2</version>
    </dependency>

    <dependency>
        <groupId>com.squareup.okhttp3</groupId>
        <artifactId>okhttp</artifactId>
        <version>4.1.0</version>
    </dependency>
</dependencies>

二、编写启动类

@SpringBootApplication
public class FreemarkerApplication {
    public static void main(String[] args) {
        SpringApplication.run(FreemarkerApplication.class);
    }
}

三、编写实体类

@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class Student {

    private String name;
    private int age;
    private Date birthday;
    private Float money;
    private List<Student> friends;
    private Student bestFriends;
}

四、在resources下创建templates文件夹,创建模版文件 test1.ftl

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
hello ${name}!
</body>
</html>

五、编写controller

@Controller
@RequestMapping("/freemarker")
public class FreemarkerController {
   @RequestMapping("/test1")
    public String freemarker(Map<String ,Object> map){
       map.put("name","cxy");
       return "test1";
   }


}

六、启动测试

freemarker基础知识:

1、注释,即<#-- 内容 -->,介于其之间的内容会被freemarker忽略

2、插值,即${},会被freemarker用真实值替代

3、FTL,和html标记类似,名字前加#予以区别,freemarker会解析标签中的表达式或逻辑

4、文本,仅文本信息,直接输出内容

List指令:在模版中遍历数据

<table>
    <#list stus as stu>
        <tr>
            <td>${stu.index}</td>
            <td>${stu.name}</td>
            <td>${stu.age}</td>
            <td>${stu.money}</td>
        </tr>
    </#list>

</table>

Map指令

<table>
   <#list stuMap ?keys as k>
       <tr>
           <td>${k.index - 1}</td>
           <td>${stuMap[k].name}</td>
           <td>${stuMap[k].age}</td>
           <td>${stuMap[k].money}</td>
       </tr>
   </#list>

</table>

if指令

<td<#if stuMap[k].name =="cxy" ></#if>>${k.index - 1}</td>

空值处理:判断某变量是否存在使用??

<#if stuMap??>
    <tr> 
    <td<#if stuMap[k].name =="cxy" ></#if>>${k.index - 1}</td>
    <td>${stuMap[k].name}</td>
    <td>${stuMap[k].age}</td>
    <td>${stuMap[k].money}</td>
</tr>
</#if>

注意:缺失变量默认值使用!指定一个默认值,当变量为空时显示默认值。

内建函数

1、集合大小:${集合名?size}

2、日期格式化:

${today?date},显示年月日

${today?time},显示时分秒

${today?datetime},显示日期加时间

${today?string(yyyy-MM-dd)},按字符格式显示

3、${money?c}将数字以字符串形式显示,不然每三位添加一个逗号

4、将字符串转成对象 <#assign data=text?eval/>

静态化实现

1、使用模版文件静态化

2、使用模版字符串静态化

模版静态化测试代码

public class FreemarkerTest {
    @Test
    public void testGenerateHtmk() throws IOException, TemplateException {
        Configuration configuration = new Configuration(Configuration.getVersion());
        URL resource = this.getClass().getClassLoader().getResource("templates/");
        String path = resource.getPath();
        configuration.setDirectoryForTemplateLoading(new File(path));
        configuration.setDefaultEncoding("utf-8");
        Template template = configuration.getTemplate("test1.ftl");
        System.out.println(template);
        Map<String,Object> map = new HashMap<>();
        map.put("name","黑马程序员");
        String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
        System.out.println(content);
    }
}

到此,关于“FreeMarker的原理和应用”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI