我想将Vaadin 7与J2EE EJB集成,但我遇到了一个问题,即我无法从servlet中查找有状态bean。我阅读了一个教程,提供了一些建议,如何与CDI集成,但我不想使用CDI。
所以我像这样重写教程。但查找服务找不到我的有状态bean - MyVaadinUI。有人请求帮助我吗?我的代码出了什么问题?我不确定在WAR模块中是否需要一些特殊的配置文件,比如ejb-jar.xml? Becouse我没有。我的应用程序由EAR模块和EJB组成 - 其中只有UserBean和WAR模块,这个类和jee6UiProveder在哪里。谢谢
package cz.simon.webmailapp.web;
@Theme("mytheme")
@SuppressWarnings("serial")
@Stateful
@LocalBean
public class MyVaadinUI extends UI{
@EJB
private UserBean bean;
@WebServlet(value = "/*", asyncSupported = true,
initParams = {
@WebInitParam(name = "UIProvider", value = "cz.simon.webmailapp.web.Jee6UIProvider") })
@VaadinServletConfiguration(productionMode = false, ui = MyVaadinUI.class, widgetset = "cz.simon.webmailapp.web.AppWidgetSet")
public static class Servlet extends VaadinServlet {
public UI getUI() {
Context jndi = null;
UI ui = null;
try {
jndi = new InitialContext();
ui = (UI) jndi.lookup("java:module/MyVaadinUI");
} catch (NamingException ex) {
Logger.getLogger(MyVaadinUI.class.getName()).log(Level.SEVERE, null, ex);
}
return ui;
}
}
@Override
protected void init(VaadinRequest request) {
bean.setUser("Test");
final VerticalLayout layout = new VerticalLayout();
layout.setMargin(true);
setContent(layout);
Button button = new Button(bean.getUser() + "Click Me!!!");
button.addClickListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
layout.addComponent(new Label("Thank you for clicking"));
}
});
layout.addComponent(button);
}
}
答案 0 :(得分:0)
我不认为你做得对。您必须使用CDI for UI注入EJB或显式查找bean。由于您不想为您的UI类使用CDI,因此唯一的选择是使用查找。
@Override
protected void init(VaadinRequest request) {
try {
//TODO Create a getter method that returns you the JNDI name of the bean
userBean = (UserBean) lookup(getJNDINameOfUserBean());
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//TODO do other Stuff.
}
private Object lookup(String jndiName) throws NamingException{
if(null==context){
initContext();
}
return context.lookup(jndiName);
}
private void initContext() throws NamingException{
//Set properties if any and initialize the context
// Class level variable for Context
context = new InitialContext();
}