1

I'm new to JSF as i have started creating JSF app one week before i'm getting this exception

java.lang.RuntimeException: Cannot find FacesContext

i'm using Eclipse INDIGO

I'have tried with url pattern /faces/*, faces/HelloWorld.jsp, jsf/HelloWorld.jsp can anyone tell me that which url we have to use when...??

my

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>


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

PrtkJn
  • 11
  • 1
  • 5

4 Answers4

2

Based in your web.xml configuration, you should call the pages under "/jsf/" to made them work with the Faces Servlet. There are two possible solutions for this:

Note for the last option: if you're using JSF 1.x, you must not use *.jsp as the url-pattern, it will give you a huge error explained here: Help with JSF 1.2 + Jboss 5.1.0 (it doesn't mind if you'reusing Tomcat, you will have the same error). If you're using JSF 2.x, then there will be no problem, and you should be using Facelets (those pages with xhtml extension) as explained here: What is the difference between JSF and Facelets?


A very simple way to test if JSF is working in your project is to make this simple page:

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

If this page gives you errors, then there must be something else in your project or in your application server that is blocking the Faces Servlet.

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • I tried "*.jsf" this url pattern too but it is showing the same error. When run my project it show this url http://localhost:8080/JSFTutorial/ – PrtkJn Aug 03 '12 at 09:35
  • @PrtkJn please add info about the Application Server you're using (Tomcat, Jetty, JBoss, etc) and the JSF version. Make sure your project has the neccesary jars to work with JSF. After that, try entering to localhost:8080/JSFTutorial/HelloWorld.jsf. If this still gives you errors, there must be something else in your web.xml. – Luiggi Mendoza Aug 03 '12 at 14:11
0

Make sure you have all the correct Jars on your classpath.

Change the following things in your code:

  1. Change your url-pattern in web.xml to *.jsf.

  2. Wrap all your html code inside <f:view> tags

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

Now your URL should be:

localhost:8080/JSFTutorial/HelloWorld.jsf 

Hope this helps!

HashimR
  • 3,803
  • 8
  • 32
  • 49
-1
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
-2

What works for me is to just omit using the Taglib in my jsp file and move them into the html tag such as

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

Becomes

<%@ 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>
Fylix
  • 2,551
  • 6
  • 45
  • 72
  • This will indeed avoid this exception, but the major payoff is that your JSF tags won't run at all :) Just fix the request URL as you see in browser's address bar to match the URL pattern of the `FacesServlet`, exactly as answered by Luiggi. By the way, JSP is deprecated since dec 2009. Get yourself up to date with its successor Facelets. You're almost on the right way by attempting to use Facelets syntax in JSP ;) – BalusC Jan 31 '13 at 16:08
  • Thanks for pointing this out, I dont' know the nuts and bolts to be honest... my introduction to java/jsf just started about 4 hours ago when i started playing the Hello World part and came across this question... always been a .NET guy. What I dont get is why do you have to fix the URL, is there no other solution to just go with .jsp and yet still have the error free? I mean I can bring up index.faces and the page shows up fine but I am just bothered by the error I see in Tomcat logging. – Fylix Jan 31 '13 at 20:44
  • 2
    You're just starting? Perhaps you were focusing too much on JSF 1.x tutorials/books/resources instead of JSF 2.x ones. Start at our JSF wiki page: http://stackoverflow.com/tags/jsf/info There's a sane Hello World and list of links to tutorials. – BalusC Jan 31 '13 at 20:47