Mapping JSF .xhtml files to no extension
Asked Answered
T

2

9

In JSF I can map the Faces Servlet to various URL patterns. E.g. to *.xhtml.

What I want however is map the Faces Servlet to no extension. Meaning, if I have a page customers.xhtml in my web root, I would like to request this using http://example.com/customers.

I looked at the question How do I configure JSF url mappings without file extensions? and this works to some degree, but it requires me to map each and every file I have individually (correct me if I'm wrong).

How can I map all my .xhtml files in one go to the Faces Servlet without having to map them individually?

Tejada answered 21/1, 2013 at 11:55 Comment(0)
M
10

That's not possible using the standard means. You basically need to homebrew a servlet filter which is mapped on /* and checks if the current request URL is an extensionless one and if so, then perform a RequestDispatcher#forward() call on the URL with the file extension appended (you know, a forward does not modify the current request URL as a redirect would do). You also need a custom view handler to produce the desired extensionless URLs for JSF <h:form>, <h:link>, etc.

Alternatively, you can use PrettyFaces or OmniFaces' FacesViews so that you don't need to reinvent the wheel. At the bottom of the FacesViews showcase page you can find some easy links directly to the source code which may give you some inspiration.

Mailer answered 21/1, 2013 at 12:1 Comment(2)
I have been searching the web for exactly what you're saying. Do you happen to know if is it planned that future releases of JSF will contemplate this kind of feature?Canale
@João: There are concrete plans to integrate FacesViews in standard JSF. But a lot depends on Servlet 4.0 API and available time/resources.Mailer
B
1

Now, it is possible with the standard. JSF 2.3 solve this problem. An example can be found here. JSF release info

Just use <url-pattern>/pageName</url-pattern> in a servlet mapping of JSF in web.xml

    <servlet>
      <servlet-name>JSF</servlet-name>
      <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
      <servlet-name>JSF</servlet-name>
      <!-- suffix -->
      <!-- if someone open /other.xhtml instead of /other -->
      <url-pattern>*.xhtml</url-pattern>

      <url-pattern>/home</url-pattern><!-- it will map to /home.xhtml -->
      <url-pattern>/other</url-pattern><!-- it will map to /other.xhtml -->
    </servlet-mapping>
Bekah answered 14/6, 2019 at 21:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.