[Tapestry5] tapestry5找不到actionlink的响应方法

hxzon 2011-07-05
tapestry5找不到actionlink的响应方法

我的页面xxPage中含有zzComp自定义组件,zzComp组件中使用了actionlink组件,

使用代码如下:
<t:actionlink context="material.id" t:id="selectItem" zone="contractItemForm">添加到合同</t:actionlink>
这是写在zzComp的tml文件中的。

然后我在xxPage中写响应方法如下:
@OnEvent(component = "selectItem")
Object selectItem(String materialId) {
}

结果出错:
Request event 'action' (on component contract/xxPage:zzComp.selectitem) was not handled; you must provide a matching event handler method in the component or in one of its containers.

请教,响应方法只能写在zzComp组件的java类中,不能写在xxPage中?是否可以有办法写在xxPage中,我不想写在zzComp中。
jimlaren 2011-07-06
第一种方法:@OnEvent(component = "selectItem",value="action")
第二种方法:去掉@OnEvent,直接onActionFromSelectItem

在页面调用,你可以把页面类注进来,调用页面类的方法。
hxzon 2011-07-06
@OnEvent的value默认就是action,所以可以省略。
注入页面类我没试过,怀疑会丢失一些当前页面参数信息。
所以我用的是下面的方法:
在xxComp组件中
// @OnEvent(component = "selectItem")
// Object selectItem(String materialId){
// return ((xxPage)componentResources.getPage()).selectItem(materialId);
// }
也就是转发的形式。
hxzon 2011-07-06
我原来希望的是,actinlink触发事件方法,如果在包含它的容器(组件)找不到,就继续在上一层容器(组件)找,直到找到最高层容器(页面)类。
jimlaren 2011-07-06
@Inject
private ComponentResources resources;

public String getSelectItemLink(){
return resources.createEventLink("selectItem").toAbsoluteURI();
}

<a href="${selectItemLink}">select</a>


页面类

Object onSelectItem(){

}
hxzon 2011-07-07
万分感谢jimlaren ,试用了一下解决了我的问题。
而且我才注意到一个差别,原来使用actionlink,生成的url如下:
xxPage.zzComp.selectItem?t:ac=abc
你的方法生成这种url,如下:
xxPage.zzComp:selectItem?t:ac=abc
前者是点号,后者是分号,前者是组件(action事件),后者是事件(selectItem事件)。
再次谢谢jimlaren
Global site tag (gtag.js) - Google Analytics