Showing posts with label wicket. Show all posts
Showing posts with label wicket. Show all posts

Tuesday, December 20, 2011

Inform user about Ajax failed (no network) in Wicket

The problem

It is well known to the internet. If you have an Ajax application you have to somehow inform user in case Ajax fails. And in the modern world Ajax may fail very easily - WiFi switched off, bad smartphone connection et al.

Also we have a Wicket which does hide all the Ajax work behind the scenes - you drop components to the page, they do communicate via Ajax.

The (possible) solution
At least we employed the one for us.

1. Create the localized message key in the property file. We are using XML properties, so it'd look like Ajax failed! Reload please

2. Create a function in one of the javascript files you always load. You may do it inline, but the function in the file makes it easier to change. My guess it should survive absence of the Wicket too.

AlertError here is the generic error function showing jQueryUI custom dialog.


function tryRegisterWicketAjaxOnFailure(message) {
if (!Wicket || !Wicket.Ajax) {
return;
}
Wicket.Ajax.registerFailureHandler(function() {
alertError(message, 0, 'center', true);
});
}


3. As our tryRegister takes a message parameter we need to pass it from the Wicket. So, in your BasePage.java or whatever code just add


add(new HeaderContributor(new IHeaderContributor() {
public void renderHead(IHeaderResponse response) {
response.renderOnDomReadyJavascript(
String.format("tryRegisterWicketAjaxOnFailure('%s')",
getString("ajaxCommunicationFailedMessage")));
}
}));
4. Just enjoy it. You may put 'refresh' button, or a spinner at this dialog which would constantly check if the network is back. It's up to your creativity and demand.

Thursday, December 15, 2011

Wicket DownoadLink and AjaxDownload with non-ascii filenames

The problem - yet unsolved for me :(

You have an Apache Wicket application (in my case, this is wicket 1.4.x). You have a page which asks user to download some content and then lets download it. Content is dynamically generated (file?). It should have a name (default one) on client's machine. Name may have cyrillic (or Chinese) symbols.


Solutions
First of all, I was unable to solve this completely :(

Coming from StackOverflow post, the good way to do would be to form the URL ending with a filename (URL-encoded), and show no filename in Content-Disposition header. All other tricks and tips work not well under different browsers.

Wicket usual approach is to use either DownloadLink or AjaxDownload (thank you guys for such a good solution! Liked it much). First of them is using usual wicket URL coding strategy and second - pretty much the same, being a AjaxBehavior and thus IBehaviorListener. That far I could see no real way to change these URLs. The issue is that both of those don't allow for custom URLs, they are actually plugged into Wicket's infrastructure of page-component-iBehaviorListener-RequestCycle-RequestCodingStrategy chain, so we don't have direct control over it.

One solution proposed by Martin Grigorov on the wicket-users mailing list was to use
http://wicketinaction.com/2011/07/wicket-1-5-mounting-resources/ for mounting, but that did not work out for me as I needed to use containing page's model to generate the actual data.

Fail :(
Will up if any luck.

Wicket this time

another incarnation, switching to wicket-related posting as we're doing wicket jobs at the moment.

Hopefully experiences shared here would save someone's time -- that's a good enough reward.