使用 .jsp 或 .xhtml 或 .jsf 扩展名创建 JSF 页面有什么区别 [英] What is the difference between creating JSF pages with .jsp or .xhtml or .jsf extension

查看:31
本文介绍了使用 .jsp 或 .xhtml 或 .jsf 扩展名创建 JSF 页面有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到一些示例使用 .jsp 扩展名创建 JSF 页面,其他示例使用 .xhtml 扩展名创建它们,其他示例选择 .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?

推荐答案

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 中,许多事情的处理方式不同.

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.

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

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.

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

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.

这篇关于使用 .jsp 或 .xhtml 或 .jsf 扩展名创建 JSF 页面有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆