Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Enhance Wicket With Spring Boot

DZone's Guide to

Enhance Wicket With Spring Boot

Wicket is a good framework, but its configuration code is verbose. Fortunately, you can integrate it with Spring Boot to take advantage of its autoconfigure capabilities.

· Java Zone
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

Wicket has traditionally provided a solid integration with the core part of Spring (i.e. the container) with its dedicated module wicket-spring. Recently, a new independent project called wicket-spring-boot has extended Spring integration to Spring Boot, too. With it, we can use Spring Boot as application starter and take advantage of its autoconfigure capabilities to set up the entire application.

To run a Wicket application with Spring Boot, we must first include the following dependency (for Maven) in our project:

<dependency>
  <groupId>com.giffing.wicket.spring.boot.starter</groupId>
  <artifactId>wicket-spring-boot-starter</artifactId>
</dependency>


Now, to run a Wicket application, we can implement a starter class like we used to do with Spring Boot:

@SpringBootApplication
public class BootApp {

    public static void main(String[] args) throws Exception {

        new SpringApplicationBuilder()
          .sources(BootApp.class)
          .run(args);
    }
}


In addition to this class, we should also specify the home page of our application by applying the annotation @WicketHomePage (the page class must be in the same application package or in a subpackage):

@WicketHomePage
public class HomePage extends WebPage {
    //...page code
}


If we run the BootApp class, we will see our home page at http://localhost:8080. As we can see, there's no trace of the Wicket application class where we override the init method to set up our web application. For this purpose, we can use the extension class WicketApplicationInitConfiguration:

@ApplicationInitExtension
public class YouExtensionConfig implements WicketApplicationInitConfiguration {

  @Override
  public void init(WebApplication webApplication) {

      //...config stuff
  }
}


In this class, we can also override the getHomePage() method if we don't want to use @WicketHomePage. If this class is not enough to configure our application, we can use WicketBootStandardWebApplication or WicketBootSecuredWebApplication (this last one is for secured applications).

To distribute our application as an executable JAR, we can simply use the Spring Boot Maven plugin:

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>


Simplify Configuration With Spring Boot

During its lifetime, Wicket has seen a growing number of satellite projects aimed at extending the core framework. Most of them are hosted at WicketStuff. The configuration code for Wicket and its extensions tends to be very verbose and hard to read and mantain. With Spring Boot, we can get rid of all this code and use a simple YAML file to configure our application.

By default, Wicket Spring Boot Starter comes with a default YML configuration file for a 'development' Spring profile that can be overriden with a custom application-development.yml (under  'src/main/resources' ). Here is an example of a generic YML configuration file:

wicket.core.settings.general.configuration-type=development # development/deployment(default)
wicket.web.servlet.filter-mapping-param=/*


#Markup - Settings
wicket.core.settings.markup.default-markup-encoding=UTF-8
wicket.core.settings.markup.automatic-linking=false
wicket.core.settings.markup.compress-whitespace=false
wicket.core.settings.markup.strip-comments=false

#CSRF prevention
wicket.core.csrf.enabled=true
wicket.core.csrf.no-origin-action=allow
wicket.core.csrf.conflicting-origin-action=abort
wicket.core.csrf.error-code=400
wicket.core.csrf.error-message=Origin does not correspond to request

#datastore settings
wicket.core.datastore.httpsession.enabled=false
wicket.core.datastore.httpsession.pagesNumber=20 


Beside core parameters wicket-spring-boot also supports configuration for a number of WicketStuff modules:

#configure REST resources with restannotations
wicket.stuff.restannotations.enabled=true
wicket.stuff.restannotations.packagename= # the package name to scan for project specific annotations

#use fast 2 as serialization library
wicket.stuff.serializer.fast2.enabled=true

#use html compressor
wicket.stuff.htmlcompressor.enabled=true
wicket.stuff.htmlcompressor.features.*=


As you can see, with wicket-spring-boot, the integration between Wicket and Spring Boot is really straightforward! For more configuration options, you can see the project homepage on GitHub.

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

Topics:
java ,apache wicket ,spring boot ,tutorial ,java ee

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}