7

I have an Ajax event that triggers the redraw of various Wicket panels. Some panels have additional calculations of their own inside their onBeforeRender() method. If there is an error, I want to display it to the user. But I can't add the FeedbackPanel to the AjaxRequestTarget because I don't have access to it anymore inside the panel that is being redrawn.

Is there a way to get the AjaxRequestTarget inside onBeforeRender() of a Wicket component? Is it even possible to add additional components to the Ajax target once the first ones are already in onBeforeRender() state?

Is there a better way to add the FeedbackPanel to the Ajax target if only the panels that need to be redrawn can decide if there is an error or not?

black666
  • 2,997
  • 7
  • 25
  • 40
  • 1
    I dont know what your app is doing but i doubt that business logic calculation on `onBeforeRender()` is a good idea. – Robert Niestroj Oct 08 '13 at 12:00
  • There was a static [`AjaxRequestTarget.get()`](http://wicket.apache.org/apidocs/1.4/org/apache/wicket/ajax/AjaxRequestTarget.html#get()) method back in Wicket 1.4 (seems to be gone in Wicket 6). However, as Robert already pointed out, validation logic and morevoer error reporting shouldn't be handled in `onBeforeRender()`. – Xavi López Oct 08 '13 at 12:24

1 Answers1

10

Getting the AjaxTarget is done like this:

AjaxRequestTarget target = requestCycle.find(AjaxRequestTarget.class);

Unfortunately, like expected, I can't add components to the target once I'm in the rendering phase. But Wicket provides the interface ITargetRespondListener with the method onTargetRespond(AjaxRequestTarget target). Here I can add anything I want to the target.

Xavi López
  • 27,550
  • 11
  • 97
  • 161
black666
  • 2,997
  • 7
  • 25
  • 40
  • 8
    For future readers. When even less context is available: `RequestCycle.get().find(AjaxRequestTarget.class)` – Rafael Winterhalter Jan 08 '14 at 14:11
  • 1
    How to get it in Wicket version 8? It is not the same way cause when I update the dependencies to version 8 it says something is wrong, but in documentation the definition of find method is the same – tomrlh Feb 22 '18 at 21:15