tags:

views:

209

answers:

1

When I write <h:outputText value="Login Name"/> tag in my JSP, I get the following exception message:

Cannot find FacesContext

Without that my JSP works fine. Here is my JSP:

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
    <body>
        Login Name <input type="text" value=""/><br>
        <h:outputText value="Login Name"/>
        Password<input type="password" value=""/><br>
        <input type="submit" value="Login">
    </body>
</html>
+3  A: 

There are two flaws in your code:

  1. The root cause of this exception is that you forgot to pass the request through the url-pattern of the FacesServlet as definied in web.xml. If the JSP page is for example named page.jsp and the url-pattern of the FacesServlet is for example *.jsf, then you need to invoke it by http://example.com/context/page.jsf instead of .jsp. This way the FacesServlet will be invoked and create the FacesContext. Otherwise the JSF components in the page will complain that the FacesContext cannot be found and you will face this particular exception.

  2. The <f:view> is missing in the page. Wrap the entire <html> in it. E.g.

     <%@ 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>
    

By the way, that import attribute in the <%@page> is completely superfluous. Get rid of it.

BalusC