Wicket is a very server-side focused framework, so working with ajax requests is quite different from what you might be used. You generally don’t write your own client-side Javascript, but rather it is generated by the different Wicket behaviors that you create and add to form components.
Not to worry though, once you get used to it, you can be very productive! Possibly much more productive than writing a lot of your own client-side code.
A simple example scenario
Let’s use the simple example of a label that displays a user counter that needs to be asynchronously refreshed in response to events. In this case, a button click. Let’s look at one way you might do it using jQuery (when it was a thing) 馃檪
The jQuery way
You might write this kind of code to solve this:
<script type="javascript">
$(document).ready(function() {
$("#refresh-btn").click(function(){
$.ajax({url: update_users_count_url, success: function(result){
$("#usersCountLbl").html(result);
}});
});
});
</script>
<a href="#" id="refresh-btn">Refresh Users</a>
Num users: <div id="usersCountLbl"></div>
The Wicket way
With Wicket, we will change this to have two parts. The html will remain almost the same, but the javascript will go away. Here is the slightly modified html, the only difference being “wicket:id”:
<a href="#" wicket:id="refreshBtn">Refresh Users</a>
Num users: <div wicket:id="usersCountLbl"></div>
And here is the associated server-side code that will create the components and the ajax behavior:
Label usersCountLbl = new Label("usersCountLbl", new LoadableDetachableModel<Integer>() {
@Override protected Integer load() { return userService.getUserCount(); }
});
usersCountLbl.setOutputMarkupId(true);
add(usersCountLbl);
AjaxLink<Void> refreshBtn = new AjaxLink<Void>("refreshBtn") {
@Override
public void onClick(AjaxRequestTarget target) {
target.add(usersCountLbl);
}
};
Line 4 is critical for making this work. It is telling Wicket to output an ID attribute when generating the HTML for usersCountLbl so that client-side javascript can use that ID when wiring up events.
Line 10 is telling Wicket to re-render the contents of usersCountLbl, send it back to the browser, and generate JS to update the html. Which would be the equivalent of the jQuery code above that replaces the html.
AjaxRequestTarget
Whenever you have any callback function that will be executed in response to an ajax request (onclick, onblur, onchange, etc), you will have access to an AjaxRequestTarget target.
By adding Wicket components to the target, you are saying you want them to be refreshed or re-rendered. Several things will happen:
- The model of the component will be re-evaluated, in this case, the user count. Note that if you don’t use a model for the label, this will not work.
- onConfigure() of the component will be executed. Here you can define visibility rules such as showing the label only if the user is an admin.
Any component that you add to the target must have been set using setOutputMarkupId(true). This is so the client-side jquery events can be wired up correctly.
Conclusion
That should be all you need to know to get started. Familiarize yourself with the classes in the org.apache.wicket.ajax package and have fun forgetting how to write Javascript!