【问题标题】:java.lang.RuntimeException: Cannot find FacesContext [duplicate]java.lang.RuntimeException:找不到 FacesContext [重复]
【发布时间】:2012-08-01 05:05:54
【问题描述】:

我是 JSF 新手,因为我在收到此异常前一周就开始创建 JSF 应用程序

java.lang.RuntimeException: Cannot find FacesContext

我正在使用 Eclipse INDIGO

我尝试过使用 url 模式 /faces/*、faces/HelloWorld.jsp、jsf/HelloWorld.jsp 谁能告诉我我们必须在什么时候使用哪个网址??

我的

web.xml

<display-name>JSFTutorial</display-name>
<welcome-file-list>
    <welcome-file>HelloWorld.jsp</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<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>/jsf/*</url-pattern>
</servlet-mapping>


我的 faces.config.xml

<?xml version="1.0" encoding="UTF-8"?>



<managed-bean>
    <managed-bean-name>helloWorldBean</managed-bean-name>
    <managed-bean-class>com.myhomepageindia.jsftutorial.web.bean.HelloWorldBean</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
</managed-bean>

<navigation-rule>
    <display-name>HelloWorld</display-name>
    <from-view-id>/HelloWorld.jsp</from-view-id>
    <navigation-case>
        <from-outcome>success</from-outcome>
        <to-view-id>/HelloWorldResult.jsp</to-view-id>
    </navigation-case>
</navigation-rule>


我的托管 bean

package com.myhomepageindia.jsftutorial.web.bean;


public class HelloWorldBean {
private String firstName;
private String lastName;

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getCompleteName() {
    return this.firstName + " " + this.lastName;
}

public String sayHelloWorld() {
    return "success";
}

}

HelloWorld.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"         "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello World</title>
</head>
<body>
<%
    try {
%>
<f:view>
    <p>
        <h:message id="errors" for="firstName" style="color:red" />
        <h:message id="errors1" for="lastName" style="color:red" />
    </p>
    <h:form>
        <h:outputText value="First Name"></h:outputText>
        <h:inputText id="firstName" value="#{helloWorldBean.firstName}"
            required="true"></h:inputText>
        <h:outputText value="Last Name"></h:outputText>
        <h:inputText id="lastName" value="#{helloWorldBean.lastName}"
            required="true"></h:inputText>
        <h:commandButton action="#{helloWorldBean.sayHelloWorld}"
            value="Get Complete Name"></h:commandButton>
    </h:form>
</f:view>
<%
    } catch (Exception e) {
        out.println(e);
    }
%>
</body>

【问题讨论】:

  • 您要访问什么网址?
  • 感谢您的回复@HashimR 我尝试了上述所有网址,我想知道 wt 是上述所有内容的差异...
  • 请粘贴您的jsp页面。谢谢!
  • 检查我的答案。看看有没有帮助:)
  • @HashimR 先生,它不工作....:(

标签: jsf


【解决方案1】:

根据您的 web.xml 配置,您应该调用“/jsf/”下的页面以使它们与 Faces Servlet 一起使用。有两种可能的解决方案:

注意最后一个选项:如果你使用 JSF 1.x,你不能使用 *.jsp 作为 url-pattern,它会给你一个巨大的错误解释:@ 987654323@(不用介意,如果你用的是Tomcat,你会遇到同样的错误)。如果您使用的是 JSF 2.x,那么就没有问题,您应该使用 Facelets(那些带有 xhtml 扩展名的页面),如下所述:What is the difference between JSF and Facelets?


测试 JSF 是否在您的项目中工作的一个非常简单的方法是制作这个简单的页面:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"         "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello World</title>
</head>
<body>
    <f:view>
        <h:outputText value="Hello world!" />
    </f:view>
</body>
</html>

如果此页面出现错误,那么您的项目或应用程序服务器中一定有其他东西阻止了 Faces Servlet。

【讨论】:

  • 我也尝试了“*.jsf”这个 url 模式,但它显示了同样的错误。运行我的项目时,它会显示此网址 localhost:8080/JSFTutorial
  • @PrtkJn 请添加有关您正在使用的应用程序服务器(Tomcat、Jetty、JBoss 等)和 JSF 版本的信息。确保您的项目具有与 JSF 一起使用的必要 jar。之后,尝试进入 localhost:8080/JSFTutorial/HelloWorld.jsf。如果这仍然给您带来错误,那么您的 web.xml 中一定还有其他内容。
【解决方案2】:

确保您的类路径中有所有正确的 Jar。

更改代码中的以下内容:

  1. web.xml 中的url-pattern 更改为*.jsf

  2. 将所有 html 代码包装在 &lt;f:view&gt; 标签内

    <%@ page pageEncoding="UTF-8" %>
    <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
    <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
    <!doctype ... >
    <f:view>
        <html>
             ...
        </html>
    </f:view>
    

现在你的网址应该是:

localhost:8080/JSFTutorial/HelloWorld.jsf 

希望这会有所帮助!

【讨论】:

  • 先生,我也尝试过使用“*.jsf”,但它不起作用...:( :'( 我也将所有 html 代码都包含在 标记中...
  • 您是否在类路径中添加了所有必要的 jar?
【解决方案3】:
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

【讨论】:

  • 虽然这可能是一个有效的答案,但它有点小。你可以通过解释你的推理来改进它。
  • OP 显然使用的是 JSP 而不是 Facelets。
  • 请解释你的答案。它目前被视为低质量的帖子。谢谢!
  • 尤其是对旧的、已回答的问题的回答应该能够明确说明为什么它们比其他问题更好。
【解决方案4】:

对我有用的是在我的 jsp 文件中省略使用 Taglib 并将它们移动到 html 标记中,例如

<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html" %>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head><title>Simple jsp page</title></head>
  <body>

     <f:view>
        <h:outputLabel value="Hello, world"/>
     </f:view>

  </body>
</html>

变成

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
   <head><title>Simple jsp page</title></head>
   <body>

     <f:view>
        <h:outputLabel value="Hello, world"/>
     </f:view>

   </body>
</html>

【讨论】:

  • 这确实可以避免这种异常,但主要的好处是您的 JSF 标记根本不会运行 :) 只需修复您在浏览器地址栏中看到的请求 URL 以匹配FacesServlet,正如 Luiggi 所回答的那样。顺便说一句,自 2009 年 12 月起,JSP 就被弃用了。让您自己了解它的继任者 Facelets。通过尝试在 JSP 中使用 Facelets 语法,您几乎走在了正确的道路上;)
  • 感谢您指出这一点,老实说,我不知道具体细节......我对 java/jsf 的介绍刚刚开始大约 4 小时前,当时我开始玩 Hello World 部分并来了在这个问题上......一直是一个 .NET 人。我不明白为什么您必须修复 URL,是否没有其他解决方案可以仅使用 .jsp 并且仍然没有错误?我的意思是我可以调出 index.faces 并且页面显示正常,但我只是对在 Tomcat 日志记录中看到的错误感到困扰。
  • 你才刚刚开始?也许您过于关注 JSF 1.x 教程/书籍/资源,而不是 JSF 2.x 的。从我们的 JSF wiki 页面开始:stackoverflow.com/tags/jsf/info 有一个理智的 Hello World 和教程链接列表。
猜你喜欢
  • 2013-04-27
  • 2011-05-28
  • 2011-07-17
  • 2014-05-22
  • 1970-01-01
  • 1970-01-01
  • 2011-10-13
  • 2019-03-04
相关资源
最近更新 更多