【问题标题】:Access nested object in freemarker访问freemarker中的嵌套对象
【发布时间】:2014-09-16 18:58:33
【问题描述】:

我正在使用 freemarker 生成 xml 输出并且在访问嵌套对象的属性时遇到问题我在"Stack Overflow" 上看到了这篇文章,但我仍然无法获取属性并获得无效的引用表达式。

代码示例

public class Inc {
private String id;
private List<BusinessAddress> businessAddress;
....

//get and setters for properties
.... 
}

//------------------------------
public class BusinessAddress{
private String id;
private Address details;
....

//get and setters for properties
....
}

//------------------------------
public class Address {
private String id;

//get and setters for properties
....

}

//--------------------------------------
public class FreemarkerTest {

public static void main(String[] args) {

    try
    {

    Inc inc = ......;


    Template freemarkerTemplate = null;
    Configuration configuration = new Configuration();
    configuration.setClassForTemplateLoading(FreemarkerTest.class, "/");

    String templateFile = "freemarker/template.ftl";
    StringWriter out = new StringWriter();
    freemarkerTemplate = configuration.getTemplate(templateFile);       
    Map<String,Object> contextPropsExpressioned = new HashMap<String,Object>();

    contextPropsExpressioned.put("payload", inc);
    freemarkerTemplate.process(contextPropsExpressioned, out);        

    System.out.println(out);
    out.flush();
    out.close();
    }
    catch(Exception ex)
    {
        System.out.println(ex.getMessage());
    }
}

而freemarker模板是

<#list payload.businessAddress as businessAddress>

    <EntityLocation>
        <nc:Location id="${businessAddress}Sub${details.id}" dataid="${businessAddress.id}">
        </nc:Location>
    </EntityLocation>

</#list>

偶数

<#list payload.businessAddress as businessAddress>

    <EntityLocation>
        <nc:Location id="${businessAddress}Sub${getDetails().id}" dataid="${businessAddress.id}">
        </nc:Location>
    </EntityLocation>

</#list

我收到的异常是

FreeMarker template error:

The failing instruction (FTL stack trace):
----------
==> ${details.id}  [in template "freemarker/template.ftl" at line 172, column 97]
----------
Tip: If the failing expression is known to be legally null/missing, either specify a default value.....

Java stack trace (for programmers):
----------
freemarker.core.InvalidReferenceException: [... Exception message was already printed; see it above ...]...

任何帮助将不胜感激。 谢谢

【问题讨论】:

  • 你试过${businessAddress.details.id}吗?
  • 是的,这是我尝试的第一个版本,我也遇到了同样的错误:“以下已评估为 null 或缺失:==> businessAddress.detail”
  • 因此,您可能在该列表或Address 字段中的某处有空值。
  • 我浏览了所有测试用例,才意识到其中一个地址没有通过 json 转换进行初始化。我确实恢复到 ${businessAddress.details.id},现在它的格式正确。在模板中添加了一些额外的检查以及空值,即使这种情况永远不会发生。感谢亚历山大的帮助
  • 您可以回答自己的问题,这将对未来的访问者有所帮助。

标签: freemarker


【解决方案1】:

最终使用了 Aleksandr 的建议并添加了空检查。

<#list payload.businessAddress as businessAddress>

   <#if (businessAddress.details??) >
       <EntityLocation>
          <nc:Location id="${businessAddress.details.id}" dataid="${businessAddress.id}">
          </nc:Location>
       </EntityLocation>
   </#if>
</#list>

【讨论】:

  • 对我不起作用,我没有空值仍然 Freemarker 看不到嵌套属性。有什么想法吗?
  • 先生,如果我的嵌套键类似于“capital-value”,那该怎么办?
猜你喜欢
  • 2011-12-27
  • 2013-05-28
  • 1970-01-01
  • 1970-01-01
  • 2013-06-12
  • 2022-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多