JSF的Cannot find FacesContxt错误

When I launch the application, I get the "javax.servlet.ServletException: Cannot find FacesContext". What does it mean?

在一般情况下,是因为你直接调用JSF页面而不是使用Faces Servlet映射造成的.

 Faces Servlet mapping在web.xml文件中配置,可以被配置为 a suffix mapping or a prefix mapping.

Example of suffix mapping:

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

 

Example of prefix mapping:

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

因此, 如果你有一个JSF页面,是位于根目录的 index.jsp 在  suffix mapping的情况下,访问它的URL应该是

 http://hostname/appname/index.jsf 
如果你使用下面的URL:
 http://hostname/appname/index.jsp 
则 "javax.servlet.ServletException: Cannot find FacesContext" exception 会出现.

在 prefix mapping的情况下,URL应该是 :

 http://hostname/appname/faces/index.jsp 

 

为了避免该问题,你可以把你的index.jsp该为home.jsp, 而把index.jsp的内容改为 :

<html>
 <head></head>
<body>
 <jsp:forward page="home.jsf" />
</body>
</html>

上面的示例是使用  *.jsf 后缀mapping.


posted @ 2009-07-31 11:30  gaofei_work  阅读(279)  评论(0编辑  收藏  举报