Adrian
Adrian

Reputation: 6101

Apache Wicket: Display an error message when StalePageException occurs

I have a Java application using the Apache Wicket 6.x; sometimes exception StalePageException is triggered as described below and shows up in the log. However, because there is no feedback to the user, the application behaves unexpectedly causing frustration.

How can I capture the StalePageException and present a message to the user (preferably using a feedback panel) explaining the situation?

Open a stateful page in a tab/window, then copy the url from the address bar and open it in a new tab/window. Then go back to the first tab/window and try to click on any stateful link. This will lead to StalePageException.

It is thrown because Wicket detects that the same page instance has been rendered between the render of the current page and the click on the link. Wicket does this because it is not sure whether there are any changes in the page tree hierarchy between the initial rendering and the click event. It is even possible that this link is no more existing in the last version of the page, so this click could lead to ComponentNotFoundException if StalePageException is not thrown earlier.

StalePageException just leads to render of the current page. So the user will see the last version of the page and (s)he will need to click the link again.

Upvotes: 3

Views: 1045

Answers (2)

Akash Mishra
Akash Mishra

Reputation: 712

Would you wanna try a rather simpler approach by capturing the exception instance (lets say e). Then check for the type of this excception. And eventually use one of the inherited methods from java.lang.Throwable to capture the details

if(e instanceof StalePageException ){
       e.printStackTrace(); //can use other methods as per desire : getCause, getLocalizedMessage, getMessage, getStackTrace, initCause, printStackTrace
}

Please verify if getting the exception instance is possible for you to highlight in your code.

Upvotes: 0

martin-g
martin-g

Reputation: 17503

You need to use custom IRequestCycleListener (extend from AbstractRequestCycleListener). Override #onException() and if the passed exception is StalePageException then you may use Session.get().error("...") to report that to the user. Just make sure you have a FeedbackPanel in the current page.

Upvotes: 2

Related Questions