Implementing new modules

New modules have to implement the AppModule interface and follow the SPI requirements for packaging by adding a new file with the name packt.vaadin.datacentric.chapter02.api.AppModule into the META-INF/services directory. This file must contain the name of the fully qualified name of the AppModule implementation.

Let's say you want to develop a module that adds an option to the main menu that shows a notification when clicked. This can be easily implemented as follows:

package com.example;
...

public class ExampleModule implements AppModule {

@Override
public void register(ApplicationLayout layout) {
ApplicationLayout.MenuOption menuOption
= new ApplicationLayout.MenuOption("Example module");
layout.addMenuOption(menuOption, this::optionClicked);
}

private void optionClicked(
ApplicationLayout.MenuOption menuOption) {
Notification.show("It works!",
Notification.Type.TRAY_NOTIFICATION);
}
}

This class can be located in a separate Maven project and should include the api dependency.

The ExampleModule implementation is located in the Data-centric-Applications-with-Vaadin-8chapter-02example-module Maven project of the source code that accompanies this book.

To make the module discoverable by the webapp application, you must add a file with the name packt.vaadin.datacentric.chapter02.api.AppModule in the main/resources/META-INF/services directory of the new module. The file must contain the fully-qualified name of the AppModule implementation as follows:

packt.vaadin.datacentric.chapter02.example.module.ExampleModule

Once packaged, you can deploy the JAR file independently and the webapp application should automatically discover and register the module.

To deploy a module with the web application, you can add it as a dependency in the pom.xml file of the Data-centric-Applications-with-Vaadin-8/chapter-02/webapp Maven project. If you are deploying the application as a WAR file to a servlet container, you can add the JAR to the WEB-INF/lib directory.

The following is a screenshot of the application, showing the example module in action:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset