Home > Back-end >  Added Vaadin to Java Spring project in IntelliJ and getting build error with add()
Added Vaadin to Java Spring project in IntelliJ and getting build error with add()

Time:09-24

I have created a new project with Spring Initializr:

  • Project: Gradle Project
  • Language: Java
  • Spring Boot: 2.7.4
  • Packaging: JAR
  • Java: 8
  • Dependencies: Spring Boot Actuator, Spring Data JPA, Spring Web, H2 Database, PostgresSQL Driver, Spring Configuration Processor

After this I added some code to be able to interact with REST APIs (GET & POST). I was able to build, run, & test the project.

The next step was add Vaading, so I did the following:

  • Created a new package "views.main" package under the source section.

  • Added a MainView.java class with the following contents:

    package io.enfuse.demo.fundemo.views.main;
    
    import com.vaadin.flow.component.Key;
    import com.vaadin.flow.component.button.Button;
    import com.vaadin.flow.component.checkbox.Checkbox;
    import com.vaadin.flow.component.html.H1;
    import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
    import com.vaadin.flow.component.orderedlayout.VerticalLayout;
    import com.vaadin.flow.component.textfield.TextField;
    import com.vaadin.flow.router.Route;
    
    @Route("")
    public class MainView {
        public MainView() {
            VerticalLayout todosList = new VerticalLayout();
            TextField taskField = new TextField();
            Button addButton = new Button("Add");
            addButton.addClickListener(click -> {
                Checkbox checkbox = new Checkbox(taskField.getValue());
                todosList.add(checkbox);
            });
            addButton.addClickShortcut(Key.ENTER);
    
            add(
                    new H1("Vaadin Todo"),
                    todosList,
                    new HorizontalLayout(
                            taskField,
                            addButton
                    )
            );
        }
    }
    
    • I also updated the build.gradle file to include Vaadin items:
    plugins {
        id 'org.springframework.boot' version '2.7.4'
        id 'io.spring.dependency-management' version '1.0.14.RELEASE'
        id 'com.vaadin' version '23.2.1'
        id 'java'
    }
    
    group = 'io.enfuse.demo'
    version = '0.0.1-SNAPSHOT'
    sourceCompatibility = '1.8'
    
    configurations {
        compileOnly {
            extendsFrom annotationProcessor
        }
    }
    
    repositories {
        mavenCentral()
    }
    
    ext {
        set('vaadinVersion', "23.2.1")
    }
    
    dependencies {
        implementation 'org.springframework.boot:spring-boot-starter-actuator'
        implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
        implementation 'org.springframework.boot:spring-boot-starter-web'
        implementation 'com.vaadin:vaadin-spring-boot-starter'
        runtimeOnly 'com.h2database:h2'
        runtimeOnly 'org.postgresql:postgresql'
        annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
        testImplementation 'org.springframework.boot:spring-boot-starter-test'
    }
    
    dependencyManagement {
        imports {
            mavenBom "com.vaadin:vaadin-bom:${vaadinVersion}"
        }
    }
    
    tasks.named('test') {
        useJUnitPlatform()
    }
    
    
  • Restart IntelliJ

At this point when I go to build I get the following error:

```
    C:\Temp\fundemo_v2\fundemo\src\main\java\io\enfuse\demo\fundemo\views\main\MainView.java:24: error: cannot find symbol
            add(
            ^
      symbol:   method add(H1,VerticalLayout,HorizontalLayout)
      location: class MainView
```

I can look at the dependecies that show Vaadin is included:

IntelliJ Project Dependencies Vaadin

What is missing exactly is not setup correctly?

CodePudding user response:

Your MainView does not extend a Vaadin component and that's why there is no add method.

Try this:

public class MainView extends Div() {
  • Related