我正在使用tapestry 5.2.4和AJAX。
在我的Test.tml中,我有一个表单:
<form t:id="form">
<t:label for="userName"/>:
<input t:type="TextField" t:id="userName" size="30"/>
</form>
显示变量“test”的区域:
<t:zone t:id="myZone" id="myZone">
<p>show test ${test}</p>
</t:zone>
现在我尝试将表单字段“userName”的值放入带有actionlink的区域中:
<t:actionlink t:id="SomeLink" zone="myZone" context="${userName}">update</t:actionlink>
这是java类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();
}
}
我认为这将“获取”表单字段userName的值,并通过actionlink将其传递给onActionFromSomeLink方法。该方法将变量“test”设置为输入,并显示区域。
这不起作用并抛出我不明白的错误:
Ajax失败:/example/test.somelink
的状态500:请求事件'action'(在组件Test:somelink上)未被处理;您必须在组件或其中一个容器中提供匹配的事件处理程序方法。
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.
如何实现一个从表单输入然后更新区域的函数?
干杯
答案 0 :(得分:7)
您使用的是错误的组件。 ActionLink
呈现HTML链接,它根本不与表单交互。虽然您可以为链接提供context
,但它绝对是静态的,并且不会从客户端的表单中检索值。 (context
主要用于区分对象,如果你有一个带有链接的项目列表,每个链接都对它们做了什么。)
您要做的是提交表单并更新您的区域。您必须将zone
参数添加到表单组件,并添加允许您提交表单的内容:
<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>
在你的班上:
@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;
}
}
如果您真的想使用链接提交表单,LinkSubmit
组件也可以让您这样做。