java.lang.RuntimeException找不到FacesContext [英] java.lang.RuntimeException Cannot find FacesContext

查看:187
本文介绍了java.lang.RuntimeException找不到FacesContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何继续,但是对于新的JSF 1.2 Web应用程序,我总会收到"java.lang.RuntimeException:找不到FacesContext".我确定这只是我找不到的某些配置.

I don't know how to continue, but I always get the "java.lang.RuntimeException: Cannot find FacesContext" for my new JSF 1.2 web application. I'm sure it's just some configuration I can't find.

第一个f:h:标记发生异常.开头已经有重要的<f:view>.

The exception occurs with the first f: or h: tag. Already with the important <f:view> at the beginning.

我的index.jsp

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<f:view>
<html>
    <head>
        <title>MyWebsite</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">
    </head>
    <body>
        <div>MyContent</div>
    </body>
</html>
</f:view>

我的web.xml看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 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-app_2_5.xsd">
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <context-param>
        <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
        <param-value>.jsp</param-value>
    </context-param>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>720</session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

然后,我还有一个faces-config.xml,它应该在页面正文中引用我以后要使用的myBean:

And then I also have a faces-config.xml that should reference myBean I want to use afterward in the body of the page:

<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="1.2" 
              xmlns="http://java.sun.com/xml/ns/javaee" 
              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">
    <application>
        <view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
    </application>

    <managed-bean>
        <managed-bean-name>myClassName</managed-bean-name>
        <managed-bean-class>
            com.company.className
        </managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>
</faces-config>

我在这里想念什么?

推荐答案

java.lang.RuntimeException: Cannot find FacesContext

因此,JSF <f:xxx><h:xxx>标记抱怨找不到FacesContext. FacesServlet是负责创建面部上下文的对象.当请求URL匹配其URL模式时,将调用face servlet,在您的特殊情况下为*.jsf.因此,当您以http://localhost:8080/context/index.jsp身份打开index.jsp或依赖于<welcome-file>设置时,那么您将不会调用Faces Servlet,并且确实会发生此异常.

Thus, the JSF <f:xxx> and <h:xxx> tags are complaining that FacesContext cannot be found. The FacesServlet is the one responsible for creating the faces context. The faces servlet is invoked when the request URL matches its URL pattern, which is in your particular case *.jsf. So, when you open the index.jsp as http://localhost:8080/context/index.jsp, or are relying on the <welcome-file> setting, then you are not invoking the faces servlet and you would indeed get this exception.

您需要将index.jsp打开为http://localhost:8080/context/index.jsf,或将欢迎文件条目设置为index.jsf,以便正确调用Faces Servlet,以便它可以创建JSF所需的faces上下文.在JSP页面中声明的组件.

You need to open the index.jsp as http://localhost:8080/context/index.jsf, or to set the welcome file entry to index.jsf in order to properly invoke the faces servlet, so that it can create the faces context which is required by the JSF components declared in the JSP page.

但是请注意,在此JSF 1.x + Tomcat环境中,仅修复欢迎文件是不够的.您还需要在Webcontent中的index.jsp文件旁边提供一个物理上存在但完全为空的index.jsf文件,以欺骗Tomcat index.jsf实际上作为欢迎文件存在.否则会显示404错误,因为它会事先检查欢迎文件的物理存在.

Note however that only fixing the welcome file isn't sufficient in this JSF 1.x + Tomcat environment. You also need to supply a physically existing, but completely empty index.jsf file next to the index.jsp file in the webcontent in order to fool Tomcat that index.jsf really exists as welcome file. It would otherwise show a 404 error because it checks the physical presence of the welcome file beforehand.

无关与具体问题无关,我想知道如果您显然已经安装了Facelets 1.x并注册了其视图处理程序,那么为什么要使用JSP. Facelets远优于JSP.

Unrelated to the concrete problem, I'm wondering why you're using JSP if you've apparently installed Facelets 1.x and registered its view handler. Facelets is far superior to JSP.

这篇关于java.lang.RuntimeException找不到FacesContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆