我正在使用 netbeans、glassfish3.1、EJB3 和 JSF2 开发一个 Maven 企业应用程序。我想将 DAOFacade EJB 注入 JSF 托管 bean,但它不起作用。这是错误消息:
Caused by: javax.naming.NamingException: Exception resolving Ejb for 'Remote ejb-ref name=com.procc.beans.AlertController/ejbFacade,Remote 3.x interface =com.procc.dao.AlertFacade,ejb-link=null,lookup=,mappedName=,jndi-name=com.procc.dao.AlertFacade,refType=Session' . Actual (possibly internal) Remote JNDI name used for lookup is 'com.procc.dao.AlertFacade#com.procc.dao.AlertFacade' [Root exception is javax.naming.NamingException: Lookup failed for 'com.procc.dao.AlertFacade#com.procc.dao.AlertFacade' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NameNotFoundException: com.procc.dao.AlertFacade#com.procc.dao.AlertFacade not found]]
DAOFacade 位于 EJB 模块中,而 JSF 托管 bean 位于 war 模块中。
+EAR-项目
--+EJB-模块
------AlertFacade.java
--+WAR-模块
------AlertController.java
我尝试将相同的 EJB 注入到 servlet 中,它运行良好,如下所示:
@WebServlet(name = "DAOServlet", urlPatterns = {"/DAOServlet"})
public class DAOServlet extends HttpServlet {
@EJB
private AlertFacadeLocal alertFacade;
@EJB
private AnomalyFacadeLocal anomalyFacade;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List alertList = alertFacade.findAll();
out.println("found "+alertList.size()+" alerts");
}
}
这是 JSF 托管 bean:@ManagedBean @ViewScoped public class AlertController extends AbstractController implements Serializable {
@EJB
private AlertFacade ejbFacade;
public AlertController() {
super(Alert.class);
}
@PostConstruct
public void init() {
super.setFacade(ejbFacade);
}
}
这是 DAO 门面 EJB:
@Stateless
public class AlertFacade extends AbstractFacade<Alert> implements AlertFacadeLocal {
@PersistenceContext(unitName = "flams_pu")
private EntityManager em;
@Override
protected EntityManager getEntityManager() {
return em;
}
public AlertFacade() {
super(Alert.class);
}
}
我搜索了互联网,但我无法解决问题。请帮忙。
是的,赖特!我忘记了控制器管理的bean。这里是:
@ManagedBean
@ViewScoped
public class AlertController extends AbstractController<Alert> implements Serializable {
@EJB
private AlertFacade ejbFacade;
public AlertController() {
super(Alert.class);
}
@PostConstruct
public void init() {
super.setFacade(ejbFacade);
}
}
在 JSF 页面中:
<p:dataTable var="item" value="#{alertController.items}" paginator="true">
将 ejbFacade 注入 AlertController 会引发错误。