Apache wicket

from Wikipedia, the free encyclopedia
Apache wicket

Apache Wicket logo
Basic data

developer Apache Software Foundation
Publishing year June 21, 2010
Current  version 8.5.0
( May 28, 2019 )
Current preliminary version 9.0.0-M5
(April 6, 2020)
operating system cross-platform
programming language Java
category Web framework
License Apache license
wicket.apache.org

Apache Wicket is a component-based web framework for the Java programming language . It is an open source project and is available today under version 2.0 of the Apache license .

target

The aim of Wicket is to provide a comfortable, powerful and flexible way to create web applications for experienced Java programmers, which should make it possible to meet complex requirements and achieve high productivity. The component-oriented approach of Wicket makes it very easy to develop your own reusable components.

In contrast to many other frameworks, Wicket forces the programmer to program consistently object-oriented. Great emphasis is placed on reusability.

Functionality

Features of wicket are:

  • Separation of logic (Java) and presentation ( HTML and CSS ).
  • Each component can save its data in an associated data model.
  • Automatic status management.
  • With the exception of web.xml, no XML configuration files are required. Only knowledge of Java and (X) HTML is required to work with Wicket.
  • The user input is taken over with Java. Java first checks whether an entry is required, then the data is converted into the corresponding data type. Finally, the data is checked for admissibility (validation). These three steps can be intercepted as components or together for a web form using an error handling system. If all steps are completed without errors, the data is stored in the data model.
  • Ajax is possible without JavaScript knowledge. There are individual classes that provide Ajax functionality for this purpose.
    • Ajax functionality can be added to all components (also retrospectively) using so-called behaviors. Among other things, there is the class AjaxEventBehaviorfor reacting to a specific JavaScript event or the class AbstractSelfUpdatingTimerBehaviorfor automatically updating individual components at specific time intervals.
  • With so-called panels you can change or exchange parts of a page or build the page from different components.
  • It is very easy to develop reusable components and, if necessary, to enrich them with Ajax functionality.
  • All files required for a component (Java and HTML files plus, if applicable, icon, CSS or JavaScript files) can be packed into a jar file for better reusability .
  • So-called resources make it possible to integrate dynamically generated data (e.g. PDF data) or to access static data (JavaScript / CSS files).
  • The structure of a URL can be URLCodingStrategiesdetermined via .
  • External JavaScript libraries can be easily integrated
  • Gradual migration of HTML pages to Wicket is possible.
  • As of version 1.5, all components can easily exchange events with one another.
  • WicketTester enables the unit test of individual pages and also the process over several pages.

Conceptually, Wicket is most comparable to Apache Tapestry , but primarily competes with JavaServer Faces and Google Web Toolkit .

history

The architecture of Wicket was designed by Jonathan Locke and Miko Matsumura in 2004 and was available on sourceforge.org up to version 1.2 . The framework later became available as an open source project under the Apache license . A group of programmers from the Dutch company Topicus, led by Eelco Hillenius, Martijn Dashorst and Johan Compagner, along with a few others, still make up the core team. Wicket is available as an open source project under the Apache license, version 2.0.

From version 6.0.0, Wicket requires JDK 6 or higher. In addition, Wicket now uses jQuery in the background as the Ajax implementation.

With Wicket 7.x Java 7 and Servlet (version 3.x) containers are required.

Since Wicket 8.x is full support for Java 8 .

Life cycle of a wicket component

Each wicket component has a specific life cycle. There are three main phases:

  • initialization
  • Rendering
  • Removing

The individual methods can be overwritten if necessary.

phase Methods involved description
initialization onInitialize Runs at the beginning of the life cycle. This method is suitable as a "special constructor" for the initialization of all components, especially for classes that inherit from WebPage.

When overwriting, the super.onInitialize () method must be called first .

 protected void onInitialize() {
     super.onInitialize();
     .....
 }
Rendering onConfigure This method is called before rendering starts. In this respect, this is the ideal place to control the status of components such as B. Visibility or Activation. Since the methods are called multiple times isVisibleor isEnabledduring the rendering of a page or component, it is strongly recommended not to use these methods directly, but instead onConfigureto use the method to control the status. If the visibility of a component is on false, the following method (onBeforeRender) is no longer called at all.
onBeforeRender This method is called before rendering begins. In this respect, it is the last chance to change the component hierarchy or to add or delete components. If this method is overwritten, the call must be made at the super.onBeforeRender() very end , as this will trigger the rendering of all subsequent components.

 protected void onBeforeRender() {
     .....
     super.onBeforeRender();
 }
onRender
onComponentTag here you can make any changes to the element (day). Both the type of component and its style can be changed. Here, too, the call to the method of the super class must not be forgotten.

 protected void onComponentTag(ComponentTag tag) {
     super.onComponentTag(tag);
     .....
 }
onComponentTagBody You can override this method if you want to change the content of a component. In combination with the replaceComponentTagBody method, changed content can be rendered. If you want to have the original behavior of the components, you have to call the method of the super class.
 public void onComponentTagBody(MarkupStream markupStream, ComponentTag tag) {
     if(!isEnabled()){
         replaceComponentTagBody(markupStream, tag, "(diese Komponente ist deaktiviert)");
     }
     else{
         super.onComponentTagBody(markupStream, tag);
     }
 }
onAfterRenderChildren
onAfterRender
Removing onRemove The method can be used to release certain resources that the component needs.

example

A simple example consists of four files:

  • an Application class to be called first
  • a Java file that represents the page
  • an HTML file belonging to the Java file
  • and the web.xml file , the deployment descriptor of the web application for configuring which class is the applications class.

Application class

The application class serves as the central entry point for the web application. Various settings can be made here that are valid for the entire application. Among other things, the start page is set here:

package com.myapp.wicket;

import org.apache.wicket.protocol.http.WebApplication;

public class Application extends WebApplication {
    public Class getHomePage() {
        return OnePage.class;
    }
}

Web page (Java)

Each page is represented by a Java class and an associated HTML file of the same name. All components of the class are defined here. All components that have a wicket: id in the HTML file are added as components.

OnePage.java:

package com.myapp.wicket;

import org.apache.wicket.ajax.AjaxEventBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.Model;

public class OnePage extends WebPage {

    public OnePage() {
        final Label label = new Label("message", new Model(""));
        label.setOutputMarkupId(true);
        Form form = new Form("form");
        final TextField field = new TextField("field", new Model(""));
        field.setOutputMarkupId(true);
        form.add(field);
        final Button but1 = new Button("button1") {

            @Override
            public void onSubmit() {
                String value = (String) field.getModelObject();
                label.setModelObject("Normal-Request: " + value);
                field.setModelObject("");
                // setResponsePage(AnotherPage.class);
            }
        };
        but1.setOutputMarkupId(true);
        form.add(but1);
        final Button but2 = new Button("button2", new Model("Button deaktivieren ..."));
        but2.add(new AjaxEventBehavior("onclick") {

            @Override
            protected void onEvent(AjaxRequestTarget target) {
                but1.setEnabled(!but1.isEnabled());
                if (but1.isEnabled()) {
                   but2.setModelObject("Button deaktivieren ...");
                } else {
                   but2.setModelObject("Button aktivieren ...");
                }
                target.addComponent(but2);
                target.addComponent(but1);
            }
        });
        form.add(but2);
        add(form);
        add(label);
    }
}

Web page (HTML)

A wicket: id attribute must be created for each component that is to be changed dynamically .

OnePage.html:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns:wicket>
    <head>
        <title>Echo Application</title>
    </head>
    <body>
        <h1>Echo example</h1>
        <form wicket:id="form">
            <input wicket:id="field" type="text" />
            <input wicket:id="button1" type="submit" value="Button" />
            <input wicket:id="button2" type="button" value="Button deaktivieren ..." />
        </form>
        <p wicket:id="message">Fun Fun Fun</p>
    </body>
</html>

web.xml

The application is only integrated web.xmlinto the servlet container as a web application via the following file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <filter>
        <filter-name>WicketApplication</filter-name>
        <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
        <init-param>
            <param-name>applicationClassName</param-name>
            <param-value>com.myapp.wicket.Application</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>WicketApplication</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

criticism

Wicket is very efficient when it comes to meeting complex web application requirements. Business logic can be programmed entirely in Java without having to worry about the specifics of a web application. However, the status of the processing within a session is managed by Wicket on the server side, which means that the developer of a wicket application must specifically solve the resource problem of many simultaneous users. In contrast, the status of JavaScript-based web frameworks is often kept on the client side and the data is only reloaded from the server if necessary.

Since JavaScript or CSS frameworks can be integrated very well in Wicket , Wicket is a flexible web development platform.

Versions

Wicket has seen a great deal of development over time.

  • from Wicket 1.4 there is support for generic data types. The last version of the 4-series is here Wicket 1.4.23 .
  • with Wicket 1.5 the API was radically changed. The last version here is Wicket 1.5.10 .
  • With Wicket 6.x , Wicket's own Ajax support was replaced by jQuery . The last version is Wicket 1.6.20 .
  • with Wicket 7.x , Wicket requires Java 7 and Servlet (version 3.x) containers.
  • Since Wicket 8.0 is Java 8 fully supported. Servlet 3.1 is also required.
  • The upcoming Wicket 9.0 is based on Java 11 and uses JUnit5 . The internal class Durationis replaced by one java.time.Durationfrom the JDK .

literature

  • Martijn Dashorst, Eelco Hillenius: Wicket in Action . Manning, Greenwich CT 2008, ISBN 978-1-932394-98-6
  • Michael Mosmann: Practice Book Wicket . Hanser, 2009, ISBN 978-3-446-41909-4
  • Roland Förther, Carl-Eric Menzel, Olaf Siefart: Wicket . Component-based web applications in Java. dpunkt.verlag, 2009, ISBN 978-3-89864-569-0 , p. 250 ( dpunkt.de [accessed on January 4, 2010]).
  • Igor Vaynberg: Apache Wicket Cookbook . Packt Publishing, 2011, ISBN 978-1-84951-160-5 , pp. 312 (English).

Web links

Individual evidence

  1. a b projects.apache.org . (accessed on April 8, 2020).
  2. wicket.apache.org .
  3. [1] . wicket.apache.org. Viewed on May 11, 2020.