您需要结合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;
}
}
我希望这会给你一个想法......
这是一个小型演示战争文件。只需下载,在您最喜欢的应用程序容器中折腾,然后观察它的作用。它也包含来源。