javax.faces.DEFAULT_SUFFIX not working
Asked Answered
B

1

5

Ive been reading some posts about javax.faces.default_suffix but without success when trying to implement it.

Using : jsf 2.0, jboss 7.1, Mojarra 2.1.5

  • I need to show in URL the following : localhost:8080/myproject/index.jsf
  • when navigating also need show the xxx.jsf

web.xml

<welcome-file-list>
    <welcome-file>/comum/inicio/index.xhtml</welcome-file>
</welcome-file-list>

<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>  **have tried *.jsf but with no success**
</servlet-mapping>

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

Would you help me on this issue please ? thanks

Buhrstone answered 11/7, 2013 at 16:42 Comment(0)
D
14

You're mixing the meaning of the default suffix and the URL pattern.

The javax.faces.DEFAULT_SUFFIX represents the default suffix of the physical file you've in your webapplication which represents a JSF file. This defaults in JSF 2.0 to .xhtml. If you change it to .jsf, then you should rename all physical files from some.xhtml to some.jsf. This makes generally no utter sense. You should not do that, just get rid of that context param altogether.

The <url-pattern> represents the default URL pattern which the enduser has to use in request URL in order to invoke the FacesServlet (which in turn uses the default suffix configuration to locate the physical file based on the URL). You said that you want to use *.jsf in URLs, however you have set it to *.xhtml. This is not right and changing the default suffix is not the right solution.

You should just set the URL pattern alone, not the default suffix.

<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
</servlet-mapping>

This way http://localhost:8080/myproject/index.jsf will work.

Then there's a third problem: you're completely misunderstanding the purpose of the welcome file. It should not represent the path to the homepage. It should represent the filename of the physical file which you'd like to serve up as default file when a folder like /, /foo/, /foo/bar/, etc is requested. Just set it to index.jsf.

<welcome-file-list>
    <welcome-file>index.jsf</welcome-file>
</welcome-file-list>

However, you should keep in mind that the container will verify the existence of the physical file before continuing the request, so that it can properly show a 404 error if absent. As *.jsf is actually a virtual URL, that step will fail. You can solve that by fooling the container by placing a physically existing but empty index.jsf file next to the index.xhtml file in the desired folder.

This way http://localhost:8080/myproject/ will work, provided that you have a real index.xhtml file and empty index.jsf file in the root folder.

Much easier is to just get rid of virtual URLs and stick to *.xhtml all the time.

See also:

Despondency answered 11/7, 2013 at 16:53 Comment(4)
[RESOLVED] Just perfect @Despondency .You are right about simplicity. Why to complicate it? I prefer to stick *.xhtml only. thanks againBuhrstone
How is javax.faces.DEFAULT_SUFFIX useful? It does not do anything right?Allodium
I have a problem. Everything is working but if I go to /index.xhtml file I get the xhtml source code (bypassing JSF processing, so user can see the JSF2 tags, etc). Id like .xhtml to be processed as if .jsf suffix was used. How can I achieve that? (I've tried including a servlet-mapping for *.xhtml, but it does not work)Enrique
@icordoba: Since JSF 2 you can use *.xhtml as the URL pattern. This makes it impossible to access the source file.Trace

© 2022 - 2024 — McMap. All rights reserved.