I'm currently playing around with Vaadin 8.4.2 + CDI.
I got a simple view with a simple model class Person
that has 2 Properties (lastname
and firstname
). I have a simple View (GreetPersonView
) and a @ViewScoped
component (MessageDisplay
) that is injected into the view. When the button is pressed, an event is fired and it the MessageDisplay
class observes this event and when the event is fired it will display a simple message (see code below). Now, when MessageDisplay is ViewScoped, the Observer does not get triggered. When I change it to UIScoped, it works. Is this intended? Is this Bug in Vaadin?
@CDIView(GreetPersonView.VIEWNAME)
public class GreetPersonView extends CustomComponent implements View {
public final static String VIEWNAME = "greetperson";
private Person person;
@Inject
private javax.enterprise.event.Event<String> messageEvent;
@Inject
private MessageDisplay messageDisplay;
@Override
public void enter(ViewChangeListener.ViewChangeEvent event) {
person = new Person();
VerticalLayout layout = new VerticalLayout();
Binder<Person> binder = new Binder<>();
person.setFirstName("John");
person.setLastName("Doe");
// An input field for editing injected data
TextField firstName = new TextField("First name");
layout.addComponent(firstName);
TextField lastName = new TextField("Last name");
layout.addComponent(lastName);
binder.bind(firstName,
Person::getFirstName,
Person::setFirstName);
binder.bind(lastName,
Person::getLastName,
Person::setLastName);
binder.readBean(person);
Button helloButton = new Button("Say Hello");
layout.addComponent(helloButton);
helloButton.addClickListener(e -> {
try {
binder.writeBean(person);
messageEvent.fire(person.getFirstName() + " " + person.getLastName());
} catch (ValidationException e1) {
Notification.show("Person could not be saved, " +
"please check error messages for each field.");
}
});
layout.addComponents(messageDisplay);
setCompositionRoot(layout);
}
}
@ViewScoped
public class MessageDisplay extends Panel {
private Label lblMessage;
public MessageDisplay() {
lblMessage = new Label("Nothing to show");
setContent(lblMessage);
}
public void onMessageReceived(@Observes String message) {
this.lblMessage.setValue("Hello " + message);
}
}