Oracle Java EE 7 JSF 示例不适用于 Tomcat
Posted
技术标签:
【中文标题】Oracle Java EE 7 JSF 示例不适用于 Tomcat【英文标题】:Oracle Java EE 7 JSF example not working with Tomcat 【发布时间】:2015-05-25 00:52:15 【问题描述】:我正在尝试获取与 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 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>
有人能发现什么吗?
【问题讨论】:
你创建了 Hello 类吗? 是的,示例所需的 Hello 类、web.xml 和两个 xhtml 文件都存在且未触及,就像它们来自 Java EE SDK 一样。我的理解是,阅读教程,该示例应该无需修改即可工作。我唯一不同的是在 Tomcat(不是完整的 EE 服务器)而不是 Glassfish 上运行它。我没有将所有源代码都放入示例的唯一原因是它会使问题变得非常非常长。 这是你必须使用 glassfish 这个例子的问题,它使用 CDI,它需要一个应用服务器 您是否指定了任何 bean 类。JSF
中的普通班级将无法与 facelets 通信。您必须将一个类指定为 bean。检查此链接***.com/questions/746047/…
"There is nothing named JEE! Never use that name! The correct short name is "Java EE"".
【参考方案1】:
如果您想在 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;
检查导入它们与示例不同的包
【讨论】:
谢谢,是的,通过提取所有 JEE 包并添加 javax.faces API 和 Mojarra Faces 实现,问题最终得到解决:以上是关于Oracle Java EE 7 JSF 示例不适用于 Tomcat的主要内容,如果未能解决你的问题,请参考以下文章
Java EE 6:JSF 与 Servlet + JSP。我应该费心学习 JSF 吗?