2013-04-09 61 views
1

此問題是關於Tapestry組件問題。無法將客戶端值'null'轉換回服務器端對象

我正在尋找一個解決方案,正是這個問題,沒有任何解決方法或替代方法,如何實現這個接口。

  1. 考慮Tapestry形式ajaxformloop元件:

    <tr t:type="ajaxformloop" t:id="items" source="getItems()" value="item">

    ...

    </tr>

  2. getItems()內部類方法返回合成組合(List接口)持久化對象和尚未持久的新增項目(其中有null id)。

  3. 當提交表單我收到此錯誤:

    Unable to convert client value 'null' back into a server-side object

此錯誤發生之前onSuccessFromSave()方法(save是提交鏈接的ID)。

我想知道,我怎麼能管理這樣的不持久對象與ajaxformloop以防止這種錯誤。實際上,我想在我的onSucceccFrom...()方法中保存(在DB中)這些項目。

我在這裏錯過了什麼?

回答

1

其實我錯過了我的ajaxformloop容器的定製ValueEncoder。提到的錯誤是由該組件的默認encoder產生的。

自定義編碼器應該以這樣的方式進行設置:

<tr t:type="ajaxformloop" t:id="items" source="getItems()" value="item" encoder="itemEncoder">

其中itemEncoder是在Java類@Property標註的字段:

@Property 
private ValueEncoder<MyItem> itemEncoder = new ValueEncoder<MyItem>() { 
    @Override 
    public String toClient(MyItem value) { 
     return value.id != null ? value.id.toString() : ""; 
    } 

    @Override 
    public MyItem toValue(String clientValue) { 
     if (clientValue != null && !clientValue.isEmpty()) { 
      Long id = Long.parseLong(clientValue); 
      return (MyItem) session.get(MyItem.class, id); 
     } 
     return new MyItem(); 
    } 
};