Search code examples
jsf-2

How to hide .xhtml extension from URL


In JSF 2.0, how can I hide .xhtml extension from URL? Can this be configured in web.xml?

I just want to change current URL "http://localhost:8080/sms/faces/admin/account/process_monthly_fee.xhtml" to ".../process_monthly_fee.jsf".

Adding following context parameter in to web.xml does not solve my problem but my application displays nothing:

<context-param>
   <param-name>javax.faces.FACELETS_VIEW_MAPPINGS</param-name>
   <param-value>*.jspx</param-value>
</context-param>

OR

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

my web.xml file is like this :

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

  <display-name>School Management System</display-name>
  <welcome-file-list>
    <welcome-file>faces/index.xhtml</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
    <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
  </servlet-mapping>

 <servlet>
    <servlet-name>upload</servlet-name>
    <servlet-class>com.sms.model.student.Upload</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>upload</servlet-name>
    <url-pattern>/Upload</url-pattern>
  </servlet-mapping> 
<servlet>
    <servlet-name>marks</servlet-name>
    <servlet-class>com.sms.student.service.Mark</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>marks</servlet-name>
    <url-pattern>/marks</url-pattern>
  </servlet-mapping>
  <error-page>
    <exception-type>java.lang.Exception</exception-type>
    <location>/error/error.xhtml</location>
  </error-page>
  <session-config>
    <session-timeout>30</session-timeout>
  </session-config>
<filter>
    <filter-name>Extensions Filter</filter-name>
    <filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>Extensions Filter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>  
<context-param>
<param-name>org.richfaces.skin</param-name>
<param-value>classic</param-value>
</context-param>
<context-param>
<param-name>org.richfaces.enableControlSkinning</param-name>
<param-value>true</param-value>
</context-param>
</web-app>

Solution

  • If you want to just change the extension, follow the advice in the link provided by @Captain Giraffe.

    To completely hide the extensions, you can use either PrettyFaces or OmniFaces.

    The OmniFaces showcase features an example.

    EDIT: I suppose the link provided by @Captain Giraffe solved a different issue - how to have files with a different extension then .xhtml to get picked up by JSF.

    If you want to change the extension at the end of your URL, you can add this to your web.xml:

    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.foo</url-pattern>
    </servlet-mapping>
    

    From now on, your pages will be accessible as /YourApplicationRoot/pagename.foo