AbstractAjaxTimerBehavior onTimer未在页面加载时调用,但如果我刷新页面则调用它。 我有一个扩展面板的类,我重写onConfigure()方法.Below是代码sniffet。
public ClockPanel(String id)
{
super(id);
hkplSettings = config.load(GeneralHkplSettings.class);
System.out.println(" ####### construcotor ##### "+hkplSettings.isDisplayCurrentDateTime());
final Label clockLabel = new Label("clockLabel", new ResourceModel("admin.hkpl.current.time"));
clock = new Clock("clock", propertyManager.getGeneralAdminSettings().getDisplayTimestampFormat());
add(clockLabel);
clock.setOutputMarkupId(true);
add(clock);
}
@Override
public void onConfigure()
{
super.onConfigure();
if (hkplSettings.isDisplayCurrentDateTime())
{
System.out.println(" ####### in ifffff ##### "+hkplSettings.isDisplayCurrentDateTime());
add(new AbstractAjaxTimerBehavior(Duration.seconds(1))
{
@Override
protected void onTimer(AjaxRequestTarget ajaxRequestTarget)
{
System.out.println(" ****** in timer ******* "+hkplSettings.isDisplayCurrentDateTime());
ajaxRequestTarget.add(clock);
}
});
setVisibilityAllowed(true);
}
else
{
System.out.println(" ####### in else ##### "+hkplSettings.isDisplayCurrentDateTime());
setVisibilityAllowed(false);
}
}
当我部署此应用程序然后按照SOP的打印
#construcotor ##### #infff #####当我刷新页面然后按照SOP的打印
#infff ##### 计时器*****中的*********,此行继续按预期打印。
答案 0 :(得分:1)
好奇;你是否有任何理由在onConfigure中添加AbstractAjaxTimerBehavior而不是在面板级别?每个请求都会调用onConfigure。
我尝试了以下似乎正在运作:
public class TestPanel extends Panel {
private String clock;
private Boolean flag = Boolean.FALSE;
public TestPanel(String id) {
super(id);
final DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
if (clock == null) {
Date today = Calendar.getInstance().getTime();
clock = df.format(today);
}
Form<Void> form = new Form<Void>("form");
form.setOutputMarkupId(true);
add(form);
final Label clockLabel = new Label("clockLbl", new PropertyModel<String>(this, "clock"));
clockLabel.setMarkupId("clockID");
clockLabel.setOutputMarkupId(true);
form.add(clockLabel);
add(new AbstractAjaxTimerBehavior(Duration.seconds(2)) {
@Override
protected void onTimer(AjaxRequestTarget ajaxRequestTarget) {
Date today = Calendar.getInstance().getTime();
clock = df.format(today);
System.out.println(" ****** in timer ******* " + clock);
clockLabel.setDefaultModelObject(clock);
ajaxRequestTarget.add(clockLabel);
}
});
}
public String getClock() {
return clock;
}
public void setClock(String clock) {
this.clock = clock;
}
@Override
protected void onConfigure() {
flag = Boolean.TRUE;
System.out.println(" Inside on Configure..." + clock);
if (flag) {
setVisibilityAllowed(true);
} else {
setVisibilityAllowed(false);
}
}
}
具有面板的页面显示计时器每两秒更新一次。 System.out.println也显示如下:
Inside on Configure...08/06/2015 09:53:47
****** in timer ******* 08/06/2015 09:53:50
****** in timer ******* 08/06/2015 09:53:52
****** in timer ******* 08/06/2015 09:53:54
****** in timer ******* 08/06/2015 09:53:56
****** in timer ******* 08/06/2015 09:53:58
****** in timer ******* 08/06/2015 09:54:00
****** in timer ******* 08/06/2015 09:54:02
****** in timer ******* 08/06/2015 09:54:04
****** in timer ******* 08/06/2015 09:54:06
****** in timer ******* 08/06/2015 09:54:08
答案 1 :(得分:0)
这适用于新项目:
public class HomePage extends WebPage {
private static final long serialVersionUID = 1L;
public HomePage(final PageParameters parameters) {
super(parameters);
}
@Override
protected void onConfigure() {
super.onConfigure();
add(new AbstractAjaxTimerBehavior(Duration.seconds(1)) {
private static final long serialVersionUID = 1L;
@Override
protected void onTimer(final AjaxRequestTarget target) {
System.out.println("timer");
}
});
}
}