1
我有MyUI類如何在Vaadin的CDIView中獲取UI實例?
@Theme("mytheme")
@CDIUI("")
public class MyUI extends UI {
@Inject
LoginView loginView;
@Override
protected void init(VaadinRequest vaadinRequest) {
setContent(loginView);
}
...
我與@PostConstruct
@UIScoped
@CDIView(LoginView.VIEWNAME)
public class LoginView extends VerticalLayout implements CustomView {
@PostConstruct
public void initView() {
//initializations elements
Component loginForm = buildLoginForm();
addComponent(loginForm);
setComponentAlignment(loginForm, Alignment.MIDDLE_CENTER);
Notification notification = new Notification("Demo");
notification.setDescription("<span>DEMO</span>");
notification.setHtmlContentAllowed(true);
notification.setStyleName("tray dark small closable login-help");
notification.setPosition(Position.BOTTOM_CENTER);
notification.setDelayMsec(20000);
notification.show(Page.getCurrent()); // NullPointerException
}
}
堆棧跟蹤註釋的一個init方法一的看法是:
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.jboss.weld.injection.producer.DefaultLifecycleCallbackInvoker.invokeMethods(DefaultLifecycleCallbackInvoker.java:98)
... 72 more
Caused by: java.lang.NullPointerException
at com.vaadin.ui.Notification.show(Notification.java:378)
at com.test.claspina.view.LoginView.initView(LoginView.java:103)
... 77 more
我的問題是如何讓UI實例在呈現視圖元素的過程中使用。
我找到了部分解決方案投入要素的全部定義成一個配置方法,這種方法:
public void configure() {
//initializations elements
Component loginForm = buildLoginForm();
addComponent(loginForm);
setComponentAlignment(loginForm, Alignment.MIDDLE_CENTER);
Notification notification = new Notification("Demo");
notification.setDescription("<span>DEMO</span>");
notification.setHtmlContentAllowed(true);
notification.setStyleName("tray dark small closable login-help");
notification.setPosition(Position.BOTTOM_CENTER);
notification.setDelayMsec(20000);
notification.show(Page.getCurrent());
}
在我的UI調用setContent前
loginView.configure();
setContent(loginView);