JSF RuntimeException:找不到FacesContext [英] JSF RuntimeException: Cannot find FacesContext
问题描述
当我在JSP中写入<h:outputText value="Login Name"/>
标记时,收到以下异常消息:
When I write <h:outputText value="Login Name"/>
tag in my JSP, I get the following exception message:
Cannot find FacesContext
没有我的JSP可以正常工作.这是我的JSP:
Without that my JSP works fine. Here is my JSP:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<body>
Login Name <input type="text" value=""/><br>
<h:outputText value="Login Name"/>
Password<input type="password" value=""/><br>
<input type="submit" value="Login">
</body>
</html>
推荐答案
您的代码有两个缺陷:
-
此异常的根本原因是,您忘记了按照
web.xml
中的定义将请求通过FacesServlet
的url-pattern
传递.如果JSP页面的名称例如为page.jsp
,而FacesServlet
的url-pattern
例如为*.jsf
,则需要使用http://example.com/context/page.jsf
而不是.jsp
来调用它.这样,将调用FacesServlet
并创建FacesContext
.否则,页面中的JSF组件会抱怨找不到FacesContext
,您将遇到此特殊异常.
The root cause of this exception is that you forgot to pass the request through the
url-pattern
of theFacesServlet
as definied inweb.xml
. If the JSP page is for example namedpage.jsp
and theurl-pattern
of theFacesServlet
is for example*.jsf
, then you need to invoke it byhttp://example.com/context/page.jsf
instead of.jsp
. This way theFacesServlet
will be invoked and create theFacesContext
. Otherwise the JSF components in the page will complain that theFacesContext
cannot be found and you will face this particular exception.
页面中缺少<f:view>
.将整个<html>
包装在其中.例如
The <f:view>
is missing in the page. Wrap the entire <html>
in it. E.g.
<%@ page pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<!doctype ... >
<f:view>
<html>
...
</html>
</f:view>
顺便说一句,<%@page>
中的import
属性是完全多余的.摆脱它.
By the way, that import
attribute in the <%@page>
is completely superfluous. Get rid of it.
这篇关于JSF RuntimeException:找不到FacesContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!