. lang。RuntimeException找不到FacesContext

时间:2021-01-07 18:53:26

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.

我不知道如何继续,但我总是得到“java.lang”。RuntimeException:无法为我的新JSF 1.2 web应用程序找到FacesContext。我确定这只是我找不到的一些构型。

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

第一个f:或h:标记出现异常。已经在开始时使用了重要的 查看>

My index.jsp

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

My web.xml looks like this:

我的网络。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>

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

然后还有一个faces-config。在页面正文中应该引用myBean的xml:

<?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>

What am I missing here?

我错过了什么?

1 个解决方案

#1


6  

java.lang.RuntimeException: Cannot find FacesContext

. lang。RuntimeException:找不到FacesContext

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.

因此,JSF 标记都在抱怨找不到FacesContext。FacesServlet负责创建faces上下文。当请求URL与它的URL模式匹配时,将调用faces servlet,这是在您的特殊情况*.jsf中。当你打开索引时。jsp像http://localhost:8080 /背景/索引。jsp,或者依赖于 设置,那么您不会调用faces servlet,您确实会得到这个异常。

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.

你需要打开索引。jsp像http://localhost:8080 /背景/索引。或将欢迎文件项设置为索引。为了正确地调用faces servlet,以便它能够创建JSP页面中声明的jsf组件所需的faces上下文。

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.

但是请注意,在这个JSF 1中仅修复受欢迎文件是不够的。x + Tomcat的环境。您还需要提供一个物理存在但完全空的索引。在索引旁边的jsf文件。jsp文件在webcontent中,以欺骗Tomcat的那个索引。jsf确实作为受欢迎的文件存在。否则,它将显示404错误,因为它预先检查欢迎文件的物理存在。

See also:


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.

与具体问题无关,我想知道,如果您显然已经安装了Facelets 1,那么为什么还要使用JSP呢?注册它的视图处理程序。Facelets比JSP优越得多。

#1


6  

java.lang.RuntimeException: Cannot find FacesContext

. lang。RuntimeException:找不到FacesContext

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.

因此,JSF 标记都在抱怨找不到FacesContext。FacesServlet负责创建faces上下文。当请求URL与它的URL模式匹配时,将调用faces servlet,这是在您的特殊情况*.jsf中。当你打开索引时。jsp像http://localhost:8080 /背景/索引。jsp,或者依赖于 设置,那么您不会调用faces servlet,您确实会得到这个异常。

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.

你需要打开索引。jsp像http://localhost:8080 /背景/索引。或将欢迎文件项设置为索引。为了正确地调用faces servlet,以便它能够创建JSP页面中声明的jsf组件所需的faces上下文。

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.

但是请注意,在这个JSF 1中仅修复受欢迎文件是不够的。x + Tomcat的环境。您还需要提供一个物理存在但完全空的索引。在索引旁边的jsf文件。jsp文件在webcontent中,以欺骗Tomcat的那个索引。jsf确实作为受欢迎的文件存在。否则,它将显示404错误,因为它预先检查欢迎文件的物理存在。

See also:


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.

与具体问题无关,我想知道,如果您显然已经安装了Facelets 1,那么为什么还要使用JSP呢?注册它的视图处理程序。Facelets比JSP优越得多。