Starting a Project

Once you have installed a development toolchain, as described in the previous chapter, you are ready to create a Vaadin project.

You have a number of choices for creating a project:

Customize a starter project at start.vaadin.com

The starter wizard lets you create, customize, and download a custom starter application with some views that you can use as starting points. It lets you customize the application theme.

Create a project from a Maven archetype

You can create a project from a Maven archetype either on command-line or with IDEs that support Maven. This way does not support any customization, except for project IDs and names.

Create a project with the Vaadin Plugin for Eclipse

The Eclipse plugin new-project wizard lets you create a Vaadin application. Using the plugin, you avoid the small effort with downloading and importing the application package.

The workflow with the above choices is illustrated in the following:

New project flowchart

Ways to create and run a new project

If you create a project outside an IDE, you need to import it in the IDE. During development, you can run the project with a Maven target, depending on the technology stack you are using.

Technology Stacks

The tools allow creating a project with three different technology stacks. All of them follow the same application architecture, where you have the UI layer as an application view, with a service-layer back-end.

Spring Boot

Spring Boot is a Java framework for creating web services that you can deploy and run easily. It enables using Spring Framework, the popular enterprise application framework for Java EE, with minimal configuration.

The application has a main view, which gets access to the business model service by autowiring.

  1. public class MainView extends VerticalLayout {
  2. public MainView(@Autowired GreetService service) {
  3. ...
  4. }
  5. }

The service is a simple Spring service:

  1. @Service
  2. public class GreetService implements Serializable {
  3. public String greet(String name) {
  4. if (name == null || name.isEmpty()) {
  5. return "Hello anonymous user";
  6. } else {
  7. return "Hello " + name;
  8. }
  9. }
  10. }

CDI and Java EE

The Java Enterprise Edition (EE) includes many features for creating enterprise applications. CDI or context dependency injection is the Java EE way to manage service objects and inject them into applications, in our case Vaadin UIs. CDI requires a Java EE -enabled web container; the starter projects use Apache TomEE.

The starter project includes an example service that handles business data and logic. It is injected in the main view, and can be injected in similar way to other views or elsewhere.

  1. public class MainView extends VerticalLayout {
  2. @Inject
  3. private GreetService greetService;
  4. ...
  5. }

The service is scoped to the Vaadin session, so each user session has its own service instance.

  1. @VaadinSessionScoped
  2. public class GreetService {
  3. public String greet(String name) {
  4. if (name == null || name.isEmpty()) {
  5. return "Hello anonymous user";
  6. } else {
  7. return "Hello " + name;
  8. }
  9. }
  10. }

Plain Java Servlet

You can also choose to develop the application as a plain Java servlet, which you can deploy to any Java web container, which does not need to support Java EE or its features.

In a similar way to the Spring and CDI starters, the plain Java application also has a service to handle business data and logic, but you need to manage access to it by your own.

  1. public class MainView extends VerticalLayout {
  2. public MainView() {
  3. // Use TextField for standard text input
  4. TextField textField = new TextField("Your name");
  5. // Button click listeners can be defined as lambda expressions
  6. GreetService greetService = new GreetService();
  7. Button button = new Button("Say hello",
  8. e -> Notification.show(greetService.greet(textField.getValue())));
  9. ...
  10. }
  11. }

In the plain Java servlet, the service is an ordinary object:

  1. public class GreetService {
  2. public String greet(String name) {
  3. if (name == null || name.isEmpty()) {
  4. return "Hello anonymous user";
  5. } else {
  6. return "Hello " + name;
  7. }
  8. }
  9. }

If you use the web tools to create a project, you then need to import it in your IDE as a Maven project. We will go through that with each major IDE: IntelliJ IDEA, Eclipse IDE, and NetBeans IDE. You can also create, compile, and run the projects on the command-line.

Updated 2021-03-08  Edit this article