Wicket and ajax rich website: The easiest way to do it?
I want to use Wicket to create an application, but I have some designers who would like to write / maintain javascript and they basically expect 1 JS segment per page and a global JS file. I think the most natural way to add javascript to gates is to add it on one component (not on the page), which would create problems for these designers (fractionated javascript and the need to write in java files). Is there a better way to solve this problem? (of course I expect to work after a partial update.)
And the second (related) thing they would like (and I would really like) is the ability to request JSON information via a static link, is this possible in Wicket?
a source to share
Wicket's built-in AJAX support is always stateful and thus reachable with URL changes. If your designers are not planning on using the Wicket JS library, it is quite easy to set up 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 can also implement your own AbstractRequestTargetUrlCodingStrategy
to directly return IRequestTarget
from IRequestTarget decode(RequestParameters params)
and mount it in your application.
As far as JS files are concerned, I would try to train them to use one file for each component. This certainly has the advantage of less copy and paste code. Also, I would definitely give up JS in Java code. This is usually needed to pass data or configuration to JS, either as variable definitions or in method calls. Since this data is usually written in Java and JS by designers, it's time for developers and programmers to merge.
a source to share
I started with JSON, making my gate pages return JSON, but quickly realized there were better tools for the job, especially if you have a full web services layer. If you just need some JSON here and there, always via GET, then by all means, just make a Wicket page.
I ended up using Jersey with Jackson along with Wicket. Jersey makes it easy to set up URLs that can do different things with different http methods (GET / POST / PUT / DELETE), and it is also easy to parse query strings, etc. I would consider going this route for heavier JSON needs.
You can easily run both Wicket and Jersey in the same web application with little web.xml configuration.
a source to share
The quick answer to your second question is yes it is possible. Use bookmarks for to access a resource that returns JSON data.
a source to share
You can easily use the following code to communicate dynamically with Wicket:
AbstractDefaultAjaxBehavior callme = new AbstractDefaultAjaxBehavior(){
@Override
protected void respond(AjaxRequestTarget target) {
}
};
page.add(callme);
//From any ajaxrequesttarget you can simply append the following code:
target.appendJavascript("wicketAjaxGet('"+callme.getCallbackUrl()+");");
This way you can have ajaxlink etc. that will pass the ajaxrequest to the Wicket side. If you want to pass data (although a static link doesn't look like this), follow these steps:
"wicketAjaxGet('"+callme.getCallbackUrl()+"&x='+value_to_pass_back''";
//to Read the value in the respond:
String x = RequestCycle.get().getRequest().getParameter("x");
So the callback url is dynamically generated (since the callback url is session specific), but it is rendered like any other url ...
For me this is 10x easier than creating a JSON system on top of a gate instead of using the built in one ... I use this all the time and it works great for me in my opinion. If your solution is different / better, I would like to know why it is possible.
a source to share