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页面的示例,还有一些使用.xhtml扩展来创建JSF页面的示例,还有一些示例选择. JSF。我只是想知道在使用JSF页面时,上述扩展之间的区别是什么,以及如何选择适当的扩展?
2 个解决方案
#1
53
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.
JSP是一种古老的视图技术,与JSF 1.x结合使用广泛。Facelets(有些人将其概括为XHTML)是JSP的继承者,并作为JSF 2的默认视图技术引入。x在2009年底。当您看到jsp时,您可能正在阅读针对JSF 1.x的过时的书籍、教程或资源。在使用JSF 2开发时,通常应该忽略它们。然后转向JSF 2上的资源。否则,您可能会陷入混乱,因为在JSF 2中,许多事情都是不同的。Facelets x。
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只是web.xml中FacesServlet映射中广泛使用的URL模式之一。其他的*。面和/faces/*,但它们来自JSF 1.0/1.1时代。它们都不表示具体的文件扩展名/路径,而只是一个虚拟文件扩展名/路径,并且只能像http://example.com/contextname/page.jsf那样在url中指定。如果您熟悉基本的servlet,那么您应该知道,当请求URL匹配servlet的URL模式时,servletcontainer将调用servlet。当请求URL匹配*时。然后FacesServlet将以这种方式调用。当使用jsp时,它实际上会执行page.jsp。当使用Facelets时,这实际上会编译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.
因为JSF 2。你也可以用*。xhtml是URL模式。这样,在指定url时就不需要混淆了。使用*。在JSF 1中不可能使用xhtml作为URL模式。x Facelets 1。x,因为FacesServlet会在每次调用自身的无限循环中运行。使用*的另一个优点。xhtml是,当最终用户有意地将浏览器地址栏中的URL扩展名从. JSF更改为.xhtml时,最终用户将无法看到原始JSF源代码。不可能使用*。jsp作为URL模式,因为该容器的builtin JspServlet(已经在使用该URL模式)将被覆盖,而FacesServlet将无法再以jsp为食。
See also:
- What is the difference between JSF, Servlet and JSP?
- JSF、Servlet和JSP之间有什么区别?
- Why Facelets is preferred over JSP as the view definition language from JSF2.0 onwards?
- 为什么从JSF2.0开始,Facelets优先于JSP作为视图定义语言?
- JSF Facelets: Sometimes I see the URL is .jsf and sometimes .xhtml. Why?
- JSF Facelets:有时我看到URL是. JSF,有时是。xhtml。为什么?
#2
11
.jsp
files are generally used for JSF views defined using JavaServer Pages. .xhtml
files are generally used for JSF views defined using Facelets.
jsp文件通常用于使用javascript页面定义的JSF视图,.xhtml文件通常用于使用Facelets定义的JSF视图。
This can be changed via configuration (e.g. see the javax.faces.DEFAULT_SUFFIX
and javax.faces.FACELETS_SUFFIX
configuration parameters.)
这可以通过配置进行更改(例如,请参见javax.faces)。DEFAULT_SUFFIX javax.faces。FACELETS_SUFFIX配置参数)。
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.
其他扩展映射(*。jsf、*.faces)通常用于通过FacesServlet处理请求。这是一个到JSF运行时将处理的视图的逻辑映射。如何处理映射是在web中定义的。xml(不需要使用扩展来完成;经常使用/faces/*映射。
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:所有对web应用程序的请求都映射到一个特定的servlet,该servlet基于在选择这个web应用程序的上下文路径之后,根据URL模式(在Java servlet规范中定义)对请求URL的部分进行匹配。JSF实现必须支持定义
的web应用程序,该应用程序将任何有效的url模式映射到FacesServlet。可以使用前缀或扩展映射。使用前缀映射时,建议使用以下映射,但不需要: 映射><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.
除了FacesServlet之外,JSF实现还可以支持调用JavaServer Faces请求处理生命周期的其他方法,但是依赖这些机制的应用程序将无法移植。
#1
53
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.
JSP是一种古老的视图技术,与JSF 1.x结合使用广泛。Facelets(有些人将其概括为XHTML)是JSP的继承者,并作为JSF 2的默认视图技术引入。x在2009年底。当您看到jsp时,您可能正在阅读针对JSF 1.x的过时的书籍、教程或资源。在使用JSF 2开发时,通常应该忽略它们。然后转向JSF 2上的资源。否则,您可能会陷入混乱,因为在JSF 2中,许多事情都是不同的。Facelets x。
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只是web.xml中FacesServlet映射中广泛使用的URL模式之一。其他的*。面和/faces/*,但它们来自JSF 1.0/1.1时代。它们都不表示具体的文件扩展名/路径,而只是一个虚拟文件扩展名/路径,并且只能像http://example.com/contextname/page.jsf那样在url中指定。如果您熟悉基本的servlet,那么您应该知道,当请求URL匹配servlet的URL模式时,servletcontainer将调用servlet。当请求URL匹配*时。然后FacesServlet将以这种方式调用。当使用jsp时,它实际上会执行page.jsp。当使用Facelets时,这实际上会编译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.
因为JSF 2。你也可以用*。xhtml是URL模式。这样,在指定url时就不需要混淆了。使用*。在JSF 1中不可能使用xhtml作为URL模式。x Facelets 1。x,因为FacesServlet会在每次调用自身的无限循环中运行。使用*的另一个优点。xhtml是,当最终用户有意地将浏览器地址栏中的URL扩展名从. JSF更改为.xhtml时,最终用户将无法看到原始JSF源代码。不可能使用*。jsp作为URL模式,因为该容器的builtin JspServlet(已经在使用该URL模式)将被覆盖,而FacesServlet将无法再以jsp为食。
See also:
- What is the difference between JSF, Servlet and JSP?
- JSF、Servlet和JSP之间有什么区别?
- Why Facelets is preferred over JSP as the view definition language from JSF2.0 onwards?
- 为什么从JSF2.0开始,Facelets优先于JSP作为视图定义语言?
- JSF Facelets: Sometimes I see the URL is .jsf and sometimes .xhtml. Why?
- JSF Facelets:有时我看到URL是. JSF,有时是。xhtml。为什么?
#2
11
.jsp
files are generally used for JSF views defined using JavaServer Pages. .xhtml
files are generally used for JSF views defined using Facelets.
jsp文件通常用于使用javascript页面定义的JSF视图,.xhtml文件通常用于使用Facelets定义的JSF视图。
This can be changed via configuration (e.g. see the javax.faces.DEFAULT_SUFFIX
and javax.faces.FACELETS_SUFFIX
configuration parameters.)
这可以通过配置进行更改(例如,请参见javax.faces)。DEFAULT_SUFFIX javax.faces。FACELETS_SUFFIX配置参数)。
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.
其他扩展映射(*。jsf、*.faces)通常用于通过FacesServlet处理请求。这是一个到JSF运行时将处理的视图的逻辑映射。如何处理映射是在web中定义的。xml(不需要使用扩展来完成;经常使用/faces/*映射。
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:所有对web应用程序的请求都映射到一个特定的servlet,该servlet基于在选择这个web应用程序的上下文路径之后,根据URL模式(在Java servlet规范中定义)对请求URL的部分进行匹配。JSF实现必须支持定义
的web应用程序,该应用程序将任何有效的url模式映射到FacesServlet。可以使用前缀或扩展映射。使用前缀映射时,建议使用以下映射,但不需要: 映射><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.
除了FacesServlet之外,JSF实现还可以支持调用JavaServer Faces请求处理生命周期的其他方法,但是依赖这些机制的应用程序将无法移植。