没有EjbContext可用

时间:2015-08-31 18:49:57

标签: java jboss xhtml ejb

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

我想在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文件会有所帮助,但它仍然无效。我是初学者,不知道我错过了什么。你知道造成这个问题的原因吗?我很乐意提供更多信息。

提前致谢。

2 个答案:

答案 0 :(得分:1)

好的,我做了这个工作。 可能就像@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();

答案 1 :(得分:0)

我认为从EJBContext获取ManagedBean是不可能的。您应该从EJB bean内部获取EJBContextHere是教程如何获取EJBContext