Freemaker教程4(处理空值)

本文介绍了Freemarker模板引擎中处理未定义变量的方法,包括使用感叹号进行默认值设定、通过if标签判断变量是否存在以及展示具体的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文转载自:http://sishuok.com/forum/posts/list/5157.html


Freemaker中,如果在ftl文件中直接写一个${没有的元素},然后程序执行会报freemarker.core.InvalidReferenceException


  1. 当你不确定一个值一定存在时,可以在这个值的后面加上!  形如${user.xxx!}

当你想要如果一个值不存在时,希望可以看到一些提示信息,可以这么写:

${user.xxx!”user.xxx不存在”},这样当值不存在时,会显示双引号中的字符串。

    注:如果你写${user.xxx.yyy!},如果${user.xxx}值不存在一样会报错,想要这种写法不报错,可以这么写:${(user.xxx.yyy)!”提示信息”}

 

  1. 你也可以使用if标签判断:

<#if (user.xxx)?? > //??表示存在

       ${user.xxx}存在

    <#else>

       user.xxx不存在

</#if>

 

  1. 实际的代码:
  1. 新建一个ftl文件(06.ftl):

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

    ${user.username} --- ${user.nickname} --- ${user.xxx!"user.xxx不存在"}

    <br/>

    ${(user.group.name)!"user.group.name不存在  需要加(user.group.name)"}

    <br/>

    <#if (user.xxx)?? > //??表示存在

       ${user.xxx}存在

    <#else>

       user.xxx不存在

    </#if>

   

</body>

</html>

  1. 代码:

    @Test

    public void  testDealNullValue()

    {

       Map<String , Object> root = new HashMap<String, Object>();

       User u1 = new User(1 , "张鸿洋" ,21 , "绿茶");

       root.put("user", u1);

       root.put("username", "管理员");

       //后面会介绍使用到的方法

       utils.print2File("06.ftl", root, "d:/freemaker/test6.html");

       utils.print2Console("06.ftl", root);

}

 

  1. 运行结果:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

    张鸿洋 --- 绿茶 --- user.xxx不存在

    <br/>

    user.group.name不存在  需要加(user.group.name)

    <br/>

   

    user.xxx不存在

   

</body>

</html>

 

 

 

 

 

上面使用到的方法:

    /**

     * 根据模版获得一个指定的模版

     * @param name

     * @return

     */

    public Template getTemplate(String name )

    {

       try {

           //获得配置对象

           Configuration configuration = new Configuration() ;

           //设置模版的文件夹路径,本人在src下新建了一个ftl文件夹

           configuration.setClassForTemplateLoading(this.getClass(), "/ftl");

           //更具名字获得指定的一个模版

           Template template = configuration.getTemplate(name);

          

           return  template;

       } catch (IOException e) {

           e.printStackTrace();

        }

       return null ;

    }

   

    /**

     * 根据指定的名字获得指定的模版,传入键值对

     * @param name

     * @param root

     */

    public void print2Console(String name , Map<String,Object> root)

    {

       try {

          

           Template template = getTemplate(name);

           template.process(root, new PrintWriter(System.out));

       } catch (TemplateException e) {

           e.printStackTrace();

       } catch (IOException e) {

           e.printStackTrace();

       }

    }

   

    public void print2File(String name , Map<String,Object> root , String fileName)

    {

       try {

           Template template = getTemplate(name);

           template.process(root, new PrintWriter(new File(fileName)));

       } catch (TemplateException e) {

           e.printStackTrace();

       } catch (IOException e) {

           e.printStackTrace();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值