迁移到RichFaces 4:HtmlAjaxCommandLink - > UICommandButton

时间:2013-02-19 19:19:45

标签: java jsf-2 richfaces ajax4jsf

我将旧项目转换为更现代的库版本。 旧版本使用以下 ajax4jsf 代码:

HtmlAjaxCommandLink link = new HtmlAjaxCommandLink()
link.addAjaxListener(new AjaxListener() {
    @Override
    public void processAjax(AjaxEvent event) { }
});

根据文档, HtmlAjaxCommandLink 已被Richfaces 4中的 UICommandLink 取代。

尽管如此,我似乎无法替代控件的 addAjaxListener

可以替换什么?

1 个答案:

答案 0 :(得分:4)

自JSF2以来,ajax已被JSF API标准化。支持客户端行为的所有组件都应实现ClientBehaviorHolder,后者又提供addClientBehavior()方法来添加客户端行为。 ajax的具体客户端行为实现是AjaxBehavior,它提供了addAjaxBehaviorListener()方法,这正是您正在寻找的方法。

总而言之,在您的特定情况下,可以按如下方式替换:

UICommandLink link = new UICommandLink(); // Note: you can also just use standard JSF HtmlCommandLink.
link.setId("linkId"); // Fixed ID is mandatory for successful processing.
link.setValue("click here"); // Not sure if you need it. Just to be complete.
AjaxBehavior ajaxAction = new AjaxBehavior();
ajaxAction.addAjaxBehaviorListener(new AjaxBehaviorListener() {
    @Override
    public void processAjaxBehavior(AjaxBehaviorEvent event) throws AbortProcessingException {
        System.out.println("Ajax behavior listener invoked."); // Do your actual job jere.
    }
});
link.addClientBehavior("action", ajaxAction); // Note: don't use "click" event!