我正在尝试获得可能使用Tomcat的最简单的JSF示例:JavaEE 7 SDK下载中的javaee-tutorial / examples / web / jsf / hello1示例。这是official tutorial用来向您介绍JSF的示例。 (全面披露-我是JSF的新手,但不是Java或Tomcat的新手。)所有文件都是JavaEE SDK示例的直接来源-当然,Oracle希望您使用Glassfish和Netbeans,而不是Tomcat和IntelliJ,所以我正在尝试添加必要的依赖项以使其能够编译和运行。

到目前为止,它可以编译并运行良好,但是当我按下表单上的“提交”按钮时,而不是另一个页面上显示“您好,您的名字在这里!”。我得到一个错误页面:

An Error Occurred:

/index.xhtml @20,43 value="#{hello.name}": Target Unreachable, identifier 'hello' resolved to null
- Stack Trace

javax.el.PropertyNotFoundException: /index.xhtml @20,43 value="#{hello.name}": Target Unreachable, identifier 'hello' 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.UIForm.processValidators(UIForm.java:253)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1258)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1258)
    at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1195)
    at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
    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.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2440)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2429)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:744)
Caused by: javax.el.PropertyNotFoundException: Target Unreachable, identifier 'hello' resolved to null
    at org.apache.el.parser.AstValue.getTarget(AstValue.java:97)
    at org.apache.el.parser.AstValue.getType(AstValue.java:81)
    at org.apache.el.ValueExpressionImpl.getType(ValueExpressionImpl.java:171)
    at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:98)
    ... 34 more
- Component Tree

<UIViewRoot id="j_id1" inView="true" locale="en_US" renderKitId="HTML_BASIC" rendered="true" transient="false" viewId="/index.xhtml">
javax_faces_location_HEAD
<ComponentResourceContainer id="javax_faces_location_HEAD" inView="true" rendered="true" transient="false">
<UIOutput id="__rf_skinning_resource" inView="true" rendered="true" transient="false"/>
</ComponentResourceContainer>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<UIOutput id="j_idt2" inView="true" rendered="true" transient="false">
<title>Facelets Hello Greeting</title>
</UIOutput>
<HtmlBody id="j_idt4" inView="true" rendered="true" transient="false">
<HtmlForm enctype="application/x-www-form-urlencoded" id="j_idt5" inView="true" prependId="true" rendered="true" submitted="true" transient="false">
<HtmlGraphicImage alt="Duke waving his hand" id="j_idt6" inView="true" ismap="false" rendered="true" transient="false" url="/hello1/javax.faces.resource/duke.waving.gif.xhtml?ln=images" value="/hello1/javax.faces.resource/duke.waving.gif.xhtml?ln=images"/>
<h2>Hello, my name is Duke. What's yours?</h2>
<HtmlInputText disabled="false" id="username" immediate="false" inView="true" localValueSet="false" maxlength="25" readonly="false" rendered="true" required="true" requiredMessage="Error: A name is required." size="-2147483648" submittedValue="yournamehere" title="My name is: " transient="false" valid="true"/>
<p/>
<HtmlCommandButton action="response" actionExpression="response" disabled="false" id="submit" immediate="false" inView="true" readonly="false" rendered="true" transient="false" type="submit" value="Submit"/>
<HtmlCommandButton disabled="false" id="reset" immediate="false" inView="true" readonly="false" rendered="true" transient="false" type="reset" value="Reset"/>
</HtmlForm>
<div class="messagecolor">
<HtmlMessages errorStyle="color: #d20005" globalOnly="false" id="j_idt10" inView="true" infoStyle="color: blue" layout="list" redisplay="true" rendered="true" showDetail="false" showSummary="true" tooltip="false" transient="false"/>
</div>
</HtmlBody>
</html>
</UIViewRoot>
- Scoped Variables

Request Parameters
Name    Value
j_idt5  j_idt5
j_idt5:submit   Submit
j_idt5:username yournamehere
View Attributes
Name    Value
None
Request Attributes
Name    Value
None
Flash Attributes
Name    Value
None
Session Attributes
Name    Value
None
Application Attributes
Name    Value
csfcff  [ ]
Mar 21, 2015 11:15:56 AM - Generated by Mojarra/Facelets


我尝试清理目标目录,重建项目,谷歌搜索此错误-祝您好运。而且此错误似乎非常可靠-我没有做任何更改。

显然,某个地方存在断开连接,但是我不知道它会是什么,并且如果没有一个有效的示例将其进行比较,很难弄清楚。

这是我修改过的POM文件,其他所有内容都来自Oracle示例:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.glassfish.javaeetutorial</groupId>
    <artifactId>hello1</artifactId>
    <version>7.0.5</version>
    <packaging>war</packaging>

    <name>${project.artifactId}</name>

    <properties>
        <javaee.api.version>7.0</javaee.api.version>
        <jsfVersion>2.2.6</jsfVersion>
        <tomcat.version>7.0.55</tomcat.version>
    </properties>

    <dependencies>
        <!-- ********************* JavaEE stuff ************************* -->

        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>${javaee.api.version}</version>
            <scope>provided</scope>
        </dependency>

        <!--  ********************** JSF Dependencies ************************************* -->


        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>${jsfVersion}</version>
        </dependency>

        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>${jsfVersion}</version>
        </dependency>

        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>${jsfVersion}</version>
        </dependency>

        <!-- **************************** RichFaces Dependencies ***********************  -->
        <dependency>
            <groupId>org.richfaces</groupId>
            <artifactId>richfaces</artifactId>
            <version>4.5.3.Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.1.2.Final</version>
        </dependency>

        <!--  ************************** Tomcat dependency to get web.xml stuff to resolve in Intellij -->
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-catalina</artifactId>
            <version>7.0.55</version>
            <scope>provided</scope>
        </dependency>



    </dependencies>

</project>


有人可以发现任何东西吗?

最佳答案

如果要在Tomcat中使用该示例,则有两个选项可将CDI添加到tomcat中,您可以选中此link来描述您所面临的问题,也可以使用JSF ManagedBeans代替CDI

import javax.faces.bean.ManagedBean
import javax.faces.bean.RequestScoped
@ManagedBean
@RequestScoped
public class Hello {

    private String name;

    public Hello() {
    }

    public String getName() {
        return name;
    }

    public void setName(String user_name) {
        this.name = user_name;
    }
}


检查导入与示例中的软件包是否不同

10-06 14:13
查看更多