NeonMan
NeonMan

Reputation: 663

JSF pages not being executed except index.xhtml

I'm learning Java web and jsf with primefaces.

My project currently has a single index.xhtml file and when I access localhost:8080/appname/ index.xhtml is executed correctly but when I access localhost:8080/appname/index.xhtml the application server returns the jsf source code.

Aditionally, any xhtml file on the faces/ directory will return an error like:

com.sun.faces.context.FacesFileNotFoundException: /face.xhtml Not Found in ExternalContext as a Resource
at com.sun.faces.facelets.impl.DefaultFaceletFactory.resolveURL(DefaultFaceletFactory.java:274)
at com.sun.faces.facelets.impl.DefaultFaceletFactory.resolveURL(DefaultFaceletFactory.java:357)
at com.sun.faces.facelets.impl.DefaultFaceletFactory.getMetadataFacelet(DefaultFaceletFactory.java:250)
at com.sun.faces.application.view.ViewMetadataImpl.createMetadataView(ViewMetadataImpl.java:113)
at com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:241)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)

/index.xhtml:

<?xml version='1.0' encoding='UTF-8' ?>
<!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:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <p:clock/>
    </h:body>
</html>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>Appname</display-name>
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <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>/faces/*</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
        <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>index.jsf</welcome-file>
    </welcome-file-list>
</web-app>

Added application tree:

.
├── faces
│   └── face.xhtml
├── faces.jsf
├── index.xhtml
├── META-INF
│   ├── context.xml
│   └── MANIFEST.MF
└── WEB-INF
    ├── classes
    │   ├── ...
    ├── glassfish-web.xml
    ├── lib
    │   ├── ...
    └── web.xml

Upvotes: 0

Views: 4522

Answers (1)

Hatem Alimam
Hatem Alimam

Reputation: 10048

You need to call the FacesServlet, as your mapping inside the web.xml shows

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
</servlet-mapping>

Try accessing the page with faces in the URL

localhost:8080/appname/faces/index.xhtml

As I can see that you have a folder called faces. You must change this folder name as it is reserved to the servlet url mapping.

Even if you try to access face.xhtml by calling localhost:8080/appname/faces/faces/index.xhtml you would fail to reach it.

Let's say that you changed to folder 'faces' into webFaces then you would access the page

localhost:8080/appname/faces/webFaces/index.xhtml

Hope that helps.

Upvotes: 1

Related Questions