JSF示例项目托管bean错误,无法将托管bean消息打印到html

JSF示例项目托管bean错误,无法将托管bean消息打印到html,jsf,jsf-2,Jsf,Jsf 2,我开始实现一个非常基本的JSF应用程序,但我无法将JSF托管Beans消息打印为html 这是我的密码: HelloWorld.java: package com.project.managedbeans; import javax.faces.bean.ManagedBean; import javax.faces.bean.RequestScoped; @ManagedBean(name="helloWorld", eager=true) @RequestScoped public cl

我开始实现一个非常基本的JSF应用程序,但我无法将JSF托管Beans消息打印为html

这是我的密码:

HelloWorld.java:

package com.project.managedbeans;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean(name="helloWorld", eager=true)
@RequestScoped
public class HelloWorld {

private String message;

public HelloWorld(){
    System.out.println("Hello World Managed Bean is created");
}

public String getMessage(){
    return "Hello World ! ";
}

public void setMessage(String message){
    this.message = message;
}
index.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">

<body>
#{helloWorld.message}

</body>
</html>

#{helloWorld.message}
faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>

<faces-config
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_2_1.xsd"
version="2.1">

</faces-config>

和web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-  app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>jsfSampleProject</display-name>
<welcome-file-list>
 <welcome-file>index.html</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>
<context-param>
 <description>State saving method: 'client' or 'server' (=default). See JSF   Specification 2.5.2</description>
 <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
 <param-value>client</param-value>
</context-param>
<context-param>
 <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
 <param-value>resources.application</param-value>
</context-param>
<listener>
 <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
</web-app>

jsfSampleProject
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
Facesservlet
javax.faces.webapp.FacesServlet
1.
Facesservlet
*.jsf
状态保存方法:“客户端”或“服务器”(=默认值)。参见JSF规范2.5.2
javax.faces.STATE_保存方法
客户
javax.servlet.jsp.jstl.fmt.localizationContext
资源.应用
com.sun.faces.config.ConfigureListener
当我在apache服务器上运行应用程序时,在

localhost:8080/jsfSampleProject/is:

#{helloWorld.message}

我在Libraries选项卡下有JSF2.1 Mojorra。你认为问题是什么


谢谢。

问题在于欢迎文件列表接受在web应用程序中实际存在的文件名,即
index.xhtml
注意您的欢迎文件都不符合该条件

接下来,您的文件未被
FacesServlet
处理。也就是说,请求的URL与web.xml中的servlet URL映射不对应注意您的映射
*.jsf
也与您请求的文件不匹配

总而言之,以下web.xml摘录将解决您的问题:

<welcome-file-list>
    <welcome-file>index.xhtml</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>*.xhtml</url-pattern>
</servlet-mapping>

index.xhtml
Facesservlet
javax.faces.webapp.FacesServlet
1.
Facesservlet
*.xhtml

当然,您需要将文件
index.xhtml
放在web应用程序的根目录下。

您遇到了什么错误?你在使用Netbeans吗?我在使用EclipseIDE,我在问题中添加了错误,我得到的是#{helloWorld.message}而不是helloWorld!更改为@SessionScope并让我知道您得到了什么。。(我知道这不是问题)不,没有改变,我尝试了各种方法,包括@ApplicationScoped,但没有改变。我处理这个问题一周了。你需要5分钟来解决这个问题。非常感谢你。。