Oracle Java EE 7 JSF 示例不适用于 Tomcat

Oracle Java EE 7 JSF example not working with Tomcat

我正在尝试获取我能找到的与 Tomcat 一起使用的最简单的 JSF 示例:来自 [=32= 的 javaee-tutorial/examples/web/jsf/hello1 示例]EE 7 SDK 下载。这是 official tutorial 用来向您介绍 JSF 的示例。 (完全公开 - 我是 JSF 的新手,但不是 Java 或 Tomcat。)所有文件都直接来自 JavaEE SDK 示例 - 当然,Oracle 希望你使用 Glassfish 和 Netbeans,而不是 Tomcat 和 IntelliJ,因此我尝试添加必要的依赖项以使其编译和 运行。

到目前为止它编译并且 运行 很好,但是当我按下表单上的 "Submit" 按钮时,而不是另一个显示 "Hello, yournamehere!" 的页面,我得到一个错误页面:

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

检查导入它们是与示例不同的包