jakarta-ee-Vaadin7+EJB集成(如何将有状态bean注入servlet)



我想将Vaadin 7与J2EEEJB集成,但我遇到了一个问题,无法从servlet中查找有状态bean。我读了一篇教程,其中给出了如何与CDI集成的建议,但我不想使用CDI。

所以我把教程改写成这样。但查找服务找不到我的有状态bean——MyVaadinUI。有人能帮帮我吗?我的代码出了什么问题?我不确定我是否需要像ejb-jar.xml这样的WAR模块中的一些特殊配置文件?因为我没有。我的应用程序由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);
}

}

我认为你做得不对。您必须使用CDI for UI来注入EJB,或者显式查找bean。由于您不想将CDI用于UI类,因此留给您的唯一选项是使用look-up。

 @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();   
   }

最新更新