freemarker语法

在jsp中${user.role.name}如果role没有值,直接不显示, 但是freemarker会报空指针异常

${(user.role.name)!}                        有值就显示,没有有值不不显示
${(user.role.name)!"当前没有值"}    有值就显示,没有就先显示后面赋的值

?? 用于判断是否为空
<#if user.role??>
当前用户没有该角色
<#else>
当前用户有该角色
</#if>

${name[0]}  取name对应值得第一个字母
${name[1..8]} 取下标 1到8所有字母

算数运算
<#assign a=10>
<#assign b=5>
${a+b};
${a-b};
${a*b}
${a/b}
${a%b}

取整
#{20.2;m0M0} 20   m0表示最少0位小数;M0表示最多0位小数  注意用#
#{20.2;m0M1} 20.2
${20.87?stirng('#.#')}  20.8
${20.87?stirng('#')}  20


比较运算
<#if a+b gte 12 || a-b lt 6>
${a+b}
<#else>
${a-b}
</#if>

内置函数
<#assign data="abcd01234">
${data?cap_first}  第一个字母大写
${data?lower_case} 所有的字母小写
${data?upper_case} 所有的字母大写

<#assign floatData=12.34>
${floatData?int}  数值取整
${floatData?size} 取集合长度
${dataTime?string("yyyy-mm-dd")}

空判断和对象集合
<#if uerList??>
	<#list userList as user>
		${user.id};  ${user.name}
	</#list>
<#else>
	${user!"变量为空给一个默认值"}
<#if>

map集合
<#assign map={"name":"小明","age":22}>
${map["name"]}  直接通过key取value值
${map.name}  这样也可以
通过key遍历map
<#list map?keys as key>
	key: ${key}; value: ${map[key]} 或者${map.key}
<#list>
通过value遍历map
<#list map?values as value>
	value: ${value}
</#list>

list集合(list标签body里面换行,整个list前后会有空格)
<#assign list=["my","name","is","guyue"]>
<#list list as item >
	${item}
<#list>

自加
<#assign i=0>
<#list "my","name","is","guyue"] as item>
<#assign i=i+1>
</#list>
输出:${i}               //i=4


 日期型:等价于java中的Date类型, 不同之处在于不能直接输出,需要转换成字符串再输出
date,time,datetime将字符串转换为日期 
<#assign date1=”2009-10-12”?date(“yyyy-MM-dd”)> 

<#assign date2=”9:28:20”?time(“HH:mm:ss”)> 

<#assign date3=” 2009-10-12 9:28:20”?time(“HH:mm:ss”)> 
输出的转为字符串输出
${date1?string}

https://blog.csdn.net/zgf19930504/article/details/50768033
https://jbeduhai.iteye.com/blog/791138

el+jstl
https://blog.csdn.net/yuzhiqiang_1993/article/details/81274676

include指令
引入其它文件 <#include "hello.ftl"/>

macro 宏指令
<#macro mo> 
	macro -- ${name} 定义无参数的宏
</#macro>

<#macro moArgs a b c>
	macro -- ${a+b+c}  定义带参的宏
</#macro>

marcro : < @moArgs a=1 b=2 c=3/> 使用带参数的宏

命名空间:
<#import "otherFreeMarker.ftl" as otherFtl>
${otherFtl.otherName}
<@otherFtl.addMethod a=10 b=20 />
<#assign otherName="修改otherFreeMarker.ftl中的otherName变量值"/>
${otherFtl.otherName}
<#assign otherName="修改otherFreeMarker.ftl中的otherName变量值" in otherFtl />
${otherFtl.otherName}

新建一个hello.ftl文件;该文件里面内容 ${hello}
生成的html的java代码

public class FreemarkerTest {
	@Test
	public void genFile() throws Exception{
		//创建一个configuration对象
		Configuration config =new Configuration(Configuration.getVersion());
		//设置模板文件所在的路径;要加载模板hello.ftl所在的文件夹
		config.setDirectoryForTemplateLoading(new File("D:/gitspace/springboot/src/main/resources/ftl"));
		//设置模板文件的字符集
		config.setDefaultEncoding("utf-8");
		//加载模板hello.ftl,创建该模板对象
		Template template=config.getTemplate("hello.ftl");
		//构建一个要显示的数据集合;可以是pojo  map等
		Map <String,Object>dataModel =new HashMap<>();
		//承载数据
		dataModel.put("hello", "this is my first freemarker test");
		//创建 一个writer对象,指定生成的html文件;
		Writer out=new FileWriter(new File("D:\\Users\\EX-HUYAN001\\Desktop\\test/hello.html"));
		//调用模板对象的Process()方法输出文件
		template.process(dataModel, out);
		//关闭流
		out.close();
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值