创建扩展名为 .jsp 或 .xhtml 或 .jsf 的 JSF 页面有什么区别

发布于 2024-12-12 07:53:02 字数 158 浏览 0 评论 0原文

我看到一些使用 .jsp 扩展名创建 JSF 页面的示例,使用 .xhtml 扩展名创建 JSF 页面的其他示例,以及其他选择 .jsf 的示例。 我只是想知道使用 JSF 页面时上述扩展有什么区别,以及如何选择合适的扩展?

I saw some examples creating the JSF pages with .jsp extension, other examples creating them with .xhtml extension, and other examples choose .jsf.
I just would like to know what the difference is between above extensions when working with JSF pages, and how to choose the appropriate extension?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那些过往 2024-12-19 07:53:02

JSP 是一种古老的视图技术,广泛与 JSF 1.x 结合使用。 Facelets(被一些人过度概括为 XHTML)是 JSP 的继承者,并作为 JSF 2.x 的默认视图技术引入2009 年底。当您看到 JSP 时,您可能正在阅读针对 JSF 1.x 的过时书籍、教程或资源。在使用 JSF 2.x 进行开发时,您通常应该忽略它们并转向针对 JSF 2.x 的资源,否则您可能会感到困惑,因为在 Facelets 上的 JSF 2.x 中,许多事情的处理方式有所不同。

*.jsf 只是 web.xmlFacesServlet 映射的广泛使用的 URL 模式之一。其他的是 *.faces/faces/*,但它们来自 JSF 1.0/1.1 时代。它们都不代表具体的文件扩展名/路径,而只是一个虚拟文件扩展名/路径,并且只能在 URL 中指定,如下所示 http://example.com/contextname/page.jsf。如果您熟悉基本的 Servlet,那么您应该知道,当请求 URL 与 servlet 的 URL 匹配时,servlet 容器将调用 servlet网址模式。因此,当请求 URL 匹配 *.jsf 时,就会以这种方式调用 FacesServlet。当使用 JSP 时,它实际上会执行 page.jsp。当使用 Facelets 时,这实际上会编译 page.xhtml

从 JSF 2.x 开始,您还可以使用 *.xhtml 作为 URL 模式。这样您在指定 URL 时就不会感到困惑。在带有 Facelets 1.x 的 JSF 1.x 中,不可能使用 *.xhtml 作为 URL 模式,因为 FacesServlet 将在每次调用自身的无限循环中运行。使用 *.xhtml 的另一个优点是,每当最终用户故意更改浏览器地址栏中的 URL 扩展名(例如 .jsf)时,最终用户将无法看到原始 JSF 源代码.xhtml。不可能使用 *.jsp 作为 URL 模式,因为这样容器的内置 JspServlet(已经使用该 URL 模式)将被覆盖,然后 < code>FacesServlet 将无法再依赖 JSP。

另请参阅:

JSP is an old view technology and widely used in combination with JSF 1.x. Facelets (by some people overgeneralized as XHTML) is the successor of JSP and introduced as default view technology of JSF 2.x at end of 2009. When you were seeing JSPs, you were perhaps reading outdated books, tutorials or resources targeted on JSF 1.x. You should generally ignore them when developing with JSF 2.x and head to resources targeted on JSF 2.x, otherwise you may end up in confusion because many things are done differently in JSF 2.x on Facelets.

The *.jsf is just one of widely used URL patterns of the FacesServlet mapping in web.xml. Other ones are *.faces and /faces/*, but those are from back in the JSF 1.0/1.1 ages. They all do not represent the concrete file extension/path, but just a virtual file extension/path and is to be specified in URLs only like so http://example.com/contextname/page.jsf. If you are familiar with basic Servlets, then you should know that the servletcontainer will invoke the servlet when the request URL matches the servlet's URL pattern. So when the request URL matches *.jsf, then the FacesServlet will be invoked this way. When using JSPs, it would actually execute page.jsp. When using Facelets, this would actually compile page.xhtml.

Since JSF 2.x you can also use *.xhtml as URL pattern. This way you don't need to get confused when specifying URLs. Using *.xhtml as URL pattern was not possible in JSF 1.x with Facelets 1.x, because the FacesServlet would then run in an infinite loop calling itself everytime. An additional advantage of using *.xhtml is that the enduser won't be able to see raw JSF source code whenever the enduser purposefully changes the URL extension in browser address bar from for example .jsf to .xhtml. It is not possible to use *.jsp as URL pattern, because this way the container's builtin JspServlet, which is already using that URL pattern, would be overridden and then the FacesServlet wouldn't be able to feed on JSPs anymore.

See also:

哎呦我呸! 2024-12-19 07:53:02

.jsp 文件通常用于使用 JavaServer Pages 定义的 JSF 视图。 .xhtml 文件通常用于使用 Facelets 定义的 JSF 视图。

这可以通过配置进行更改(例如,请参阅 javax.faces.DEFAULT_SUFFIXjavax.faces.FACELETS_SUFFIX 配置参数。)

其他扩展映射 (*.jsf< /code>、*.faces)往往用于通过 FacesServlet 处理请求。这是到 JSF 运行时将处理的视图的逻辑映射。如何处理映射是在 web.xml 中定义的(不必使用扩展来完成;通常使用 /faces/* 映射。

根据规范:

Servlet 映射

对 Web 应用程序的所有请求都映射到特定的 servlet
基于匹配 URL 模式(如 Java Servlet 中所定义)
规范)针对请求 URL 后面的部分
选择此 Web 应用程序的上下文路径。日本科学基金会
实现必须支持定义了
将任何有效的 url-pattern 映射到
FacesServlet。可以使用前缀或扩展名映射。使用时
前缀映射,推荐使用以下映射,但不推荐使用
必填:


 faces-servlet-name ;
/faces/*

使用扩展映射时,建议使用以下映射,但是
不需要:


 faces-servlet-name ;
*.faces

除了 FacesServlet 之外,JSF 实现还可以支持其他
调用 JavaServer Faces 请求处理生命周期的方法,但是
依赖这些机制的应用程序将不可移植。

.jsp files are generally used for JSF views defined using JavaServer Pages. .xhtml files are generally used for JSF views defined using Facelets.

This can be changed via configuration (e.g. see the javax.faces.DEFAULT_SUFFIX and javax.faces.FACELETS_SUFFIX configuration parameters.)

Other extension mappings (*.jsf, *.faces) tend to be used for processing requests via the FacesServlet. This is a logical mapping to the view which the JSF runtime will handle. How mappings are handled is defined in the web.xml (that doesn't have to be done using extensions; the /faces/* mapping is often used.

From the spec:

Servlet Mapping

All requests to a web application are mapped to a particular servlet
based on matching a URL pattern (as defined in the Java Servlet
Specification) against the portion of the request URL after the
context path that selected this web application. JSF
implementations must support web application that define a
<servlet-mapping> that maps any valid url-pattern to the
FacesServlet. Prefix or extension mapping may be used. When using
prefix mapping, the following mapping is recommended, but not
required:

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

When using extension mapping the following mapping is recommended, but
not required:

<servlet-mapping>
<servlet-name> faces-servlet-name </servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>

In addition to FacesServlet, JSF implementations may support other
ways to invoke the JavaServer Faces request processing lifecycle, but
applications that rely on these mechanisms will not be portable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文