1

我有一些从 db 搜索结果的表格。如何每隔 x 秒自动显示 db 的结果?这意味着每 x 秒提交一次按钮。我发现关于刷新的是这个类:

add(new AjaxSelfUpdatingTimerBehavior(Duration.seconds(10)) {
    private static final long serialVersionUID = 1;       
    });

这只是刷新页面而不是提交表单。然后我想从检票口示例页面中获得灵感:http: //www.wicket-library.com/wicket-examples/ajax/clock? 1 但是当我点击源代码时,我只看到返回的网址:内部错误

更新:

我尝试调用简单的javascript,然后从js提交表单:

    add(new AjaxSelfUpdatingTimerBehavior(Duration.seconds(10)) {
        private static final long serialVersionUID = 1L;

        @Override
        protected void onPostProcessTarget(AjaxRequestTarget target) {
            target.appendJavaScript("alert('hello');");
        }

    });

但没有成功

4

3 回答 3

2

您需要结合AbstractAjaxTimerBehaviour来引发触发AjaxFormSubmittingBehaviour的事件。我没有尝试过,但从我的检票口经验和这两种行为的 JavaDocs 来看,它应该可以工作。

由于似乎需要一些演示代码...

免责声明:这是由两个提到的课程中的 Copy'n'Pasting 在几分钟内完成的。所以这不是好的代码,经过测试的代码或任何我在没有深入了解的情况下投入生产的东西。但它似乎工作。

首先,您需要组合行为:

public abstract class AjaxTimerFormSubmitBehavior extends AbstractAjaxTimerBehavior {

    /**
     * should never be accessed directly (thus the __ cause its overkill to
     * create a super class), instead always use #getForm()
     */
    private Form<?> __form;

    private boolean defaultProcessing = true;

    /**
     * @param updateInterval
     */
    public AjaxTimerFormSubmitBehavior(Duration updateInterval) {
        this(null, updateInterval);
    }

    public AjaxTimerFormSubmitBehavior(Form<?> form, Duration updateInterval) {
        super(updateInterval);
        __form = form;

        if (form != null) {
            form.setOutputMarkupId(true);
        }
    }

    @Override
    protected void onTimer(final AjaxRequestTarget target) {
        getForm().getRootForm().onFormSubmitted(new IFormSubmitter() {
            public Form<?> getForm() {
                return AjaxTimerFormSubmitBehavior.this.getForm();
            }

            public boolean getDefaultFormProcessing() {
                return AjaxTimerFormSubmitBehavior.this.getDefaultProcessing();
            }

            public void onSubmit() {
                AjaxTimerFormSubmitBehavior.this.onSubmit(target);
            }

            public void onError() {
                AjaxTimerFormSubmitBehavior.this.onError(target);
            }
        });
    }

    /**
     * @return Form that will be submitted by this behavior
     */
    public final Form<?> getForm() {
        if (__form == null) {
            __form = findForm();

            if (__form == null) {
                throw new IllegalStateException(
                        "form was not specified in the constructor and cannot "
                                + "be found in the hierarchy of the component this behavior "
                                + "is attached to: Component="
                                + getComponent().toString(false));
            }
        }
        return __form;
    }

    /**
     * @see Button#getDefaultFormProcessing()
     *
     * @return {@code true} for default processing
     */
    public boolean getDefaultProcessing() {
        return defaultProcessing;
    }

    /**
     * Finds form that will be submitted
     *
     * @return form to submit or {@code null} if none found
     */
    protected Form<?> findForm() {
        // try to find form in the hierarchy of owning component
        Component component = getComponent();
        if (component instanceof Form<?>) {
            return (Form<?>) component;
        } else {
            return component.findParent(Form.class);
        }
    }

    /**
     * Listener method that is invoked after the form has been submitted and
     * processed without errors
     *
     * @param target
     */
    protected abstract void onSubmit(AjaxRequestTarget target);

    /**
     * Listener method invoked when the form has been processed and errors
     * occurred
     *
     * @param target
     */
    protected abstract void onError(AjaxRequestTarget target);

}

然后你必须使用它

public class HomePage extends WebPage {
    private static final long serialVersionUID = 1L;

    private Integer counter = 0;

    public HomePage(final PageParameters parameters) {
        final Label label = new Label("counter", new PropertyModel<Integer>(this, "counter"));
        label.setOutputMarkupId(true);
        add(label);
        Form form = new Form("form");
        form.add(new AjaxTimerFormSubmitBehavior(form, Duration.seconds(10)) {

            @Override
            protected void onSubmit(AjaxRequestTarget target) {
                counter++;
                target.add(label);
            }

            @Override
            protected void onError(AjaxRequestTarget target) {
                // TODO Auto-generated method stub

            }
        });
        add(form);
    }

    public Integer getCounter() {
        return counter;
    }

    public void setCounter(Integer counter) {
        this.counter = counter;
    }
}

我希望这会给你一个想法......

是一个小型演示战争文件。只需下载,在您最喜欢的应用程序容器中折腾,然后观察它的作用。它也包含来源。

于 2012-06-26T13:46:29.107 回答
1

要设置间隔以定期运行一些代码,您可以使用 JQuery 执行此操作:

$(document).ready(function(){
    //ajax code here
    myVar = setInterval(someCode, 10000);
});

那是你追求的吗?

编辑

刚刚意识到... set Interval 实际上并不是一个 JQuery 函数。

//use this to stop it    
clearInterval(myVar);
于 2012-06-26T11:35:37.830 回答
0

我认为您做了正确的事情,我认为您将行为附加到不会改变自身的组件上。您将需要在onPostProcessTarget方法中编写更新逻辑并将要刷新的组件添加到 ajaxRequestTarget。Firebug查看或Chrome查看该行为是否触发了对服务器的调用的网络选项卡。

如果不是这样,可能是脚本的前提条件失败(标记id更改可以做到这一点)

于 2012-06-26T15:11:25.040 回答