Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
615 views
in Technique[技术] by (71.8m points)

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

I'm trying a simple project to work on jboss, but I'm stuck at this error (I already tried using .jsf on the URL). the application in tomcat work's fine

javax.servlet.ServletException: java.lang.RuntimeException: Cannot find FacesContext
 javax.faces.webapp.FacesServlet.service

root cause

javax.faces.FacesException: java.lang.RuntimeException: Cannot find FacesContext
 org.apache.myfaces.context.servlet.ServletExternalContextImpl.dispatch(ServletExternalContextImpl.java:425)
 org.apache.myfaces.application.jsp.JspViewHandlerImpl.renderView(JspViewHandlerImpl.java:211)
 org.apache.myfaces.lifecycle.RenderResponseExecutor.execute(RenderResponseExecutor.java:41)
 org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:132)
 javax.faces.webapp.FacesServlet.service(FacesServlet.java:140)
 org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96

my web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
         version="2.4">
 <listener>
  <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
 </listener> 

<servlet>
    <servlet-name>javax.faces.FacesServlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>javax.faces.FacesServlet</servlet-name>
    <url-pattern>*.faces</url-pattern>
  </servlet-mapping>
  <filter>
    <filter-name>faces-redirect-filter</filter-name>
    <filter-class>core.FacesRedirectFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>faces-redirect-filter</filter-name>
    <url-pattern>*.jsp</url-pattern>
  </filter-mapping>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
</web-app>

faces-config xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE faces-config PUBLIC
"-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
"http://java.sun.com/dtd/web-facesconfig_1_1.dtd">    

<faces-config>
  <managed-bean>
    <managed-bean-name>resumeBean</managed-bean-name>
    <managed-bean-class>core.ResumeBean</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
  </managed-bean>
  <managed-bean>
    <managed-bean-name>colorBean</managed-bean-name>
    <managed-bean-class>core.ColorBean</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
  </managed-bean>
  <navigation-rule>
    <from-view-id>/customize.jsp</from-view-id>
    <navigation-case>
      <from-outcome>same-color</from-outcome>
      <to-view-id>/WEB-INF/results/same-color.jsp</to-view-id>
    </navigation-case>
    <navigation-case>
      <from-outcome>success</from-outcome>
      <to-view-id>/WEB-INF/results/show-preview.jsp</to-view-id>
    </navigation-case>
  </navigation-rule>
  <navigation-rule>
    <from-view-id>/customize-bg.jsp</from-view-id>
    <navigation-case>
      <from-outcome>success</from-outcome>
      <to-view-id>/WEB-INF/results/show-preview2.jsp</to-view-id>
    </navigation-case>
  </navigation-rule>
</faces-config>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...