gpt4 book ai didi

java - 除index.xhtml 外,JSF 页面未执行

转载 作者:行者123 更新时间:2023-12-01 13:42:23 28 4
gpt4 key购买 nike

我正在使用 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

最佳答案

您需要调用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

希望有帮助。

关于java - 除index.xhtml 外,JSF 页面未执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20622662/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com