【问题标题】:CDI Inject Alternatives {identifier not found}CDI Inject Alternatives {identifier not found}
【发布时间】:2014-11-07 12:44:35
【问题描述】:

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

这是 GlassFish 服务器日志:

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

模板.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>

这是两个 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 实现:

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 实现:

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>

【问题讨论】:

  • 尝试从 HelloGreeting 中删除 @Alternative

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


【解决方案1】:

似乎您的 stackTrace 表明未找到来自 &lt;h:inputText value="#{welcome.name}" title="name" id="name" required="true"/&gt;welcome

尝试替换:

@Named

作者:

@Named("welcome")

在您的 Welcome.java 豆中。

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 然后我得到了 NullPointerException
    【解决方案3】:

    你确定你真的需要使用@Alternative。这仅在非常特殊的情况下使用,例如部署时的配置。

    查看文档:http://docs.oracle.com/javaee/6/tutorial/doc/gjsdf.html

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

    但是,您无需更改应用程序的源代码,而是可以在部署时使用替代方案进行选择。

    【讨论】:

    • 稍后我需要它来处理特定于客户端的业务逻辑:/
    猜你喜欢
    • 2017-11-08
    • 2018-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-02
    • 1970-01-01
    • 2011-05-07
    • 2014-12-20
    相关资源
    最近更新 更多