【问题标题】:JSF pages not being executed except index.xhtmlJSF 页面除了 index.xhtml 之外没有被执行
【发布时间】:2013-12-16 22:56:27
【问题描述】:

我正在使用 primefaces 学习 Java web 和 jsf。

我的项目目前只有一个 index.xhtml 文件,当我访问 localhost:8080/appname/ 时 index.xhtml 被正确执行 但是 当我访问 localhost:8080/appname/index.xhtml应用服务器返回jsf源码。

此外,faces/ 目录中的任何 xhtml 文件都会返回如下错误:

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>

添加应用程序树:

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

【问题讨论】:

    标签: java jsf jakarta-ee glassfish


    【解决方案1】:

    您需要调用FacesServlet,因为您在 web.xml 中的映射显示

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

    尝试访问 URL 中包含面孔的页面

    localhost:8080/appname/faces/index.xhtml

    我可以看到您有一个名为 faces 的文件夹。 您必须更改此文件夹名称,因为它保留给 servlet url 映射。

    即使您尝试通过调用 localhost:8080/appname/faces/faces/index.xhtml 来访问 face.xhtml 你将无法到达它。

    假设您将文件夹“faces”更改为 webFaces,然后您将访问该页面

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

    希望对您有所帮助。

    【讨论】:

    • 返回一个新错误:com.sun.faces.context.FacesFileNotFoundException: /face.xhtml Not Found in ExternalContext as a Resource
    • 因为您正在调用 /face.xhtml,如果您有一个名为 myPage.xhtml 的页面,您将使用 localhost:8080/appname/faces/myPage.xhtml 访问它,因此在您的情况下您的页面是 index.xhtml > localhost:8080/appname/faces/index.xhtml
    • 该异常是localhost:8080/Appname/faces/face.xhtml 的结果,该文件位于预期的域文件系统中。尝试打开不存在的文件会返回通常的 404 错误。
    • 您使用的是哪个应用服务器?尝试重建,并再次完全发布您的项目。该页面可能尚未发货。
    • 如果您可以共享您的 webapp 文件夹层次结构。
    猜你喜欢
    • 2016-06-07
    • 2015-04-20
    • 2013-07-27
    • 2017-02-26
    • 1970-01-01
    • 1970-01-01
    • 2014-03-20
    • 2023-04-07
    • 2018-07-08
    相关资源
    最近更新 更多