Search code examples
javatapestrytml

Pass a value to method onSucces


I am working with tapestry in java and I have in a tml file a loop with a button in each row. I need to pass the value of the element in the row to the onSuccess() method when the button is clicked. I've tried it with t: context = "value" in the button but it does not work and I can't find a solution.

TML:

<tr t:type="Loop" t:source="movieList" t:value="movie">
    <td>
        <a href="#" t:type="PageLink" t:page="index">
            ${movie.title}
        </a>
    </td>
    <td>
        ${movie.score}
    </td>
    <td>
        <form t:type="Form" class="form-horizontal" t:id="deleteMovie">
            <button type="submit" t:context="movie.movieId" class="btn btn-primary">
                <img src="${context:i/basura.png}" width="25" heigth="25"/>
            </button>
        </form>
    </td>
</tr>

Java method:

 @OnEvent(value = "success", component ="deleteMovie")
 Object[] onSuccesFromDeleteMovie(Long movieId) throws InstanceNotFoundException {
     movieService.removeMovie(movieId);
     return new Object[] {startIndex};
 }

Solution

  • It "doesn't work" because your button is a plain HTML button, not a Tapestry component. In this case t:context will be rendered as a plain HTML attribute, you can verify that by inspecting resulted HTML.

    To let Tapestry know that your button is actually a Tapestry Submit component, you can add the t: prefix to the type attribute, i.e.:

    <button t:type="submit" t:context="movie.movieId" ... />

    Foreseeing your next question: returning Object[] from an event handler won't work because it's not a supported method return value.

    For such a simple use case you could use plain EventLink with context, you don't have to submit a form to trigger server-side event handler.

    Also maybe this JumpStart's AjaxFormLoop example will be helpful if you decide to use Ajax at some point.