Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encoder with <t:loop> component in Tapestry

Tags:

tapestry

I have one Page which is displaying details of some Client. I am using t:loop to display some data. In t:loop I am just passing source and value. So far so good, my page is working fine. But when I try to submit the Page it suddenly give me the Exception.

“Could not find a coercion from type java.lang.String to type [addressUsageValue] Available coercions:……….”

Below is the sample code

<t:loop source="addressUsageInfo" value="addressUsageValue">
                  <tr>
                              <td>${addressUsageValue?.usage}</td>
                              <td>${addressUsageValue?.address}</td>
                              <td>${addressUsageValue?.postCode}</td>
                              <td>${addressUsageValue?.city}</td>
                              <td>${addressUsageValue?.country}</td>
                  </tr>
</t:loop>

I did some goggling and find below references.

http://tapestry.apache.org/5.3.3/apidocs/org/apache/tapestry5/corelib/components/Loop.html https://issues.apache.org/jira/browse/TAP5-609

So I have created encoder for Loop. Below is the sample code. In below toClient() method I have randomly returned any value and in toValue() method I am returning null.

private final ValueEncoder<DtoAddressUsageInfo> addressUssageEncoder = 
    new ValueEncoder<DtoAddressUsageInfo>() {

    public String toClient(DtoAddressUsageInfo value) {
          return String.valueOf(value.getUsage());
    }

    public DtoAddressUsageInfo toValue(String clientValue) {
          return null;
    }

};

Now my code is working fine and I am able to submit the form.

Here my doubt comes

First – I am not able to understand why encoder is required when using loop?? And if it is required to submit the form then why it is not Mandatory parameter??

Second – I have just implemented the Encoder without any logic. I am not able to understand where toValue() and toClient() method is used and what is the purpose?

Third – when I submit the Page why form required Encoder??

like image 533
Mahendra Athneria Avatar asked Jun 22 '12 06:06

Mahendra Athneria


2 Answers

The formState and encoder parameters solve the following problem: When the form is submitted, how do you make sure the values from the form go to the objects they are intended for?

If the collection you're iterating over does not change between the rendering of the form and the form submission, then the simplest solution is to set formState="iteration" (explained nicely in the docs).

If, however, the collection could change between render and submit, you will have to encode information on which loop iteration is which object. This is done with a ValueEncoder (again, explained nicely in its documentation). A common implementation could for example encode a database ID into the form (toClient()) when rendering, and load the entity back from the database when the form is submitted (toValue()).

All of this is also explained well in the Loop component documentation that you found.

like image 102
Henning Avatar answered Sep 30 '22 12:09

Henning


Henning's answer helped me as well. The value for my loop would turn up null only when trying to set a checkbox for a row in my loop. The encoder works and data turns up fine. The checkbox is set with a setter and retrieved with a getter per iteration, per row. The actual Boolean value lives inside a wrapper around the data object. The loop is given a list of these wrapper objects. An ''onClick' event submits the form. But per iteration my wrapper object(loop value) would always be null. I added formstate="iteration" and now checkboxes keep their state.

<t:loop t:source="treeNodeList" value="treeNode" 
t:encoder="treeNodeEncoder" formState="iteration">
    <tr style="text-align:center;">
        <td class="table-checkbox" id="data-table-1st-col">
        <input t:type="checkbox" value="currentSelectedTreeNode" 
        onclick="this.form.submit();"/>

    ....

        </td>
    </tr>
</t:loop>
like image 39
Mark Espinoza Avatar answered Sep 30 '22 13:09

Mark Espinoza