How to make xhtml page work with JSF?

2.9k views Asked by At

Anyone knows why my xhtml page does not work with its named bean?

I get Welcome #{indexBean.userName} in the browser. IndexBean is @Named, @SessionScoped and implements 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

Dec 15, 2013 10:27:54 PM org.apache.myfaces.config.DefaultFacesConfigurationProvider getClassloaderFacesConfig INFO: Reading config : jar:file:/C:/tomee16/lib/openwebbeans-jsf-1.2.1.jar!/META-INF/faces-config.xml Dec 15, 2013 10:27:54 PM org.apache.myfaces.config.LogMetaInfUtils logArtifact INFO: Artifact 'myfaces-api' was found in version '2.1.13' from path 'file:/C:/tomee16/lib/myfaces-api-2.1.13.jar' Dec 15, 2013 10:27:54 PM org.apache.myfaces.config.LogMetaInfUtils logArtifact INFO: Artifact 'myfaces-impl' was found in version '2.1.13' from path 'file:/C:/tomee16/lib/myfaces-impl-2.1.13.jar'

3

There are 3 answers

5
McDowell On BEST ANSWER

XHTML files are only treated as JSF views if you access them via the FacesServlet. Your FacesServlet is mapped to:

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

So for a file foo/bar.xhtml your would have to access it via the URL http://host/app/faces/foo/bar.xhtml.

Consider changing the mapping to:

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

Assumes all XHTML files in the app are JSF views.

0
Hatem Alimam On

Probably you are calling your page without the FacesServlet handling it.

You should include /faces/ before your page name url

localhost:8080/WebAdmin/faces/index.xhtml
0
Cesar Loachamin On

First check the package for the annotations: javax.inject.Named and javax.enterprise.context.SessionScoped. Second If you'r using JEE 6 and because you're using CDI, you need to activate the CDI adding the file WEB-INF/beans.xml, this file can be empty.