first of all, I've seen all the similar question here on SO regarding the Vaadin CDI, but none of them helps with my problem. I've got here pretty standard scenario: trying to create EAR application with separated modules which are in this case only EJB (jar) and Web app (war).
I's all base on Maven, build and deployment work fine so I won't post (those long) pom files yet. WildFly 8.1.0 FINAL is used as application server.
EJB Module contains just one stateless bean:
@Stateless
public class Greeter implements GreetinInterface {
private final String[] greets = new String[]{"Hi", "Ho", "Hey", "Hello"};
@Override
public String greet() {
return greets[new Random().nextInt(greets.length)];
}
}
@Local
public interface GreetinInterface {
String greet();
}
Bean gets deployed successfully which is confirmed by both the WildFly admin console and following output:
21:50:20,275 INFO [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-6) JNDI bindings for session bean named Greeter in deployment unit subdeployment "WEBModule.war" of deployment "EARModule.ear" are as follows:
java:global/EARModule/WEBModule/Greeter!cz.kousalik.GreetingInterface
java:app/WEBModule/Greeter!cz.kousalik.GreetingInterface
java:module/Greeter!cz.kousalik.GreetingInterface
java:global/EARModule/WEBModule/Greeter
java:app/WEBModule/Greeter
java:module/Greeter
WEB module contains simple Vaadin application generated by Vaadin plugin in netbeans. Contains only one UI, with one Button:
@CDIUI
@Theme("mytheme")
@Widgetset("cz.kousalik.pernik.web.MyAppWidgetset")
public class MyUI extends UI {
@Inject
private GreetinInterface greeter;
@Override
protected void init(VaadinRequest vaadinRequest) {
final VerticalLayout layout = new VerticalLayout();
layout.setMargin(true);
setContent(layout);
Button button = new Button("Click Me");
button.addClickListener(e -> layout.addComponent(new Label("Thank you for clicking, " + greeter.greet())));
layout.addComponent(button);
}
@WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
@VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
public static class MyUIServlet extends VaadinServlet {
}
}
In this situation the application runs, but clicking the Button results in Nullpointer exception, because there is nothing injected inside greeter
field.
After reading through several tutorials I believe the problem is in the servlet part of my UI class. Vaadin CDI tutorial is saying that deleting this part should do the trick. However if I do so I can't access the webapp at all and am just getting Forbidden
.
Could someone help and what would be even better explain how this is supposed to work together? All the tutorials are unfortunately lacking more background information.
So I solved my problem.
The servlet part indeed has to go away from my code and by doing so the VaadinCDIServlet
gets deployed automaticaly.
And what is also important, inside the WEBModules WEB-INF folder beans.xml
(can be empty) must be present.
That's it. Now it works like a charm. More accurate information available here in the Book of Vaadin