我不知道如何继续,但我的新 JSF 1.2 Web 应用程序总是收到“java.lang.RuntimeException:找不到 FacesContext”。我确信这只是我找不到的一些配置。
异常发生在第一个f:
or h:
标签。已经有了重要的<f:view>
一开始。
My 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
看起来像这样:
<?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 我想稍后在页面正文中使用:
<?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>
and <h:xxx>
标签抱怨说FacesContext
找不到。这FacesServlet
是负责创建面孔上下文的人。当请求 URL 与其 URL 模式匹配时(在您的特定情况下),将调用 faces servlet*.jsf
。所以,当你打开index.jsp
as http://localhost:8080/context/index.jsp
,或者依赖于<welcome-file>
设置,那么您就没有调用 faces servlet,并且确实会得到此异常。
您需要打开index.jsp
as http://localhost:8080/context/index.jsf
,或将欢迎文件条目设置为index.jsf
为了正确调用 faces servlet,以便它可以创建 JSP 页面中声明的 JSF 组件所需的 faces 上下文。
但请注意,在此 JSF 1.x + Tomcat 环境中仅修复欢迎文件是不够的。您还需要提供一个实际存在但完全空的index.jsf
文件旁边的index.jsp
将文件添加到 webcontent 中以欺骗 Tomcatindex.jsf
确实作为欢迎文件存在。否则它会显示 404 错误,因为它事先检查了欢迎文件的物理存在。
也可以看看:
- javax.faces.FacesException:java.lang.RuntimeException:找不到 FacesContext https://stackoverflow.com/questions/4491703/javax-faces-facesexception-java-lang-runtimeexception-cannot-find-facescontext/4491972#4491972
无关对于具体问题,我想知道如果您显然已经安装了 Facelets 1.x 并注册了其视图处理程序,为什么还要使用 JSP。面容还很远superior https://stackoverflow.com/questions/13092161/why-facelets-is-preferred-over-jsp-as-the-view-definition-language-from-jsf2-0-o to JSP.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)