0

I am new to tapestry just creating my first application.

I have a form where I am creating an "Object" consisting of 2 other objects, Customer and Country.

 <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>

In my javaclass I have

   @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");
   }

and in my ObjectBean I have 2 attributes, country and customer defined as Strings with corresponding getters and setters.

private String aCustomer; private String aCountry;

My CustomerEncoder looks like the following

  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();
   }

When I create a new Object my customer and country becomes null. What am I doing wrong, Should my ObjectBean have Objects instead of String like Customer and Country? Is my Encoder wrong or is there anything else. If I try with having just a primitive String instead of an Object that needs Encoders the value is submitted.

All help and comments are welcome!

Patrik
  • 94
  • 2
  • 9

1 Answers1

0

I'm unable to "comment" on your question to ask you about some stuff (I think may be I don't have enought reputation !), but your scenario isn't clear so here is what I think might help. You should take care of 2 things:

  1. Initialize your anewobject and it's associated objects (customer & country) on the preparation event of your form in which the beaneditor is enclosed.

    @OnEvent(component="your-form-id",value=EventConstants.PREPARE)

  2. Tapestry5 versions before 5.4 (http://tapestry.apache.org/release-notes-54.html) implements the redirect after post mechanism so you lose your request data and so you need to @Persist some of your page variables to keep their value between even one form submit and it's response !

Muhammad Gelbana
  • 3,890
  • 3
  • 43
  • 81