Skip to content

Commit dacf049

Browse files
committed
vaadin spring
1 parent 717bd1d commit dacf049

6 files changed

Lines changed: 348 additions & 0 deletions

File tree

‎vaadin-spring/pom.xml‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.baeldung</groupId>
8+
<artifactId>vaadin-spring</artifactId>
9+
<version>0.1.0</version>
10+
11+
<parent>
12+
<groupId>org.springframework.boot</groupId>
13+
<artifactId>spring-boot-starter-parent</artifactId>
14+
<version>2.0.3.RELEASE</version>
15+
</parent>
16+
17+
<properties>
18+
<java.version>1.8</java.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-data-jpa</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>com.vaadin</groupId>
28+
<artifactId>vaadin-spring-boot-starter</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>com.h2database</groupId>
32+
<artifactId>h2</artifactId>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>org.springframework.boot</groupId>
37+
<artifactId>spring-boot-starter-test</artifactId>
38+
<scope>test</scope>
39+
</dependency>
40+
</dependencies>
41+
42+
<dependencyManagement>
43+
<dependencies>
44+
<dependency>
45+
<groupId>com.vaadin</groupId>
46+
<artifactId>vaadin-bom</artifactId>
47+
<version>10.0.1</version>
48+
<type>pom</type>
49+
<scope>import</scope>
50+
</dependency>
51+
</dependencies>
52+
</dependencyManagement>
53+
54+
<build>
55+
<plugins>
56+
<plugin>
57+
<groupId>org.springframework.boot</groupId>
58+
<artifactId>spring-boot-maven-plugin</artifactId>
59+
</plugin>
60+
</plugins>
61+
</build>
62+
63+
</project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import org.springframework.boot.CommandLineRunner;
7+
import org.springframework.boot.SpringApplication;
8+
import org.springframework.boot.autoconfigure.SpringBootApplication;
9+
import org.springframework.context.annotation.Bean;
10+
11+
@SpringBootApplication
12+
public class Application {
13+
14+
private static final Logger log = LoggerFactory.getLogger(Application.class);
15+
16+
public static void main(String[] args) {
17+
SpringApplication.run(Application.class);
18+
}
19+
20+
@Bean
21+
public CommandLineRunner loadData(EmployeeRepository repository) {
22+
return (args) -> {
23+
repository.save(new Employee("Bill", "Gates"));
24+
repository.save(new Employee("Mark", "Zuckerberg"));
25+
repository.save(new Employee("Sundar", "Pichai"));
26+
repository.save(new Employee("Jeff", "Bezos"));
27+
};
28+
}
29+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.baeldung;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.Id;
6+
7+
@Entity
8+
public class Employee {
9+
10+
@Id
11+
@GeneratedValue
12+
private Long id;
13+
14+
private String firstName;
15+
16+
private String lastName;
17+
18+
protected Employee() {
19+
}
20+
21+
public Employee(String firstName, String lastName) {
22+
this.firstName = firstName;
23+
this.lastName = lastName;
24+
}
25+
26+
public Long getId() {
27+
return id;
28+
}
29+
30+
public String getFirstName() {
31+
return firstName;
32+
}
33+
34+
public void setFirstName(String firstName) {
35+
this.firstName = firstName;
36+
}
37+
38+
public String getLastName() {
39+
return lastName;
40+
}
41+
42+
public void setLastName(String lastName) {
43+
this.lastName = lastName;
44+
}
45+
46+
@Override
47+
public String toString() {
48+
return String.format("Employee[id=%d, firstName='%s', lastName='%s']", id, firstName, lastName);
49+
}
50+
51+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package com.baeldung;
2+
3+
import com.vaadin.flow.component.Key;
4+
import com.vaadin.flow.component.KeyNotifier;
5+
import com.vaadin.flow.component.button.Button;
6+
import com.vaadin.flow.component.icon.VaadinIcon;
7+
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
8+
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
9+
import com.vaadin.flow.component.textfield.TextField;
10+
import com.vaadin.flow.data.binder.Binder;
11+
import com.vaadin.flow.spring.annotation.SpringComponent;
12+
import com.vaadin.flow.spring.annotation.UIScope;
13+
import org.springframework.beans.factory.annotation.Autowired;
14+
15+
/**
16+
* A simple example to introduce building forms. As your real application is probably much
17+
* more complicated than this example, you could re-use this form in multiple places. This
18+
* example component is only used in MainView.
19+
* <p>
20+
* In a real world application you'll most likely using a common super class for all your
21+
* forms - less code, better UX.
22+
*/
23+
@SpringComponent
24+
@UIScope
25+
public class EmployeeEditor extends VerticalLayout implements KeyNotifier {
26+
27+
private final EmployeeRepository repository;
28+
29+
/**
30+
* The currently edited employee
31+
*/
32+
private Employee employee;
33+
34+
/* Fields to edit properties in Employee entity */
35+
TextField firstName = new TextField("First name");
36+
TextField lastName = new TextField("Last name");
37+
38+
/* Action buttons */
39+
// TODO why more code?
40+
Button save = new Button("Save", VaadinIcon.CHECK.create());
41+
Button cancel = new Button("Cancel");
42+
Button delete = new Button("Delete", VaadinIcon.TRASH.create());
43+
HorizontalLayout actions = new HorizontalLayout(save, cancel, delete);
44+
45+
Binder<Employee> binder = new Binder<>(Employee.class);
46+
private ChangeHandler changeHandler;
47+
48+
@Autowired
49+
public EmployeeEditor(EmployeeRepository repository) {
50+
this.repository = repository;
51+
52+
add(firstName, lastName, actions);
53+
54+
// bind using naming convention
55+
binder.bindInstanceFields(this);
56+
57+
// Configure and style components
58+
setSpacing(true);
59+
60+
save.getElement().getThemeList().add("primary");
61+
delete.getElement().getThemeList().add("error");
62+
63+
addKeyPressListener(Key.ENTER, e -> save());
64+
65+
// wire action buttons to save, delete and reset
66+
save.addClickListener(e -> save());
67+
delete.addClickListener(e -> delete());
68+
cancel.addClickListener(e -> editEmployee(employee));
69+
setVisible(false);
70+
}
71+
72+
void delete() {
73+
repository.delete(employee);
74+
changeHandler.onChange();
75+
}
76+
77+
void save() {
78+
repository.save(employee);
79+
changeHandler.onChange();
80+
}
81+
82+
public interface ChangeHandler {
83+
void onChange();
84+
}
85+
86+
public final void editEmployee(Employee c) {
87+
if (c == null) {
88+
setVisible(false);
89+
return;
90+
}
91+
final boolean persisted = c.getId() != null;
92+
if (persisted) {
93+
// Find fresh entity for editing
94+
employee = repository.findById(c.getId()).get();
95+
} else {
96+
employee = c;
97+
}
98+
cancel.setVisible(persisted);
99+
100+
// Bind employee properties to similarly named fields
101+
// Could also use annotation or "manual binding" or programmatically
102+
// moving values from fields to entities before saving
103+
binder.setBean(employee);
104+
105+
setVisible(true);
106+
107+
// Focus first name initially
108+
firstName.focus();
109+
}
110+
111+
public void setChangeHandler(ChangeHandler h) {
112+
// ChangeHandler is notified when either save or delete
113+
// is clicked
114+
changeHandler = h;
115+
}
116+
117+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
5+
import java.util.List;
6+
7+
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
8+
9+
List<Employee> findByLastNameStartsWithIgnoreCase(String lastName);
10+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.baeldung;
2+
3+
import com.vaadin.flow.component.button.Button;
4+
import com.vaadin.flow.component.grid.Grid;
5+
import com.vaadin.flow.component.icon.VaadinIcon;
6+
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
7+
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
8+
import com.vaadin.flow.component.textfield.TextField;
9+
import com.vaadin.flow.data.value.ValueChangeMode;
10+
import com.vaadin.flow.router.Route;
11+
import com.vaadin.flow.spring.annotation.UIScope;
12+
import org.springframework.util.StringUtils;
13+
14+
@Route
15+
public class MainView extends VerticalLayout {
16+
17+
private final EmployeeRepository employeeRepository;
18+
19+
private final EmployeeEditor editor;
20+
21+
final Grid<Employee> grid;
22+
23+
final TextField filter;
24+
25+
private final Button addNewBtn;
26+
27+
public MainView(EmployeeRepository repo, EmployeeEditor editor) {
28+
this.employeeRepository = repo;
29+
this.editor = editor;
30+
this.grid = new Grid<>(Employee.class);
31+
this.filter = new TextField();
32+
this.addNewBtn = new Button("New employee", VaadinIcon.PLUS.create());
33+
34+
// build layout
35+
HorizontalLayout actions = new HorizontalLayout(filter, addNewBtn);
36+
add(actions, grid, editor);
37+
38+
grid.setHeight("200px");
39+
grid.setColumns("id", "firstName", "lastName");
40+
grid.getColumnByKey("id").setWidth("50px").setFlexGrow(0);
41+
42+
filter.setPlaceholder("Filter by last name");
43+
44+
// Hook logic to components
45+
46+
// Replace listing with filtered content when user changes filter
47+
filter.setValueChangeMode(ValueChangeMode.EAGER);
48+
filter.addValueChangeListener(e -> listEmployees(e.getValue()));
49+
50+
// Connect selected Employee to editor or hide if none is selected
51+
grid.asSingleSelect().addValueChangeListener(e -> {
52+
editor.editEmployee(e.getValue());
53+
});
54+
55+
// Instantiate and edit new Employee the new button is clicked
56+
addNewBtn.addClickListener(e -> editor.editEmployee(new Employee("", "")));
57+
58+
// Listen changes made by the editor, refresh data from backend
59+
editor.setChangeHandler(() -> {
60+
editor.setVisible(false);
61+
listEmployees(filter.getValue());
62+
});
63+
64+
// Initialize listing
65+
listEmployees(null);
66+
}
67+
68+
// tag::listEmployees[]
69+
void listEmployees(String filterText) {
70+
if (StringUtils.isEmpty(filterText)) {
71+
grid.setItems(employeeRepository.findAll());
72+
} else {
73+
grid.setItems(employeeRepository.findByLastNameStartsWithIgnoreCase(filterText));
74+
}
75+
}
76+
// end::listEmployees[]
77+
78+
}

0 commit comments

Comments
 (0)