Search code examples
ajaxwicketrich-internet-application

Wicket and a rich ajax website: easiest way to do it?


I want to use Wicket to build an application, but I have some designers that would like to write/maintain the javascript, and they basically expect 1 JS-segment per page, and a global JS-file. I think the most natural way to add javascript in wicket is to add it per component (not per page), which would create problems for those designers (fractioned javascript, and having to write it in java-files). Is there a better way to solve this? (of course, I expect things to work after a partial refresh.)

And a second (related) thing they'd like (and I'd like actually) is the possibility to request information in JSON-format through a static link , is this possible in Wicket?


Solution

  • Wicket's built in AJAX support is always stateful and thus accessed with changing URLs. If your designers aren't planning to use Wicket's JS library, it's pretty straightforward to mount a JSON page:

    public class JsonReturningPage extends WebPage {
      public JsonReturningPage(PageParameters params) {
        String json = "{foo: 4711}";
        IRequestTarget t = new StringRequestTarget("application/json", "UTF-8", json);
        getRequestCycle().setRequestTarget(t);
      }
    }
    

    Alternatively, you could also implement your own AbstractRequestTargetUrlCodingStrategy to directly return an IRequestTarget from IRequestTarget decode(RequestParameters params) and mount it in your application.

    Regarding JS files, I'd try to educate them to use one file per component. This certainly has the advantage of less copy-paste code and simpler maitenance. Additionally, I'd certainly discourage JS in Java code. It's normally only needed to pass data or config to JS , either as variable definitions or method calls. As this data is typically in Java and JS is written by designers, it's time for designers and programmers to team up.