我是 JSF 的新手,因为我在收到此异常前一周就开始创建 JSF 应用程序
java.lang.RuntimeException: Cannot find FacesContext
我正在使用 Eclipse 靛蓝
我已经尝试过使用 url 模式 /faces/*、faces/HelloWorld.jsp、jsf/HelloWorld.jsp 谁能告诉我我们必须在什么时候使用哪个 url...??
我的
web.xml
<display-name>JSFTutorial</display-name>
<welcome-file-list>
<welcome-file>HelloWorld.jsp</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/jsf/*</url-pattern>
</servlet-mapping>
我的 faces.config.xml
<?xml version="1.0" encoding="UTF-8"?>
<managed-bean>
<managed-bean-name>helloWorldBean</managed-bean-name>
<managed-bean-class>com.myhomepageindia.jsftutorial.web.bean.HelloWorldBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<navigation-rule>
<display-name>HelloWorld</display-name>
<from-view-id>/HelloWorld.jsp</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/HelloWorldResult.jsp</to-view-id>
</navigation-case>
</navigation-rule>
我的托管豆
package com.myhomepageindia.jsftutorial.web.bean;
public class HelloWorldBean {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getCompleteName() {
return this.firstName + " " + this.lastName;
}
public String sayHelloWorld() {
return "success";
}
}
HelloWorld.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello World</title>
</head>
<body>
<%
try {
%>
<f:view>
<p>
<h:message id="errors" for="firstName" style="color:red" />
<h:message id="errors1" for="lastName" style="color:red" />
</p>
<h:form>
<h:outputText value="First Name"></h:outputText>
<h:inputText id="firstName" value="#{helloWorldBean.firstName}"
required="true"></h:inputText>
<h:outputText value="Last Name"></h:outputText>
<h:inputText id="lastName" value="#{helloWorldBean.lastName}"
required="true"></h:inputText>
<h:commandButton action="#{helloWorldBean.sayHelloWorld}"
value="Get Complete Name"></h:commandButton>
</h:form>
</f:view>
<%
} catch (Exception e) {
out.println(e);
}
%>
</body>