jakarta-ee - FacesServlet 如何根据 URL 知道要呈现哪个 facelet?

标签 jakarta-ee servlets jsf-2 url-routing facesservlet

我一直在四处查看,但找不到有关 FacesServlet 如何将 URL 解析为 Web 应用程序文件结构中的真实文件的解释。在 servlet 的上下文中,我的理解是 URL 只是您希望客户端使用的虚构名称。然后,在 web.xml 中,您将特定的 servlet 映射到 URL 模式,但 servlet 的真实名称/位置对外界是隐藏的……这是一般的 servlet。

特别是对于 JSF 2,我们处理 FacesServlet,这引出了我的第一个问题:FacesServlet 是我需要在我的应用程序中提供映射详细信息的唯一 servlet(也是我唯一需要的 servlet,句号)? 答案似乎是"is",但如果存在并非如此的情况,请举例说明。

通过阅读其他关于 SO 的问题,我了解到并非所有请求都需要通过 FacesServlet,因此请求基本上分为 A) 对不应由 FacesServlet 处理的静态内容的请求,以及 B) 请求FacesServlet 需要处理的动态内容。 那么,静态内容是如何获取的呢?只是收到一个传入请求,其中的 URL 与 FacesServlet 的 URL 模式不匹配,但与应用文件结构中的真实文件位置匹配?

最后,我的主要问题是:当收到与 FacesServlet 的 URL 模式匹配的请求时,FacesServlet 如何知道要呈现哪个 View 文件 (.xhtml)?使用我需要遵循的 JSF 2 才能使其工作?如果不是,那么我不明白,因为,就像我在上面提到的“通用”servlet 的情况一样,URL 可以包含一个与真实文件名无关的名称,只要它映射到web.xml 文件中的正确 servlet。我觉得我在这里遗漏了一些明显(且重要)的东西。我唯一能想到的是 URL 应该与真实文件位置匹配,或者有另一个映射表或将 URL 与 View 文件相关联的东西。

顺便看了下this question ,这是相关的,但没有任何答案。

谢谢!

最佳答案

servlet specification 中概述了提供静态内容的最简单方法, 第 10.5 节:

A Web application exists as a structured hierarchy of directories. The root of this hierarchy serves as the document root for files that are part of the application. For example, for a Web application with the context path /catalog in a Web container, the index.html file at the base of the Web application hierarchy or in a JAR file inside WEB-INF/lib that includes the index.html under META-INF/resources directory can be served to satisfy a request from /catalog/index.html. If an index.html is present both in the root context and in the META-INF/resources directory of a JAR file in the WEB-INF/lib directory of the application, then the file that is available in the root context MUST be used. The rules for matching URLs to context path are laid out in Chapter 12, “Mapping Requests to Servlets”.

A special directory exists within the application hierarchy named “WEB-INF”. This directory contains all things related to the application that aren’t in the document root of the application. Most of the WEB-INF node is not part of the public document tree of the application. Except for static resources and JSPs packaged in the META- INF/resources of a JAR file that resides in the WEB-INF/lib directory, no other files contained in the WEB-INF directory may be served directly to a client by the container.

也就是说,要提供静态内容,将内容保存在 Web 应用程序的适当目录中就足够了。

Servlet 映射是对这个“隐式”servlet 的补充。因此,大多数 JSF 应用程序只声明 FacesServlet。 IIRC,在最近的 JSF 实现中,如果省略了 servlet 的声明,它甚至会声明自己,因此您甚至不必显式声明它。

FacesServlet 如何定位要使用的 View 的定义在 JSF specification 中定义。 ,特别是第 7.6.2 节:

The terms view identifier and viewId are used interchangeably below and mean the context relative path to the web application resource that produces the view, such as a JSP page or a Facelets page. In the JSP case, this is a context relative path to the jsp page representing the view, such as /foo.jsp. In the Facelets case, this is a context relative path to the XHTML page representing the view, such as /foo.xhtml.

JSF implementations must provide a default ViewHandler implementation, along with a default ViewDeclarationLanguageFactory implementation that vends ViewDeclarationLanguage implementations designed to support the rendering of JSP pages containing JSF components and Facelets pages containing JSF components.

默认实现在下面的 7.6.2.1 节中指定。我保留你的完整报价单。要点是,如果 Faces Servlet 使用前缀映射(例如 /faces/**)进行映射,则 viewId 是前缀后面的 URL 部分,如果 Faces Servlet 使用后缀映射(例如 *.jsf),viewId 是 URL 中跟随上下文路径的部分,替换了文件扩展名。例如,如果 servlet 映射到 *.jsf,则对 URL http://host/context/admin/userlist.jsf 的请求将读取 View 定义来自 Web 应用程序目录中的文件 admin/userlist.xhtml

关于jakarta-ee - FacesServlet 如何根据 URL 知道要呈现哪个 facelet?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15864497/

相关文章:

java - @资源错误: "Naming binding already exists for foo.NewServlet/userName in namespace"

jakarta-ee - 如何使用 weblogic 12c 快速部署多模块 EAR 项目?

java - 基本 request.getParameter 不起作用

multithreading - servlet/jdbc的正确设计和并发问题

servlets - 将 poi 工作簿写入输出流会产生奇怪的值

java - 需要创建ExecutorService的对象

java - Java EE规范的实现在Scala中能否更高效?

java - Primefaces 对话框更新机制

java.lang.ArrayStoreException : sun. 反射.annotation.TypeNotPresentExceptionProxy

jsf-2 - Primefaces 数据表过滤不起作用