如何在 Class 的 Timer 方法中看到 HTML 标记组件

How can I visible HTML markup component in Timer method in Class

在计时器事件上,我想再次使组件可见。有什么不同的方法来完成这个任务吗?我附上了 snipet 亲爱的。

HTML 标记:在此文件中,我创建了警告栏

  <div Class="row" wicket:id="alert_app" >
      <div  class="alert alert-info" role="alert" style="top: 2%; left: 50%; position: absolute;">
      <a href="#" wicket:id="alert" style="color:red"  >Alert - Match Found</a>

     </div>
    </div>    

Java Class:我已经实例化了 WebMarkupContainer 并在初始阶段不可见,在 5 秒警告栏出现后(这是我的计划)我使用 Timer 并且卡住了在定时器事件中。

 WebMarkupContainer informationBox = new WebMarkupContainer("alert_app");
     add(informationBox);

        final AjaxLink saveProfile = new AjaxLink("alert") {

        private static final long serialVersionUID = 1L;

        public void onClick(AjaxRequestTarget target) {

            this.setResponsePage(ABC.class);
        }

    };

    informationBox.add(saveProfile);
    informationBox.setVisible(false);  

    Timer timer = new Timer();

    timer.schedule(new TimerTask() {

        @Override
        public void run() {

            informationBox.setVisibilityAllowed(true);

            informationBox.setVisible(true);   // I got error in this line

        }
    }, 5000);

错误:

   12:37:04,735 INFO  
  [org.apache.wicket.response.filter.AjaxServerAndClientTimeFilter] (http-
   localhost-127.0.0.1-8080-4) 1ms server time taken for request 
wicket/bookmarkable/xyz.abc.vbn.class?
4&username=user+name response size: 9386
12:37:09,736 ERROR [stderr] (Timer-3) Exception in thread "Timer-3" 
org.apache.wicket.WicketRuntimeException: No RequestCycle is currently set!

12:37:09,736 ERROR [stderr] (Timer-3)   at 
org.apache.wicket.Component.getRequest(Component.java:1791)

12:37:09,736 ERROR [stderr] (Timer-3)   at 
org.apache.wicket.markup.html.WebPage.dirty(WebPage.java:334)

12:37:09,736 ERROR [stderr] (Timer-3)   at 
org.apache.wicket.Page.dirty(Page.java:248)

12:37:09,736 ERROR [stderr] (Timer-3)   at 
org.apache.wicket.Page.componentStateChanging(Page.java:937)

12:37:09,736 ERROR [stderr] (Timer-3)   at 
org.apache.wicket.Component.addStateChange(Component.java:3512)

12:37:09,736 ERROR [stderr] (Timer-3)   at 
org.apache.wicket.Component.setVisible(Component.java:3195)


 12:37:09,736 ERROR [stderr] (Timer-3)  at 
 java.util.TimerThread.mainLoop(Timer.java:555)

 12:37:09,736 ERROR [stderr] (Timer-3)  at 
 java.util.TimerThread.run(Timer.java:505)

我不确定,但我没有在代码中看到

informationBox.setOutputMarkupId(true);
informationBox.setOutputMarkupPlaceholderTag(true)

您不能只使用 java.util.Timer。它启动一个不是 HTTP 工作线程的新线程,因此 WicketFilter 没有机会设置 ThreadLocal 的(Application、Session 和 RequestCysle)。

您必须改用 org.apache.wicket.ajax.AbstractAjaxTimerBehavior。 它将在给定的持续时间后触发一个新的 Ajax 请求,一切都会按预期进行!

我把我的回答放在这里是为了对观众有帮助,谢谢@Alexei 和@martin,你们两个的回答很有帮助。

 WebMarkupContainer informationBox = new WebMarkupContainer("alert_app");
 informationBox.add(new AjaxLink("alert1"){

        @Override
        public void onClick(AjaxRequestTarget target) {
            // TODO Auto-generated method stub
            this.setResponsePage(ABC.class);
        } });

    add(informationBox);
    informationBox.setOutputMarkupId(true);
    informationBox.setOutputMarkupPlaceholderTag(true);


   informationBox.setVisible(false);

   this.add(new AbstractAjaxTimerBehavior(Duration.seconds(5))
    {
        private static final long serialVersionUID = 1L;

        @Override
        protected void onTimer(AjaxRequestTarget target)
        {
            target.add(informationBox.setVisible(true));
            System.out.println("on timer");

            stop(target);
        }
    });