java.lang.RuntimeException:找不到FacesContext

时间:2012-08-03 06:59:48

标签: jsf

我是JSF的新手,因为我在获得此异常前一周开始创建JSF应用程序

java.lang.RuntimeException: Cannot find FacesContext

我正在使用Eclipse INDIGO

我已尝试使用url pattern / 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>


my 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>

4 个答案:

答案 0 :(得分:2)

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

注意最后一个选项:如果您使用的是JSF 1.x,必须使用* .jsp作为url-pattern,它会给您一个巨大的错误:{ {3}}(不介意你使用Tomcat,你会有同样的错误)。如果你正在使用JSF 2.x,那么就没有问题,你应该使用Facelets(那些带有xhtml扩展名的页面),如下所述:Help with JSF 1.2 + Jboss 5.1.0


测试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的其他内容。

答案 1 :(得分:0)

确保您的类路径中包含所有正确的Jars。

更改代码中的以下内容:

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

  2. 将所有html代码包含在<f:view>代码

    <%@ 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>
    
  3. 现在您的网址应为:

    localhost:8080/JSFTutorial/HelloWorld.jsf 
    

    希望这有帮助!

答案 2 :(得分:-1)

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

答案 3 :(得分:-2)

对我来说有用的是在我的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>
相关问题