5

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>
Narayan Subedi
  • 1,343
  • 2
  • 20
  • 46
  • http://stackoverflow.com/questions/5416546/jsf-2-0-view-file-name-extension-other-than-xhtml – Captain Giraffe Dec 07 '12 at 15:12
  • @ Captain Giraffe, i also saw that but that does not work... when i add that context parameter in web.xml, my whole project does not display... – Narayan Subedi Dec 07 '12 at 15:37
  • You just want to replace `.xhtml` by `.jsf`? Stating *"Hide `.xhtml` extension"* is completely different from *"Replace `.xhtml` extension"*. Work on your English. – BalusC Dec 07 '12 at 16:42
  • have revised my answer - please tell me if it is helpful – kostja Dec 07 '12 at 16:42
  • @BalusC, if I can hide it then it is better but neither i got success in hiding it nor in replacing it... – Narayan Subedi Dec 07 '12 at 16:51
  • Does this answer your question? [Customize FacesServlet to get rid of .xhtml extension](https://stackoverflow.com/questions/18508329/customize-facesservlet-url-pattern-to-get-rid-of-xhtml-extension) – BalusC Mar 22 '23 at 12:41

3 Answers3

14

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

Community
  • 1
  • 1
kostja
  • 60,521
  • 48
  • 179
  • 224
  • using above link i could not solve my problem, and i do not want to use other libraries... any help please... – Narayan Subedi Dec 07 '12 at 15:54
  • @PrabhatSubedi please describe what you did and what went wrong precisely. I am still not quite clear about what you are trying to achieve - changing or hiding the extensions? BTW - it would be best to add this addidtional info directly to your answer instead of an extended comment. – kostja Dec 07 '12 at 16:04
  • @PrabhatSubedi this is strange - I have tried it just now and it did work for me. You could try reducing the range of things that could have gone wrong - remove the other servlets and the filter perhaps. – kostja Dec 07 '12 at 17:07
  • kostja and Baluc, thanx for your kind help. I solved my problem. I was using /faces/.../page.xhtml in every link. I erased that /faces/ and now it works. Again thank you all... – Narayan Subedi Dec 08 '12 at 06:57
1

You just need to add following code in web.xml

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

<servlet-mapping>
    <servlet-name>faces</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

<context-param>    
    <param-name>org.omnifaces.FACES_VIEWS_SCAN_PATHS</param-name>    
    <param-value>/*.xhtml</param-value>
</context-param>

and add omnifaces dependency in pom.xml

    <dependency>
        <groupId>org.omnifaces</groupId>
        <artifactId>omnifaces</artifactId>
        <version>2.6.6</version>
   </dependency>

It will hide .xhtml extension from the url.

0

There is a way to use Dispatcher View pattern in which it is possible to map any url to any xhtml page (from /WEB-INF).

For exampe, url http://localhost:8080/sms/faces/admin/account/process_m_fee could be forwarded to /WEB-INF/faces/admin/account/process_monthly_fee.xhtml. So url-s to and on jsf pages could be without .xhtml extension.

You may look details here (also look last comment)

Community
  • 1
  • 1