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

查看:33
本文介绍了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: 标签上.已经在开头使用了重要的 .

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 看起来像这样:

My web.xml looks like this:

<?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:找不到 FacesContext

因此,JSF 标签抱怨找不到 FacesContext.FacesServlet 负责创建人脸上下文.当请求 URL 与其 URL 模式匹配时,faces servlet 被调用,在您的特定情况下是 *.jsf.所以,当你打开index.jsphttp://localhost:8080/context/index.jsp,或者依赖 设置,那么您就不会调用 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.jsphttp://localhost:8080/context/index.jsf,或者设置欢迎文件入口为index.jsf 以便正确调用faces servlet,以便它可以创建JSP 页面中声明的JSF 组件所需的faces 上下文.

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天全站免登陆