0x44656E6E79
0x44656E6E79

Reputation: 1063

CDI Inject Alternatives {identifier not found}

I already checked other posts but nothing seems to solve my trivial issue. Hope you can help me. I created a J2EE application with NetBeans 8.0.1 and GlassFish 4.1. I have an input.xhtml and a show.xhtml file. (Both are facelets and use template.xthml). In addition I've got a Welcome bean which is using one of two implementations (HelloGreeting or HowdyGreeting) from the Interface Greeting. Since I added the second implementation HowdyGreeting I got the error message shown below.

That's the 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

template.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">

    <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>

These are the two facelet clients (input and show):

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 interface:

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>

Upvotes: 0

Views: 519

Answers (3)

Thrax
Thrax

Reputation: 1964

Seems like your stackTrace indicates that welcome from <h:inputText value="#{welcome.name}" title="name" id="name" required="true"/> is not found.

Try to replace :

@Named

by :

@Named("welcome")

in your Welcome.java bean.

You can also try to specify a @Default annotation for one of your @Alternative implementation.

See : http://docs.oracle.com/javaee/6/tutorial/doc/gjsdf.html (specialization)

Upvotes: 1

Thrax
Thrax

Reputation: 1964

Are you sure you really need to use @Alternative. This is only used in very particular cases, e.g. configuration at deployment time.

See documentation : http://docs.oracle.com/javaee/6/tutorial/doc/gjsdf.html

When you have more than one version of a bean you use for different purposes, you can choose between them during the development phase by injecting one qualifier or another.

Instead of having to change the source code of your application, however, you can make the choice at deployment time by using alternatives.

Upvotes: 0

jimfromsa
jimfromsa

Reputation: 381

Perhaps try changing your backing bean to a managed bean?

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

Upvotes: 0

Related Questions