2

因此,正如编程中通常那样,我问了一个问题,一个潜在的解决方案导致了更多问题(和错误)。我是使用 servlet、JSF 和 EJB 的新手,并且遇到了依赖注入错误。

这是我最初的问题:JSF h tags not displayed

有人告诉我我需要提出一个新问题,粘贴我的托管 bean 代码和注入错误导致的堆栈跟踪。任何帮助将不胜感激。

托管豆

@ManagedBean(name ="draftSavvyController")
@RequestScoped
public class DraftSavvyController 
{
    // ======================================
    // =             Attributes             =
    // ======================================
    @EJB
    private DraftSavvyBean draftBean;
    private Person person;
    private String name;
    private String password;
    private String userType;
    private String searchTerm;
    private static final String apiKey = "4765b95b1bb4ca5248261b7b9af443db";

    // ======================================
    // =           Public Methods           =
    // ======================================
    public String doRegisterUser()
    {
        draftBean.registerUser(name, password, userType);
        return "chooseBeers.xhtml";
    }

    public String doCheckForUser()
    {        
        if(draftBean.checkForUser(name, password))
        {
            return "chooseBeers.xhtml";
        }
        else return "loginerror.xhtml";
    }

    public String searchForBeers() throws MalformedURLException, IOException, JSONException
    {
        draftBean.searchForBeers(searchTerm, apiKey);

        return "displaybeers.js";
    }

堆栈跟踪

com.sun.faces.mgbean.ManagedBeanCreationException: An error occurred performing resource injection on managed bean draftSavvyController
    at com.sun.faces.mgbean.BeanBuilder.injectResources(BeanBuilder.java:211)
    at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:103)
    at com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:409)
    at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:269)
    at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:244)
    at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:116)
    at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
    at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
    at com.sun.el.parser.AstIdentifier.getValue(AstIdentifier.java:103)
    at com.sun.el.parser.AstValue.getValue(AstValue.java:179)
    at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:224)
    at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
    at javax.faces.component.UIOutput.getValue(UIOutput.java:169)
    at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(HtmlBasicInputRenderer.java:205)
    at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.java:355)
    at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeEnd(HtmlBasicRenderer.java:164)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1764)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
    at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1757)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:402)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
    at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:288)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:662)
Caused by: com.sun.faces.spi.InjectionProviderException: com.sun.enterprise.container.common.spi.util.InjectionException: Exception attempting to inject Remote ejb-ref name=com.draftsavvy.frontend.DraftSavvyController/draftBean,Remote 3.x interface =com.draftsavvy.frontend.DraftSavvyBean,ejb-link=null,lookup=,mappedName=,jndi-name=com.draftsavvy.frontend.DraftSavvyBean,refType=Session into class com.draftsavvy.frontend.DraftSavvyController: Lookup failed for 'java:comp/env/com.draftsavvy.frontend.DraftSavvyController/draftBean' 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}
    at org.glassfish.faces.integration.GlassFishInjectionProvider.inject(GlassFishInjectionProvider.java:194)
    at com.sun.faces.mgbean.BeanBuilder.injectResources(BeanBuilder.java:205)
    ... 56 more
Caused by: com.sun.enterprise.container.common.spi.util.InjectionException: Exception attempting to inject Remote ejb-ref name=com.draftsavvy.frontend.DraftSavvyController/draftBean,Remote 3.x interface =com.draftsavvy.frontend.DraftSavvyBean,ejb-link=null,lookup=,mappedName=,jndi-name=com.draftsavvy.frontend.DraftSavvyBean,refType=Session into class com.draftsavvy.frontend.DraftSavvyController: Lookup failed for 'java:comp/env/com.draftsavvy.frontend.DraftSavvyController/draftBean' 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}
    at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl._inject(InjectionManagerImpl.java:703)
    at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.inject(InjectionManagerImpl.java:470)
    at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.injectInstance(InjectionManagerImpl.java:171)
    at org.glassfish.faces.integration.GlassFishInjectionProvider.inject(GlassFishInjectionProvider.java:184)
    ... 57 more
Caused by: javax.naming.NamingException: Lookup failed for 'java:comp/env/com.draftsavvy.frontend.DraftSavvyController/draftBean' 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.NamingException: Exception resolving Ejb for 'Remote ejb-ref name=com.draftsavvy.frontend.DraftSavvyController/draftBean,Remote 3.x interface =com.draftsavvy.frontend.DraftSavvyBean,ejb-link=null,lookup=,mappedName=,jndi-name=com.draftsavvy.frontend.DraftSavvyBean,refType=Session' .  Actual (possibly internal) Remote JNDI name used for lookup is 'com.draftsavvy.frontend.DraftSavvyBean#com.draftsavvy.frontend.DraftSavvyBean' [Root exception is javax.naming.NamingException: Lookup failed for 'com.draftsavvy.frontend.DraftSavvyBean#com.draftsavvy.frontend.DraftSavvyBean' 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.draftsavvy.frontend.DraftSavvyBean#com.draftsavvy.frontend.DraftSavvyBean not found]]]
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:518)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:455)
    at javax.naming.InitialContext.lookup(InitialContext.java:392)
    at javax.naming.InitialContext.lookup(InitialContext.java:392)
    at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl._inject(InjectionManagerImpl.java:599)
    ... 60 more
Caused by: javax.naming.NamingException: Exception resolving Ejb for 'Remote ejb-ref name=com.draftsavvy.frontend.DraftSavvyController/draftBean,Remote 3.x interface =com.draftsavvy.frontend.DraftSavvyBean,ejb-link=null,lookup=,mappedName=,jndi-name=com.draftsavvy.frontend.DraftSavvyBean,refType=Session' .  Actual (possibly internal) Remote JNDI name used for lookup is 'com.draftsavvy.frontend.DraftSavvyBean#com.draftsavvy.frontend.DraftSavvyBean' [Root exception is javax.naming.NamingException: Lookup failed for 'com.draftsavvy.frontend.DraftSavvyBean#com.draftsavvy.frontend.DraftSavvyBean' 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.draftsavvy.frontend.DraftSavvyBean#com.draftsavvy.frontend.DraftSavvyBean not found]]
    at com.sun.ejb.EjbNamingReferenceManagerImpl.resolveEjbReference(EjbNamingReferenceManagerImpl.java:191)
    at com.sun.enterprise.container.common.impl.ComponentEnvManagerImpl$EjbReferenceProxy.create(ComponentEnvManagerImpl.java:1109)
    at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.lookup(GlassfishNamingManagerImpl.java:776)
    at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.lookup(GlassfishNamingManagerImpl.java:744)
    at com.sun.enterprise.naming.impl.JavaURLContext.lookup(JavaURLContext.java:169)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:498)
    ... 64 more
Caused by: javax.naming.NamingException: Lookup failed for 'com.draftsavvy.frontend.DraftSavvyBean#com.draftsavvy.frontend.DraftSavvyBean' 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.draftsavvy.frontend.DraftSavvyBean#com.draftsavvy.frontend.DraftSavvyBean not found]
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:518)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:455)
    at javax.naming.InitialContext.lookup(InitialContext.java:392)
    at javax.naming.InitialContext.lookup(InitialContext.java:392)
    at com.sun.ejb.EjbNamingReferenceManagerImpl.resolveEjbReference(EjbNamingReferenceManagerImpl.java:186)
    ... 69 more
Caused by: javax.naming.NameNotFoundException: com.draftsavvy.frontend.DraftSavvyBean#com.draftsavvy.frontend.DraftSavvyBean not found
    at com.sun.enterprise.naming.impl.TransientContext.doLookup(TransientContext.java:248)
    at com.sun.enterprise.naming.impl.TransientContext.lookup(TransientContext.java:215)
    at com.sun.enterprise.naming.impl.SerialContextProviderImpl.lookup(SerialContextProviderImpl.java:77)
    at com.sun.enterprise.naming.impl.LocalSerialContextProviderImpl.lookup(LocalSerialContextProviderImpl.java:119)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:505)
    ... 73 more

无状态 Bean

@Stateless
public class DraftSavvyBean implements DraftSavvyBeanRemote {

    // ======================================
    // =             Attributes             =
    // ======================================

    @PersistenceContext(unitName = "DraftSavvyFrontEndPU")
    private EntityManager em;

    // ======================================
    // =           Public Methods           =
    // ======================================
    public void registerUser(String username, String password, String userType)
    {
        //code here
    }

    public boolean checkForUser(String username, String password)
    {
        //code here
    } 

    public void searchForBeers(String searchTerm, String apiKey) throws MalformedURLException, IOException, JSONException
    {
        //code here
    }

}
4

1 回答 1

1

正如 Petr Mensik 所说,您可能不需要远程 EJB(除非您计划从不同的服务器调用该 ejb 的方法)。您可以使用@Local接口,甚至根本不使用接口。只需声明 bean @Stateless 并删除implements DraftSavvyBeanRemote.

如果确实使用接口(本地或远程),则必须注入对接口的引用,而不是 bean。所以你的注入应该是这样的。

@EJB
private DraftSavvyBeanRemote draftBean;
于 2012-08-03T11:45:28.683 回答