JSF RuntimeException:找不到FacesContext [英] JSF RuntimeException: Cannot find FacesContext

查看:89
本文介绍了JSF RuntimeException:找不到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>

推荐答案

您的代码有两个缺陷:

  1. 此异常的根本原因是,您忘记了按照web.xml中的定义将请求通过FacesServleturl-pattern传递.如果JSP页面的名称例如为page.jsp,而FacesServleturl-pattern例如为*.jsf,则需要使用http://example.com/context/page.jsf而不是.jsp来调用它.这样,将调用FacesServlet并创建FacesContext.否则,页面中的JSF组件会抱怨找不到FacesContext,您将遇到此特殊异常.

  1. The root cause of this exception is that you forgot to pass the request through the url-pattern of the FacesServlet as definied in web.xml. If the JSP page is for example named page.jsp and the url-pattern of the FacesServlet is for example *.jsf, then you need to invoke it by http://example.com/context/page.jsf instead of .jsp. This way the FacesServlet will be invoked and create the FacesContext. Otherwise the JSF components in the page will complain that the FacesContext 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屋!

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