【问题标题】:Migration to RichFaces 4: HtmlAjaxCommandLink -> UICommandButton迁移到 RichFaces 4:HtmlAjaxCommandLink -> UICommandButton
【发布时间】:2013-02-04 14:45:16
【问题描述】:

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

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

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

不过,我似乎无法很好地替换控件的 addAjaxListener

可以用什么代替?

【问题讨论】:

    标签: java jsf-2 richfaces ajax4jsf


    【解决方案1】:

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

    All with all,在您的特定情况下,可以按如下方式替换:

    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!
    

    【讨论】:

    • 我很难找到 AjaxBehavior、AjaxBehaviorEvent、AbortProcessingException 的导入。有没有机会,您可以从代码中复制粘贴这些导入?谢谢!
    • 点击链接。他们都指向javadoc。或者只需在 Eclipse 中按 Ctrl+Shift+O 并在必要时从 javax.faces 包中挑选。
    猜你喜欢
    • 2021-06-23
    • 1970-01-01
    • 2012-06-05
    • 2011-09-12
    • 1970-01-01
    • 1970-01-01
    • 2012-09-06
    • 2013-05-15
    相关资源
    最近更新 更多