CDI注入替代品{标识符未找到}

时间:2014-11-07 12:44:36

标签: jsf java-ee ejb cdi glassfish-4

我已经检查了其他帖子,但似乎没有解决我的琐碎问题。希望您能够帮助我。 我使用NetBeans 8.0.1和GlassFish 4.1创建了一个J2EE应用程序。我有一个input.xhtml和一个show.xhtml文件。 (两者都是facelets并使用template.xthml)。另外我有一个Welcome bean,它使用Interface Greeting中的两个实现之一(HelloGreeting或HowdyGreeting)。自从我添加了第二个实现HowdyGreeting后,我收到了如下所示的错误消息。

这是GlassFish Serverlog:

FATAL:   JSF1073: javax.faces.FacesException caught during processing of PROCESS_VALIDATIONS 3 : UIComponent-ClientId=, Message=/input.xhtml @14,102 value="#{welcome.name}": Target Unreachable, identifier 'welcome' resolved to null
FATAL:   /input.xhtml @14,102 value="#{welcome.name}": Target Unreachable, identifier 'welcome' resolved to null
javax.faces.FacesException: /input.xhtml @14,102 value="#{welcome.name}": Target Unreachable, identifier 'welcome' resolved to null
    at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:84)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javax.el.PropertyNotFoundException: /input.xhtml @14,102 value="#{welcome.name}": Target Unreachable, identifier 'welcome' resolved to null
    at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:100)
    at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:95)
    at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1046)
    at javax.faces.component.UIInput.validate(UIInput.java:976)
    at javax.faces.component.UIInput.executeValidate(UIInput.java:1249)
    at javax.faces.component.UIInput.processValidators(UIInput.java:712)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1261)
    at javax.faces.component.UIForm.processValidators(UIForm.java:253)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1261)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1261)
    at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1195)
    at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
    ... 30 more
Caused by: javax.el.PropertyNotFoundException: Target Unreachable, identifier 'welcome' resolved to null
    at com.sun.el.parser.AstValue.getTarget(AstValue.java:174)
    at com.sun.el.parser.AstValue.getType(AstValue.java:86)
    at com.sun.el.ValueExpressionImpl.getType(ValueExpressionImpl.java:201)
    at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:98)
    ... 41 more

所引用:

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:h="http://xmlns.jcp.org/jsf/html">

    <h:head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <h:outputStylesheet name="./css/default.css"/>
        <h:outputStylesheet name="./css/cssLayout.css"/>
        <title>Facelets Template</title>
    </h:head>

    <h:body>

        <div id="top">
            <ui:insert name="top"><h1>Super Cool App</h1></ui:insert>
        </div>

        <div id="content" class="center_content">
            <ui:insert name="content">Content</ui:insert>
        </div>

        <div id="bottom">
            <ui:insert name="bottom"><br/><br/>Powered by GlassFish &amp; NetBeans</ui:insert>
        </div>

    </h:body>

</html>

这是两个facelet客户端(输入和显示):

input.xhtml:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
    <body>

        <ui:composition template="./WEB-INF/templates/template.xhtml">

            <ui:define name="content">                
                <h:form>                    
                    <h:panelGrid columns="3">                        
                        <h:outputText value="Name:"/>
                        <h:inputText value="#{welcome.name}" title="name" id="name" required="true"/>
                        <h:message for="name" style="color: red"/>                      
                    </h:panelGrid>                    
                    <h:commandButton action="show" value="submit"/>                
                </h:form>                       
            </ui:define>

        </ui:composition>

    </body>
</html>

show.xhtml:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:h="http://xmlns.jcp.org/jsf/html">

    <body>

        <ui:composition template="./WEB-INF/templates/template.xhtml">

            <ui:define name="content">
                <h:outputText value="#{welcome.message}"/>
            </ui:define>

        </ui:composition>

    </body>
</html>

beans.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
       bean-discovery-mode="all">
    <alternatives>
        <class>impl.HelloGreeting</class>
    </alternatives>
</beans>

Welcome.java:

package beans;

//import javax.faces.bean.ManagedBean;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
/**
 * @author Kolling, Denny
 */
@Named
@RequestScoped
public class Welcome{
    private String name;
    private String message;

    @Inject Greeting greeting;

    public String getMessage(){
        return greeting.greet(name);
    }
    public void setMessage(String message){
        this.message = message;
    }
    public String getName(){
        return name;
    }
    public void setName(String name){
        this.name = name;
    }
}

Greeting.java界面:

package beans;
/**
 * @author Kolling, Denny
 */
public interface Greeting {
    public String greet(String name);
}

HelloGreeting impl。:

package impl;

import beans.Greeting;
import javax.enterprise.inject.Alternative;
/**
 * @author Kolling, Denny
 */
@Alternative
public class HelloGreeting implements Greeting{

    @Override
    public String greet(String name) {
        return "Hello " + name;
    }
}

HowdyGreeting.java impl。:

package impl;

import beans.Greeting;
import javax.enterprise.inject.Alternative;
/**
 * @author Kolling, Denny
 */
@Alternative
public class HowdyGreeting implements Greeting{

    @Override
    public String greet(String name) {
        return "Howdy " + name;
    }
}

的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
    </welcome-file-list>
</web-app>

3 个答案:

答案 0 :(得分:1)

似乎您的stackTrace表示找不到welcome中的<h:inputText value="#{welcome.name}" title="name" id="name" required="true"/>

尝试替换:

@Named

by:

@Named("welcome")

在您的Welcome.java bean中。

您还可以尝试为@Default实施之一指定@Alternative注释。

请参阅:http://docs.oracle.com/javaee/6/tutorial/doc/gjsdf.html(专业化)

答案 1 :(得分:0)

也许尝试将您的支持bean更改为托管bean?

@ManagedBean(name = "welcome")
@RequestScoped
public class Welcome{

答案 2 :(得分:0)

您确定确实需要使用@Alternative。这仅用于非常特殊的情况,例如部署时的配置。

请参阅文档:http://docs.oracle.com/javaee/6/tutorial/doc/gjsdf.html

  

当您有多个版本的bean用于不同目的时,您可以在开发阶段通过注入一个限定符来选择它们。

     

但是,您可以通过使用替代方案在部署时做出选择,而不必更改应用程序的源代码。