I was wondering is there any way to make View @UiScoped?
In a current version of CDI addon (1.0.0.beta4) @CDIView is @ViewScoped. I tried to add @UiScoped to view, but View is created twice, and after quick look in addon source, I realized that adding annotation won’t help.
I have also read discussion at http://dev.vaadin.com/ticket/12378 and I agree that @ViewScoped is good solution, but I also need @UiScoped view.
As a workaround, I’m using @UiScoped custom component injected into view. Is this recomended way to go?
You should be able to just annotate your view with @UIScoped to have that override the default @ViewScoped. If that’s not the case, please file a bug ticket so we can get it fixed.
My mistake. I also use cdi-mvp add-on, and that is the problem.
When I create only View annotated as @UIScoped, everything is fine.
When I use cdi-mpv (modified, AbstractMVPView to suport navigation) instance of View is created twice.
@CDIView(value=ViewToken.VIEW_HOME, supportsParameters=true, uis={MainUI.class})
@UIScoped
public class HomeViewImpl extends AbstractNavigatableMVPView implements HomeView {
@PostConstruct
protected void initView() {
LOG.error("NEW INSTANCE CREATED");
}
}
@ViewInterface(HomeView.class)
@UIScopedStereotype
public class HomePresenter extends AbstractMVPPresenter<HomeView> {
@PostConstruct
@Override
protected void postConstruct() {
super.postConstruct();
LOG.error("Presenter new instance");
}
@Override
public void viewEntered() {
}
}
I suspect that problem lies somwhere in MVPExtension.java in mvp add-on.
Yes. I made a small modification to the cdi-mvp version I’m using to address this.
As a note glassfish 4.1 ( WELD 2.2.2 Final ) wont even deply an application with an MVPView that is annotated with @UIScoped.
In my case I implemented a ViewDisplay as an MVPView ( and this needs to be UIScoped ).
The solution is to make a copy of AbstractMVPPresenter ( with @UIScopedStereotype instead of @ViewScopedStereotype )
You will need some modifications to MVPExtension to handle the UIScoped View and Presenter…
My ViewDisplay is already @UiScoped, and a have modify AbstractMVPPresenter to be @UiScopedStereotype. I’m now stuck with MVPExtension. Can you show me your modifications of MVPExtension?
I was doing something similar, but with no success.
I have implemented your changes but that also didn’t help.
I’m using MVPView that is also annotated with @CDIView. When I navigate to view, CDI add-on create one view instance, but mvp-addon create another one. I suspect that this is hapening here:
protected void postConstruct() {
// ViewInterface must be defined
final Class<? extends MVPView> viewInterface = getClass()
.getAnnotation(ViewInterface.class).value();
view = (T) viewInstance.select(viewInterface).get();
logger.info("Presenter initialized: " + getClass());
}
So, the real problem is how to use mvp-addon with @CDIView and Navigator based navigation?
You should not be using AbstractUIScopedMVPPresenter or AbstractUIScopeMVPView for CDIViews, for a CDIView (CDIViews would imply a ViewScoped ) you should use the normal AbstractMVPPresenter and AbstractMVPView. If you implement a ViewDisplay ( that is injected into the UI itstelf you will need to use AbstractUIScopedMVPPresenter and AbstractUIScopeMVPView.
As Marcus said, CDIView can be annotated as @UiScoped and override default @ViewScoped scope. I tested this and it works as expected.
In my project I’m using cd-mvp, and all of my CDIView-s are also MVPView-s.
I have tested all posible combinations and only working solution is to modify AbstractMVPresenter to use CDIViewProvider to get reference to MVPView (if MVPView is @CDIView). For this situation I have implemented AbstractNavigatableMVPView and AbstractNavigatableMVPPresenter without scope and stereotype annotations and new @NavigatableViewInterface annotation.
Also, I’m using MVPView without @CDIView to implement nested view logic. Something like this:
In this situation all MVPSubView-s are implemented as @ViewScoped AbstractMVPView.
With CDI addon version 1.0.0.beta4 only the first childView wil be created (when user navigate to ParentView). After that every call to setView method will throw exception.
Caused by: java.lang.IllegalStateException: Can't find proper view for @ViewScoped bean, no views are active for this ui.
I looked into cdi addon source on GitHub and there is a fix for this. I built it and now child views are created in @ViewScope.
If I didn’t miss something, with this modifications everything is working correctly.
I filed a
ticket about this issue. Also pushed a new
commit to CDI-MVP’s Github repository including tests that should pass when the bug gets fixed. I’ll publish a new version of the add-on as soon as the issue is resolved.
This issue seems not to be resolved! I am currently running in this issue and have no workaround for this behaviour.
I am not using the MVP-Addon. I am using Vaadin CDI 1.0.3 an annoted my class like this:
[code] @UIScoped @CDIView(QuestionViewImpl.ID)
public class QuestionViewImpl extends AbstractView implements QuestionView {
…
@PostConstruct
private void initLayout() {
…
}
[/code]After that the postconstruct is called twice when i open a window and close it. This destroys all references of my view.