每隔x秒如何显示表格的结果

时间:2012-06-26 11:29:43

标签: java wicket

我有一些从db搜索结果的表单。如何每隔x秒自动显示db的结果?这意味着每x秒提交一次按钮。我发现的所有关于提神的内容都是这个课程:

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

只是刷新页面不提交表单。然后我想从wicket示例页面启发:http://www.wicket-library.com/wicket-examples/ajax/clock?1但是当我点击源代码时,我只看到刚刚返回的url:内部错误

更新:

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

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

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

    });

但没有成功

3 个答案:

答案 0 :(得分: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;
    }
}

我希望能给你一个想法......

Here是一个小型演示war文件。只需下载,折腾您最喜欢的应用程序容器并观察其功能。它也包含来源。

答案 1 :(得分:1)

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

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

你说的是什么?

修改

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

//use this to stop it    
clearInterval(myVar);

答案 2 :(得分:0)

我认为你做了正确的事情,我认为你将行为附加到一个不会改变自己的组件。您需要在onPostProcessTarget方法中编写更新逻辑,并将要刷新的组件添加到ajaxRequestTarget。查看FirebugChrome的网络标签,查看该行为是否触发了对服务器的调用。

如果不是这样,可能是脚本的前提条件失败(标记ID更改可能会这样做)