2017-07-19 63 views
0

On Timer事件我想使組件再次可見。有沒有什麼不同的方法來完成這項任務?我附上snipet belove。如何在類Timer中的可見HTML標記組件中使用

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類:我有實例化WebMarkupContainer,使無形的初始階段,出現的將5秒內提醒欄後(這是我的計劃)因爲我使用計時器,我卡在計時器事件。

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) 

回答

3

你不能只用java.util.Timer看到。它啓動一個不是HTTP工作線程的新線程,所以WicketFilter沒有機會設置ThreadLocal(Application,Session和RequestCysle)。

您必須改用org.apache.wicket.ajax.AbstractAjaxTimerBehavior。 它會在給定的持續時間後觸發一個新的Ajax請求,一切都會按預期工作!

0

我不知道,但我不代碼

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

我把我的答案放在這裏,所以它可以幫助觀衆有所幫助,謝謝@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); 
     } 
    });