知识点

相关文章

更多

最近更新

更多

freemarker 空值处理

2019-03-09 07:34|来源: 网路

先看示例:

${user.name?if_exists },

${user.name?default(‘领悟书生’)}//默认值领悟书生
${ user.name!"www.656463.com"}//默认值www.656463.com


如果freemarker出现空值不处理的时候,会报empty Value Expression xxx is undefined


freemarker中空值的多种处理方法:

1、使用和上面示例一样的处理方法,也是用得最多的方法

   ${ user.name!"www.656463.com"}

2、使用escape对所有的变量进 空值处理

   <#escape x as x!""></#escape> 全部替换为空字符串

   <#noescape></#noescape> 如果不替换

  1. <#escape identifier as expression>  

  2.  ...  

  3.  <#noescape>...</#noescape>  

  4.  ...  

  5. </#escape>  

  1. <#escape x as x?html>  

  2.  First name: ${firstName}  

  3.  <#noescape>Last name: ${lastName}</#noescape>  

  4.  Maiden name: ${maidenName}  

  5. </#escape>  


3、设置classic_compatible=true把null替换为空字符串

3.1、通过Configuration设置classic_compatible=true

   Configuration cfg = new Configuration(); cfg.setClassicCompatible(true);//设置属性

3.2、把Environment的setClassicCompatible设置classic_compatible=true

   Environment env = template.createProcessingEnvironment(root, out);
  env.setClassicCompatible(true);

3.3、在ftl文件的前面加入<!--#setting classic_compatible=true-->

3.4、通过spring的配置文件设置classic_compatible=true

<bean id="freemarkerConfig"
 class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
  <property name="freemarkerSettings">
    <props>
      <prop key="classic_compatible">true</prop>
    </props>
  </property>
</bean>

3.5、在classpath下添加freemarker.properties文件,加入classic_compatible=true



相关问答

更多
  • 直接赋值就可以了呀 <#assign x=one/> 如果one是数值型,直接可以加减乘除操作,比如 <#assign x=one+1/>
  • 可能是pager这个类为空的情况出现的这个问题,当然也可能是pager.list为空造成的。 笨点的方法,你先去<#if pager.list?exists>判空去,如果这个也出错,那就是pager为空造成的了。 至于处理方式,你可以保证pager.list不会为空。也可以在判断长度之前先加空值判断。
  • <#assign sifvar=ifvar?int> <#if sifvar?int=1> ifvar的值为 ${ifvar} 另外,struts2里面用freemarker的话,不需要那么写的,只要不是private变量,都能直接引用的。
  • springmvc+freemarker[2022-02-28]

    用c:forEach 标签, 里面取Users类属性 循环 即可
  • <#if actionStr?contains("update") > <#else>
  • 你可以使用?? 测试员: 这将检查对象的属性是否不为空: <#if object.attribute??> 这检查对象或属性是否不为空: <#if (object.attribute)??> 资料来源: FreeMarker手册 You can use the ?? test operator: This checks if the attribute of the object is not null: <#if object.attribute??> This che ...
  • 我为我的项目寻找了类似的东西,但没有这样的东西存在。 跨平台模板语言的概念是最近的,最常用的是mustache.js。 考虑到Freemarker严重依赖Java以及它的宏和复杂的文件包含,我认为这将是一个非常难以编写的解析器。 Found jmarker, parser for freemarker in JS. http://sanshi.me/p/jmarker/
  • 只公开公共类的方法/属性,因此User类必须是public(或者getName()必须从公共类/接口继承)。 Only the methods/properties of public classes are exposed, thus the User class must be public (or getName() must be inherited from a public class/interface).
  • 获取Template对象本身就足以进行语法验证。 验证模板是否会在运行时失败可能会非常棘手,具体取决于应用程序,因为您需要一个类似于真实数据模型的数据模型。 但是我们假设你可以在你的情况下提供这样的数据模型。 那么问题是ModelAndView不是你的模型。 顾名思义,这就是你的模型和观点。 它只包含你的模型,里面。 (FreeMarker不依赖于Spring,因此只需将该对象用作泛型JavaBean,因此您的变量将是ModeAndView对象本身的JavaBean属性。)尝试将ModelAndView.g ...
  • 由于FreeMarker使用java.beans.Introspector来发现bean属性和操作,因此它确实BeanInfo的内容。 问题是,在您的情况下,将返回具有冲突名称的PropertyDescriptor -s和MethodDescriptor -s。 由于模板语言没有单独的属性和方法命名空间,因此必须将另一个命名空间。 默认情况下,方法阴影属性(现在不太实用,因为流畅的API通常有像Foo foo()而不是Foo getFoo() )的方法。 您可以通过将methodAppearanceFine ...