2016-08-04 30 views
0

首先,我讀了this question,但它沒有解決我的問題。使用Freemarker顯示任意Java對象及其字段表

我想創建一個表,它將顯示任意的Java對象列表。當我說「任意」時,我的意思是對象的數量是任意的,並且對象的類型是任意的(儘管它們都將是同一類的實例)。我希望這個表的行代表對象,而列代表每個對象的實例變量(電子表格樣式,基本上)的值。但是,第一行將只是實例變量名稱的列表。

我目前正在測試這個對象的所有變量都設置爲private,但我提供了相關的getter和setter。

這是我的Java代碼片段。我從Oracle Coherence緩存中提取對象,並將它們放入ArrayList中。然後我做了實例變量名的字符串數組:

 /** 
    * Get objects in cache and add to ArrayList. 
    */ 

    for(Iterator iter = currentCache.entrySet().iterator();iter.hasNext();){ 
     Map.Entry entry = (Map.Entry)iter.next(); 
     String key = (String) entry.getKey(); 
     Pof tempPof = (Pof)entry.getValue(); 
     tableList.add(tempPof); 
     System.out.println("one loop"); 
    } 

    request.setAttribute("beans",tableList); 

    System.out.println("Size of tableList is: " + tableList.size()); 
    /** 
    * Build an array containing the variable names of cached objects. 
    */ 

    Field[] fields = Pof.class.getDeclaredFields(); 
    String[] variableNames = new String[fields.length]; 

    for(int j = 0; j < fields.length;j++){ 
     variableNames[j] = fields[j].getName(); 
     System.out.println(variableNames[j]); 
    } 

    request.setAttribute("colNames",variableNames); 


    /** 
    * numCols determines the number of columns displayed in the table. 
    */ 

    int numCols = fields.length; 
    String[] fieldStrings = new String[numCols]; 
    request.setAttribute("numCols",numCols); 
    Pof thing = (Pof) tableList.get(0); 

下面是相關.ftl文件的一個片段:

<table border = "1px"> 
     <thead> 
      <tr> 
       <th colspan="${numCols}">${selectedCache}</th> 
      </tr> 
      <tr> 
       <#list colNames as colName> 
        <td>${colName}</td> 
       </#list> 
      </tr> 
     </thead> 
     <tbody> 
      <#list beans as bean> 
       <tr> 
        <#list colNames as colName> 
         <td>${bean[colName]}</td> 
        </#list> 
       </tr> 
      </#list> 
     </tbody> 

    </table> 

這讓我以下錯誤:


freemarker.core.InvalidReferenceException:以下評估爲null或缺失: ==> bean [colName] [模板「front.ftl」在第46行,第35列]

提示:這是導致此錯誤的最後一步,而不是之前的錯誤。

!提示:如果發生故障的表達被稱爲是指合法的東西,有時候空或丟失,或者指定一個像myOptionalVar myDefault的默認值,或使用<#如果myOptionalVar ??>時存在的<的#else>當缺失。 (這些只覆蓋了表達式的最後一步;爲了覆蓋整個表達式,使用括號:(myOptionalVar.foo)!myDefault,(myOptionalVar.foo) FTL堆棧跟蹤(「〜」表示嵌套相關): - 在失敗:$ {豆[COLNAME]} [模板 「front.ftl」 在第46行,列33]

at freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:134) 
at freemarker.core.EvalUtil.coerceModelToTextualCommon(EvalUtil.java:451) 
at freemarker.core.EvalUtil.coerceModelToStringOrMarkup(EvalUtil.java:374) 
at freemarker.core.DollarVariable.calculateInterpolatedStringOrMarkup(DollarVariable.java:96) 
at freemarker.core.DollarVariable.accept(DollarVariable.java:59) 
Truncated. see log file for complete stacktrace 

的問題似乎是我的FTL語法;也就是說,它不「t等表達式$ {豆[COLNAME]}

問題:

1)語法錯了嗎?

2)這是Freemarker無法做到的事情嗎?

3)我應該嘗試另一種方法嗎?例如,我應該創建一個包含實例變量值的數組(或其他數據結構)的每個桶的數組嗎?

回答

0

它應該工作,提供:

  • Pof是一個公共類
  • 有一個公共Pof.getFoo()方法對每個colName"foo"
  • getFoo()返回非null值。如果它有時返回null,則必須指定要顯示的內容,例如:${bean[colName]!'-'}
+0

生成了我的表格!但是有很多空字段。當我檢查發送給FreeMarker的對象的值時,它們不是null。 似乎沒有遵循變量類型或值的模式。但我注意到返回null的變量以單個大寫字母開頭(例如「Big_Contracts」),而FreeMarker成功訪問的實例變量則以兩個大寫字母(例如「LG_Contracts」)開頭。 當用於從散列檢索數據時,FreeMarker會對這些字符串做些什麼嗎? – murdoc1871

+0

FreeMarker不關心字段(當然,它可以配置爲讀取公共字段,但現在已經不在話題了)。它關心JavaBeans的屬性。所以變量名是根據JavaBeans規範指定的規則從getter方法名中推導出來的。對於'getBigContacts',屬性名稱是'bigContacts'。奇怪的是,對於'getLGContacts()',屬性名稱是'LGContacts' ...這些規則不是由FreeMarker定義的,而是由JavaBeans規範定義的。 – ddekany

相關問題