Wicket + Javascript

I'm wrapping up a Javascript widget in a Wicket component. I want to let the JS side talk to the component. What I've got so far:

Component in question goes like

talker = new GridAjaxBehavior();

in constructor

and then, later on, puts something like

"var MyGridTalker = new talker(" + this.talker.getCallbackUrl() + ");";

into the JS.

where GridAjaxBehavior extends AbstractDefaultAjaxBehavior. I want GridAjaxBehavior to spit back some XML when the JS calls it.

Am I doing this the right way? What should GridAjaxBehaviour do to spit back the XML?


Asked by: Sarah140 | Posted: 23-01-2022

Answer 1

Spit back some XML for what? Presumably to update the model or the view, yes?

The strength of Wicket is that you don't have to worry about the rendered HTML. In Model-View-Controller terms, you set up the Controller to correctly modify the Model, and Wicket takes care of the View.

The separation is not entirely clear: in fact you can show/hide view components, or change then, and that can be seen as altering the View.

But what you generally don't have to do is directly manage the browser or javascript. Wicket takes care of that, if you take care of making your changes in the Java code.

In Wicket, the Ajax will call a method on your AjaxBehavior with an AjaxRequestTarget target.

In that method (or in methods called from it), you do whatever you need to do, updating models or views, and then you add to the target any view component that that has changed. Wicket takes care of updating the browser.

Here's an example. It's taken from some code I did, but heavily altered just to make explication clearer. The idea is simple: "chained" dropdown choices, where the options in the child change when the select option in the parent changes, as in the series of [State] [County] [District].

(In the actual class, the Model change is passed to the child, which decides for itself if it has changed, and adds itself to the target if it has, then passes the target to its child. I've removed most of that to make a clearer example.)

Here's the ctor, which just adds to itself an anonymous subclass of an AjaxBehavior:

public AjaxChildNotifyingDropDownChoice(...code elided for clarity...) {
    this.child = child;

    // Ajax won't work without this:
    add( new OnChangeAjaxBehavior() {
        public void onUpdate(final AjaxRequestTarget target) {

            // tell child to update its list
            // based on newly selected value

            // when the Ajax is called, 
            // my owning component's model
            // is already updated

            // note we could just type getModel()
            // I'm making explicit that we're calling it
            // on the enclosing class 
            // (which a non-static inner class has a hidden ref to) 

            // now add the child to the target
            // Wicket javascript will receive the new 
            // options and re-render the child dropdown


We could also have hidden or un-hidden components, or added behaviors like CSS styles, or even swapped one Panel for another. As long as for each changed component we: 1) called setOutputMarkupId(true); so that the javascript can find it, and 2) added it to the AjaxRequestTarget

Note that different types (subclases) of Ajax Behavior have different callback functions, so be sure you're overriding the right one (add an @Override annotation so the compiler can complain if you got the name wrong).

But again, the basic wicket idea is that instead of sending raw data for the client to parse and act on, you update your model and view, and tell Wicket to re-render what you've changed, by adding the chnaged components to the target.

The only reason I can think of to send straight XML would to be to feed it to non-Wicket javascript. Let me know if that's your aim, and I completely missed the point. ;)

Answered by: Lily559 | Posted: 24-02-2022

Answer 2

I don't really know what Wicket is or what it does, but there is a minor bug in your code (as it appears).


"var MyGridTalker = new talker(" + this.talker.getCallbackUrl();

You seem to be missing your end parens:

"var MyGridTalker = new talker(" + this.talker.getCallbackUrl() + ")";

Anyway, not a big deal, but didn't know if it was intentional.

Answered by: Anna454 | Posted: 24-02-2022

Similar questions

java - How to use the model in wicket?

I try to create an page of the following structure: MainPage { DateSelector {} TabGroup { Tab0 {...} Tab1 {...} Tab2 {...} } } DateSelector is a component (extending Panel) that enables the user to select a timespan (startDate, endDate). TabGroup extending "AjaxTabbedPanel". The tabs shown in the TabGroup should use the selected timespan to display conte...

java - How to make a wicket link appear as an image?

I am trying to make a wicket link appear as an image instead of text. I am creating an external link and then placing it on the page. add(new ExternalLink("link", url, "Page name")); <a wicket:id="link"></a> My problem is that I can't put the image in place of "Page name" because it will just set the html as text. I also tried to span a wicket id instead and it also is render...

java - Wicket - which ORM?

I'm looking for an easy-to-use and intergrate ORM for Wicket application. I was thinking about Hibernate. Is it a good choice? What are the possible problems with Wicket/Hibernate? Any other suggestions are welcome. Thanks for any help.

java - Wicket vs Vaadin

I am torn between Wicket and Vaadin. I am starting a micro-isv and need to make a choice of web framework. I have narrowed down my choices to Wicket and Vaadin. I have used both frameworks and I love them both. however I need to make a choice. If If I choose Vaadin: I wont have to worry much about the look and feel. It comes with nice themes. I will do all my programming in Java which am ver...

Can we call html ids from Wicket Java class

Is there a way to call ids that are created in html page without specifically giving wicket:id in the component. If so, how can I do that? What I am trying to accomplish is to get access of the component to assign a default value from CMS. I could assign the default value using JS, however I need that value to be populated to Java so that I could use that in my class to update my business logic depending on the def...

java - Wicket 1.5 call wicket from javascript

I'm developing a project under Wicket 1.5 which uses jQuery. I've been using Wicket 1.4 before, and I'm used to call Wicket Java code from JavaScript with function callWicket() { var wcall = wicketAjaxGet('$url$' + '$args$', function() { }, function() { }); } As seen in C...

java - wicket 1.5 new urls

After migration from wicket 1.4 to wicket 1.5 I can see a new behaviour concerning web URLs. Every pages has ?[0-9]+ added to it for example http://mywickethomepage.com/?0 , http://mywickethomepage.com/?3 . My question is: what actually it is? Googling just gave me a hint that's an id for statefu...

java - Wicket on Jetty: Cannot save page with id

I deployed my Apache Wicket App on a Jetty Server and everytime i open a Wicket-Page, i can see the following error on my jetty log: WARN - DiskDataStore - Cannot save page with id '2' because the data file cannot be opened. ERROR - DiskDataStore - /tmp/jetty- /wicket.Tourneys-filestore/2gs9iqj4zdjtkerejipyu0co/data (No such file or directory...

java - When is necessary to use LDM in wicket?

I've been reading about tuning wicket session size and I've found that is recommended to use LoadableDetachableModel to dont make the models live forever. Are Property models(Property and PropertyCompoundModel) so expensive? When to use LDM or Property Models?

java - Wicket - Back button after form submit

I have a new problem with a Form and the BACK button. Take this kind of FORM: public SearchSomethingForm() { super(ORSAWebConstants.FORM); setModel(new CompoundPropertyModel<Void>(this)); add(section01Panel); add(section02Panel); add(section03Panel); add(new SubmitButton()); } As you see, I don't have a property model base on a single Object. Inside the form I onl...

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)