2012-04-24 23 views
0

我是tapestry的新手,只是創建我的第一個應用程序。使用ValueEncoders時未設置Tapestry Bean屬性

我在那裏,我創造一個「對象」包括其他兩個對象,客戶和國家的一種形式。

<t:beaneditform t:id="createMyObject" t:object="anewobject" rt:submitlabel="Create Object"> 
     <p:customer> 
      <t:label for="Customer"/> 
      <t:select t:id="customer" value="aCustomer" model="aCustomerSelectModel" encoder="customerEncoder"/> 
     </p:customer> 
     <p:country> 
      <t:label for="Country"/> 
      <t:select t:id="country" value="aCountry" model="aCountrySelectModel" encoder="countryEncoder"/> 
     </p:country> 

在我javaclass我有

@Property 
    private Customer aCustomer; 
    @Property 
    private Country aCountry; 

    @Property 
    private ObjectBean aNewObject; 

    public New() 
    { 
     // create a SelectModel from the list of customers 
     aCustomerSelectModel = aSelectModelFactory.create(aCustomers, "name"); 
     aCountrySelectModel = aSelectModelFactory.create(aCountries, "name"); 
    } 

,在我ObjectBean我有2個屬性,國家和客戶定義爲字符串與相應的getter和setter。

private String aCustomer; private String aCountry;

我CustomerEncoder如下所示

public class CustomerEncoder implements ValueEncoder<Customer>, ValueEncoderFactory<Customer> 
{ 
    @Override 
    public String toClient(Customer pCustomer) 
    { 
     // return the given object's ID 
     return String.valueOf(pCustomer.getId()); 
    } 

    @Override 
    public Customer toValue(String id) 
    { 
     // find the color object of the given ID in the database 

     return new Customer("John", "Smith"); 
    } 

    // let this ValueEncoder also serve as a ValueEncoderFactory 
    @Override 
    public ValueEncoder<Customer> create(Class<Customer> type) 
    { 
     return this; 
    } 


    void onSubmitFromCreateCustomization() 
    { 
     String vCustomer = aNewObject.getCustomer(); 
     String vCountry = aNewObject.getCountry(); 
    } 

當我創建一個新的對象我的客戶和國家變成零。 我做錯了什麼,我的ObjectBean應該有像客戶和國家的字符串而不是字符串? 是我的編碼器錯了還是還有別的。如果我嘗試使用原始字符串而不是需要編碼器的對象,則會提交該值。

歡迎所有的幫助和評論!

回答

0

我無法對你的問題,以「評論」來問你關於一些東西(我想可能是我沒有足夠多的聲譽!),但你的情況是不是清楚,所以這裏是我認爲可能幫幫我。你應該照顧兩兩件事:

  1. 初始化您anewobject和它在其中的beaneditor被封閉的形式編寫事件相關聯的對象(客戶&國家)。

    @OnEvent(組分= 「您的形式-ID」,值= EventConstants.PREPARE)

  2. Tapestry5之前版本5.4(http://tapestry.apache.org/release-notes-54.html )在發佈機制之後實現重定向,因此你失去了你的請求數據,所以你需要@Persist你的一些頁面變量保持它們之間的值甚至是一個表單提交和它的響應!

相關問題