javax.faces.FacesException: java.lang.RuntimeException: Cannot find FacesContext

跟風遠走 提交于 2019-11-29 16:03:36

javax.faces.FacesException: java.lang.RuntimeException: Cannot find FacesContext

JSF components in the JSP page are complaining that the FacesContext cannot be found. The one responsible for creating this is the FacesServlet.

Here,

<servlet-mapping>
    <servlet-name>javax.faces.FacesServlet</servlet-name>
    <url-pattern>*.faces</url-pattern>
</servlet-mapping>

you've declared the FacesServlet to listen on an url-pattern of *.faces. So, to get the FacesServlet to run (and to create the FacesContext) you need to ensure that the request URL matches http://example.com/context/page.faces and thus not http://example.com/context/page.jsp.

If you rather want to use http://example.com/context/page.jsf, then you need to change the url-pattern of the FacesServlet to *.jsf.


That said, the FacesRedirectFilter is suspicious as well. Isn't it redirecting *.jsp to *.jsf or so? If so, then you need to modify that filter as well. However, if the sole intent is to prevent users from accessing *.jsp files directly without involvement of FacesServlet, then better add the following security constraint to the web.xml:

<security-constraint>
    <display-name>Restrict direct access to JSP files</display-name>
    <web-resource-collection>
        <web-resource-name>JSP files</web-resource-name>
        <url-pattern>*.jsp</url-pattern>
    </web-resource-collection>
    <auth-constraint />
</security-constraint> 

(and remove that Filter).


Unrelated to the problem, you mentioned JSF 1.2, but you've declared your faces-config.xml as JSF 1.1. Any JSF 1.2 implementation or newer will fall back to JSF 1.1 compatibility. You need to declare it as JSF 1.2 as well.

<faces-config version="1.2" 
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xi="http://www.w3.org/2001/XInclude"
    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">

(and get rid of that DOCTYPE)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!