Reputation: 373
I am trying to implement spring views in my vaadin application but I get an NPE on my SpringViewProvider ("Cannot add a null view provider"). This is my code:
@SpringUI(path = "permalink")
@Theme("myTheme")
public class PermalinkUI extends UI {
@Autowired
private SpringViewProvider springViewProvider;
private Navigator navigator;
@Override
protected void init(VaadinRequest request) {
final VerticalLayout root = new VerticalLayout();
root.setSizeFull();
root.setMargin(true);
root.setSpacing(true);
setContent(root);
final Panel viewContainer = new Panel();
viewContainer.setSizeFull();
root.addComponent(viewContainer);
root.setExpandRatio(viewContainer, 1.0f);
navigator = new Navigator(this, viewContainer);
navigator.addProvider(springViewProvider);
setNavigator(navigator);
setContent(root);
I have also tried to use a ViewDisplay instead of a single component container (root) but with the same result. I have also implemented a test view to navigate to:
@SpringView(name = "test")
public class PermalinkTest extends VerticalLayout implements View {
@Override
public void enter(ViewChangeListener.ViewChangeEvent event) {
removeAllComponents();
String params = event.getParameters();
addComponent(new Label(params));
}
}
I get this NPE when writing this URL "localhost:8080/permalink/test. What am I missing? Shouldn't the SpringViewProvider be autowired?
Regards Johan
Upvotes: 4
Views: 4577
Reputation: 105
If you need to use SpringViewProvider manually you need to send it through a constructor just like this:
@SpringUI
@Theme(ValoTheme.THEME_NAME)
public class NavigationUI extends UI {
private final SpringViewProvider viewProvider;
@Autowired
public NavigationUI(SpringViewProvider viewProvider) {
this.viewProvider = viewProvider;
}
@Override
protected void init(VaadinRequest request) {
final VerticalLayout root = new VerticalLayout();
root.setSizeFull();
root.setMargin(true);
root.setSpacing(true);
setContent(root);
...
This example was taken from official Spring4Vaadin project - https://github.com/peholmst/vaadin4spring
Upvotes: 1
Reputation: 2900
When using vaadin-spring-2.0.0
, SpringNavigator
is a managed @UIScope
-ed bean so it can be injected directly. (You don't have to inject SpringViewProvider
at all if you otherwise don't need it.)
Besides, you have to manually initialize the SpringNavigator
instance to prevent NullPointerException
:
@SpringUI
public class SampleUi extends UI {
@Inject
private SpringNavigator navigator;
@Override
protected void init(VaadinRequest request) {
navigator.init(this, this); // Explicit init() call
navigator.navigateTo(StartView.NAME);
}
}
Upvotes: 3
Reputation: 3131
Which Spring Vaadin Version are you using? I can sugest you just just go for simple navigator and add viewProvider:
@Autowired
SpringViewProvider viewProvider;
Navigator navi = new Navigator(this,this);
navi.addViewProvider(viewProvider);
navi.setErrorView(ErrorView.class);
This works well in both versions 1.0.0 and 1.1.1, however this trick doesn't work with SpringNavigator (1.1.1)
Upvotes: 0
Reputation: 11
Autowiring should work as soon as you setup the Vaadin - Spring integration correctly.
E.g. the WebServlet must extend SpringVaadinServlet:
@WebServlet(value = "/*", asyncSupported = true)
public static class PermalinkUIServlet extends SpringVaadinServlet{
@Override
protected void servletInitialized() throws ServletException {
super.servletInitialized();
...
}
}
Also have a look at https://vaadin.com/wiki/-/wiki/Main/Vaadin+Spring
Upvotes: 1