Our Blog

Let us find tech solutions together

Jul 20

When writing AJAX-specific code for Wicket, in order to make any updates to a component, it needs to be added to the AjaxTarget. If you’ve got a particularly large form, this can get tedious, so use an IVisitor instead!

1
2
3
4
5
6
7
8
9
10
11
@Override
protected void onSubmit(final AjaxRequestTarget target, Form form) {
    form.visitFormComponents(new FormComponent.IVisitor() {
        public Object formComponent(IFormVisitorParticipant
                formComponent) {
            final FormComponent fc = (FormComponent)formComponent;
            target.addComponent(fc);
            return Component.IVisitor.CONTINUE_TRAVERSAL;
        }
    });
    ...

And as always, for each component you access via AJAX, you’ll need to:

1
   component.setOutputMarkupId(true);
Read more
Jul 20

When developing with Apache Wicket, there are times when you won’t be able to use wicket-spring to access your bean implementations. Here is a simple example that you can add to your Wicket Application class to make accessing the context easier

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    protected void init() {
        ...
        ServletContext servletContext = super.getServletContext();
        applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        ...
    }

    private ApplicationContext applicationContext;


    public Object getBean(String name) {
        if (name == null) return null;

        return applicationContext.getBean(name);
    }
Read more
Jul 20

A quick howto (via wicket wiki) on adding Javascript or CSS to your pages, and having them compressed during the “deployment” cycle automatically by Wicket. So to start with, we need to copy or Javascript or CSS file somewhere in our package hierarchy that we can reference in our Page. For simplicity, we can copy it right next to the HTML file like so:

1
2
3
4
MyPage.java
MyPage.html
MyPage.js
MyPage.css

Then in our Page, we need to reference these items based on the path of our Java file, like so:

1
2
3
private static final CompressedResourceReference MYPAGE_JS = new CompressedResourceReference(MyPage.class, "MyPage.js");

private static final CompressedResourceReference MYPAGE_CSS = new CompressedResourceReference(MyPage.class, "MyPage.css");

This code gives us a ResourceReference that we can add to our page, most use cases to the HTML head element block. To do that in your page:

1
2
3
add(HeaderContributor.forJavaScript( MYPAGE_JS ));

add(HeaderContributor.forCss( MYPAGE_CSS ));

In Wicket 1.4 HeaderContributor.forJavaScript() and HeaderContributor.forCss() are deprecated, you can use the code below:

1
2
3
add(JavascriptPackageResource.getHeaderContribution(MYPAGE_JS));

add(CSSPackageResource.getHeaderContribution(MYPAGE_CSS));
Read more