java.lang.RuntimeException Cannot find FacesContext
Asked Answered
C

1

5

I don't know how to continue, but I always get the "java.lang.RuntimeException: Cannot find FacesContext" for my new JSF 1.2 web application. I'm sure it's just some configuration I can't find.

The exception occurs with the first f: or h: tag. Already with the important <f:view> at the beginning.

My index.jsp

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<f:view>
<html>
    <head>
        <title>MyWebsite</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">
    </head>
    <body>
        <div>MyContent</div>
    </body>
</html>
</f:view>

My web.xml looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <context-param>
        <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
        <param-value>.jsp</param-value>
    </context-param>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>720</session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

And then I also have a faces-config.xml that should reference myBean I want to use afterward in the body of the page:

<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="1.2" 
              xmlns="http://java.sun.com/xml/ns/javaee" 
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
              xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">
    <application>
        <view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
    </application>

    <managed-bean>
        <managed-bean-name>myClassName</managed-bean-name>
        <managed-bean-class>
            com.company.className
        </managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>
</faces-config>

What am I missing here?

Clino answered 3/5, 2013 at 8:3 Comment(0)
S
8

java.lang.RuntimeException: Cannot find FacesContext

Thus, the JSF <f:xxx> and <h:xxx> tags are complaining that FacesContext cannot be found. The FacesServlet is the one responsible for creating the faces context. The faces servlet is invoked when the request URL matches its URL pattern, which is in your particular case *.jsf. So, when you open the index.jsp as http://localhost:8080/context/index.jsp, or are relying on the <welcome-file> setting, then you are not invoking the faces servlet and you would indeed get this exception.

You need to open the index.jsp as http://localhost:8080/context/index.jsf, or to set the welcome file entry to index.jsf in order to properly invoke the faces servlet, so that it can create the faces context which is required by the JSF components declared in the JSP page.

Note however that only fixing the welcome file isn't sufficient in this JSF 1.x + Tomcat environment. You also need to supply a physically existing, but completely empty index.jsf file next to the index.jsp file in the webcontent in order to fool Tomcat that index.jsf really exists as welcome file. It would otherwise show a 404 error because it checks the physical presence of the welcome file beforehand.

See also:


Unrelated to the concrete problem, I'm wondering why you're using JSP if you've apparently installed Facelets 1.x and registered its view handler. Facelets is far superior to JSP.

Songstress answered 3/5, 2013 at 11:30 Comment(5)
Yes you're right, I see that we used (in another web project) also a starting page with .jsf suffix and then just redirect to the .jsp pages. Well I still don't really get the idea of JSF anyhow - I'm used to HTML and would like to continue using that syntax - as I have a 100% control over the code the user's getting. So I'll continue with .jsp and forget jsf.Clino
stackoverflow.com/a/4424775 In a nutshell: JSF removes the need to write all that HTML/CSS/JS yourself (you only have to learn which HTML output the JSF components generate so that you can easily pick the right one) and it also removes the need to manually collect submitted values, convert/validate them and put them as bean properties and idenfity and invoke the action method. You ultimately end up with a JSP file as "view" and a javabean as "model". In other words, you end up with less code.Songstress
I must however admit that JSP is a terrible view technology and makes JSF 1.x unnecessarily painful. See also stackoverflow.com/a/3646940Songstress
Well less code for the developer, but the browser will have to handle generated code. So this "less code" will imply less control, 3rd party dependencies and in the end produce frustration in case of problems or customizations. But thanks so much for the comments - maybe I should really look into advanced version like 2.X+.Clino
@FiveO The generated code is HTML. Browser will have to "handle" it you use JSF or not. Overall, any framework will give you 'less control', be it JSF, EJB, JPA, etc. However, most of the time they give you a way to do things "manually". With JSF you can always write pure HTML/CSS/JS, or create your own custom tags/components. You need no 3rd party dependency, just use pure Java EE running on a complete Java Application Server, like Glassfish, JBoss AS or WebSphere AS. IMO, frustration is having to reinvent the wheel over and over again, instead of focusing on business code.Dyslexia

© 2022 - 2024 — McMap. All rights reserved.