Tapestry 5.2: Update Zone with data from Form
I'm playing with tapestry 5.2.4 and AJAX.
In my Test.tml I have a form:
<form t:id="form">
<t:label for="userName"/>:
<input t:type="TextField" t:id="userName" size="30"/>
</form>
And a zone that displays the variable "test":
<t:zone t:id="myZone" id="myZone">
<p>show test ${test}</p>
</t:zone>
Now I try to put the value of the form field "userName" into the zone with an actionlink:
<t:actionlink t:id="SomeLink" zone="myZone" context="${userName}">update</t:actionlink>
Here's the java class Test.java:
public class Test {
@Persist
@Property
private String userName;
@Property
private String test;
@InjectComponent
private Zone myZone;
@Component
private Form form;
Object onActionFromSomeLink(String input) {
test = input;
return myZone.getBody();
}
}
I thought this would "take" the value of the form field userName and pass it with an actionlink to the method onActionFromSomeLink. The method sets the variable "test" to input and the zone is display开发者_运维百科ed.
This does not work and throws an error I do not understand:
Ajax failure: Status 500 for /example/test.somelink
: Request event 'action' (on component Test:somelink) was not handled; you must provide a matching event handler method in the component or in one of its containers.
Communication with the server failed: Request event 'action' (on component Test:somelink) was not handled; you must provide a matching event handler method in the component or in one of its containers.
How can I implement a function, that takes input from a form and then updates a zone?
Cheers
You're using the wrong component. An ActionLink
renders an HTML link, it does not at all interact with forms. While you can provide a context
to the link, it is absolutely static and does not retrieve values from forms on the client side. (context
is useful mainly to discriminate between objects if you have a list of items somewhere with a link each that does something to them.)
What you're trying to do is to submit the form and have that update your zone. You'll have to add the zone
parameter to the form component, and add something that lets you submit the form:
<form t:id="form" t:zone="myZone">
<t:label for="userName"/>:
<input t:type="TextField" t:id="userName" size="30"/>
<input type="submit" t:type="Submit" />
</form>
And in your class:
@Inject
private Request request;
@OnEvent(EventConstants.SUCCESS)
Object formSubmitted(){
//return zone content only if AJAX request, page otherwise
if (request.isXHR()) {
return myZone.getBody();
} else {
return this;
}
}
If you really want to use a link to submit the form, the LinkSubmit
component lets you do that, too.
精彩评论