我正在使用JBoss AS 7.1开发带有外部数据库(Postgres)的简单EJB应用程序。

我想找出一个Bean中当前登录的用户的用户名。
我收到此异常:

javax.servlet.ServletException: JBAS014558: No EjbContext available as no EJB invocation is active
javax.faces.webapp.FacesServlet.service(FacesServlet.java:606)
org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:62)

根本原因:
javax.naming.NamingException: JBAS014558: No EjbContext available as no EJB invocation is active [Root exception is java.lang.IllegalStateException: JBAS014558: No EjbContext available as no EJB invocation is active]
org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:135)
org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:74)
org.jboss.as.naming.NamingContext.lookup(NamingContext.java:178)
org.jboss.as.naming.InitialContext.lookup(InitialContext.java:123)
org.jboss.as.naming.NamingContext.lookup(NamingContext.java:214)
javax.naming.InitialContext.lookup(InitialContext.java:411)
com.FantasyGame.DataProviderBean.getForests(DataProviderBean.java:130)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:606)
javax.el.BeanELResolver.invokeMethod(BeanELResolver.java:735)
javax.el.BeanELResolver.invoke(BeanELResolver.java:467)
javax.el.CompositeELResolver.invoke(CompositeELResolver.java:246)
org.apache.el.parser.AstValue.getValue(AstValue.java:159)
org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189)
org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
javax.faces.component.UIData.getValue(UIData.java:731)
javax.faces.component.UIData.getDataModel(UIData.java:1798)
javax.faces.component.UIData.setRowIndexWithoutRowStatePreserved(UIData.java:484)
javax.faces.component.UIData.setRowIndex(UIData.java:473)
com.sun.faces.renderkit.html_basic.TableRenderer.encodeBegin(TableRenderer.java:81)
javax.faces.component.UIComponentBase.encodeBegin(UIComponentBase.java:820)
javax.faces.component.UIData.encodeBegin(UIData.java:1118)
javax.faces.component.UIComponent.encodeAll(UIComponent.java:1777)
javax.faces.component.UIComponent.encodeAll(UIComponent.java:1782)
javax.faces.component.UIComponent.encodeAll(UIComponent.java:1782)
com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:402)
com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:125)
javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:288)
com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:62)

尝试访问上下文时:
@Singleton
@ManagedBean(name = "dataProviderBean")public class DataProviderBean {
//...
    InitialContext initialContext = new InitialContext();
    EJBContext sessionContext = (SessionContext)initialContext.lookup("java:comp/EJBContext");

我正在从.xhtml页面访问dataProviderBean:
<h:dataTable value="#{dataProviderBean.getForests()}" var="category">

我以为通过配置添加jndi.properties文件会有所帮助,但仍然无法正常工作。我是初学者,不知道我缺少什么。您是否知道导致问题的原因?我很乐意提供更多信息。

提前致谢。

最佳答案

好的,我完成了这项工作。
可能就像@Szarpul提到的那样,无法从ManagedBean获得EJBContext。因此,我创建了另一个类来提供上下文:

@Stateless
public class ContextProviderBean {

    @Resource
    private SessionContext sctx;

    public void setSctx(SessionContext sctx) {
        this.sctx = sctx;
    }

    public SessionContext getSctx() {
        return sctx;
    }

}

并将其添加到我的ManagedBean中:
@ManagedBean(name = "dataProviderBean")
public class DataProviderBean {
    @EJB
    private ContextProviderBean ctxProvider;

    public DataProviderBean() {
        super();
        ctxProvider = new ContextProviderBean();
    }
//...
}

这样,我可以在DataProviderBean中的任何位置访问上下文:
EJBContext sessionContext = ctxProvider.getSctx();
String currentUser = sessionContext.getCallerPrincipal().getName();

09-27 02:52