views:

295

answers:

1

Been try to learn JSF, and sometimes I see the URL is .jsf and sometimes is .xhtml. Can sometimes fill my knowledge, please? When I create a JSF using Facelet, the file extension is .xhtml, so where does .jsf URL extension come from?

+2  A: 

The .jsf extension is where the FacesServlet is by default mapped on in the web.xml.

<servlet-mapping>
    <servlet-name>facesServlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
</servlet-mapping>

The .xhtml extension is of the actual Facelets page as you've physically placed in the webcontent of your webapp, e.g. Webapp/WebContent/page.xhtml.

If you invoke this page with the .jsf extension, e.g. http://localhost:8080/webapp/page.jsf then the FacesServlet will be invoked, locate the page.xhtml and load/process its JSF components.

Sometimes a *.faces extension or /faces/* foldermapping is been used. But this was from back in the JSF 1.0/1.1 ages. You're free to choose and use whatever mapping you'd like to let FacesServlet listen on, even if it's a nothing-saying *.xyz. The actual page itself should always have the .xhtml extension, but this is configureable by the following <context-param> in web.xml:

<context-param>
    <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
    <param-value>.html</param-value>
</context-param>

This will change the FacesServlet to locate page.html instad of (default) page.xhtml.

BalusC
I see, could you show me how the mapping taking place in web.xml
Harry Pham
When the servletcontainer starts up, it parses web.xml, loads all servlets, remembers the mappings and then on every request it will check if the url matches the servlet mapping and then invoke the servlet. Also see [this answer](http://stackoverflow.com/questions/2183974/difference-each-instance-of-servlet-and-each-thread-of-servlet-in-servlets/2184147#2184147) for a rough example.
BalusC
excellent. Thank you very much.
Harry Pham
You're welcome.
BalusC