FacesContext not found

Jatin picture Jatin · Jul 27, 2011 · Viewed 11.8k times · Source

my jsp page is: Home.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="f"  uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h"  uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<f:view>
    <h:form>
    <center>
        <br><br><br><br>
        <h:outputLink value="EmployeeHome.jsp">Employee Management</h:outputLink><br><br>
        <h:outputLink value="ProjectHome.jsp">Project Management</h:outputLink><br><br>
    </center>
    </h:form>
</f:view>
</body>
</html>

My web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>EmployeeMgmt</display-name>
 <welcome-file-list>
    <welcome-file>Home.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>/faces/*</url-pattern>
  </servlet-mapping>


</web-app>

The exception i got :

org.apache.jasper.JasperException: java.lang.RuntimeException: Cannot find FacesContext
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:500)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:428)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

Answer

McDowell picture McDowell · Jul 27, 2011
<url-pattern>/faces/*</url-pattern>

The URL you use to access the JSF page must include the above pattern. It is likely that you are accessing the JSP directly. You need http://host/app/faces/foo.jsp instead of http://host/app/foo.jsp. The URL pattern routes the request through the JSF servlet which provides the context.


<url-pattern>*.faces</url-pattern>

Personally, I prefer extension mappings like the above where the URL becomes http://host/app/foo.faces