如何使xhtml页面与JSF一起工作



有人知道为什么我的xhtml页面不能使用它的命名bean吗?

我在浏览器中得到Welcome #{indexBean.userName}。IndexBean是@Named、@SessionScoped并实现Serializable。

index.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en"
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core">
<h:head>
    <h:outputStylesheet library="css" name="default.css"/>
    <title>Admin Panel</title>
</h:head>
<h:body>
<h:form>
    <h2>Welcome #{indexBean.userName}</h2>
</h:form>
</h:body>
</html>

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>WebAdmin</display-name>
<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
</context-param>
<servlet>
    <servlet-name>FacesServlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>FacesServlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
    <welcome-file>/WEB-INF/faces/index.xhtml</welcome-file>
</welcome-file-list>
</web-app>

Bean

import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
@Named
@SessionScoped
public class IndexBean implements Serializable {
private static final long serialVersionUID = 1L;

MyFaces

INFO: Reading config : jar:file:/C:/tomee16/lib/openwebbeans-el22-1.2.1.jar!/META-INF/faces-config.xml

2013年12月15日10:27:54下午org.apache.myfaces.config.DefaultFacesConfigurationProvider getClassloaderFacesConfig信息:正在读取配置:jar:file:/C:/tomee16/lib/openwebbeans-jsf1.2.1.jar/META-INF/faces-config.xml2013年12月15日10:27:54下午org.apache.myfaces.config.LogMetaInfUtils日志工件信息:在版本"2.1.13"中从路径"file:/C:/tomee16/lib/myfaces-api-2.1.13.jar"找到项目"myfaces api"2013年12月15日10:27:54下午org.apache.myfaces.config.LogMetaInfUtils日志工件信息:在版本"2.1.13"中从路径"file:/C:/tomee16/lib/myfaces-impl-2.11.3.jar"中找到项目"myfaces impl"

XHTML文件只有在通过FacesServlet访问时才会被视为JSF视图。您的FacesServlet被映射到:

<url-pattern>/faces/*</url-pattern>

因此,对于文件foo/bar.xhtml,您必须通过URL http://host/app/faces/foo/bar.xhtml来访问它。

考虑将映射更改为:

<url-pattern>*.xhtml</url-pattern>

假设应用程序中的所有XHTML文件都是JSF视图

首先检查包中的注释:javax.inject.Name和javax.enterprise.context.SessionScoped。第二,如果您使用JEE 6,并且因为您使用的是CDI,您需要激活CDI并添加文件WEB-INF/beans.xml,则此文件可能为空。

可能您调用的页面没有FacesServlet处理。

您应该在您的页面名称url 之前包含/faces/

localhost:8080/WebAdmin/faces/index.xhtml

最新更新