javax.faces.FacesException:java.lang.RuntimeException:找不到FacesContext

时间:2010-12-20 16:35:46

标签: jsp jsf jboss5.x

我正在尝试一个简单的项目来处理jboss,但我遇到了这个错误(我已经尝试在URL上使用.jsf)。 tomcat中的应用程序工作正常

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

我的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>

1 个答案:

答案 0 :(得分:3)

  

javax.faces.FacesException:java.lang.RuntimeException:找不到FacesContext

JSP页面中的JSF组件抱怨无法找到FacesContext。负责创建此内容的是FacesServlet

下面,

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

您已声明FacesServlet收听url-pattern *.faces。因此,要让FacesServlet运行(并创建FacesContext),您需要确保请求网址与http://example.com/context/page.faces匹配,因此不是 {{3 }}

如果您想使用http://example.com/context/page.jsp,则需要将url-pattern的{​​{1}}更改为FacesServlet


尽管如此,*.jsf也是可疑的。是不是将FacesRedirectFilter重定向到*.jsp左右?如果是这样,那么您还需要修改该过滤器。但是,如果唯一的目的是阻止用户直接访问*.jsf文件而不涉及*.jsp,那么最好将以下安全约束添加到FacesServlet

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> )。


与此问题无关,您提到了JSF 1.2,但您已将Filter声明为JSF 1.1。任何JSF 1.2实现或更新都将回退到JSF 1.1兼容性。您还需要将其声明为JSF 1.2。

faces-config.xml

(并摆脱<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">