Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javax.faces.DEFAULT_SUFFIX not working

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

like image 483
Al2x Avatar asked Jul 11 '13 16:07

Al2x


1 Answers

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:

  • JSF Facelets: Sometimes I see the URL is .jsf and sometimes .xhtml. Why?
  • JSF welcome file is not recognized
  • richfaces + index.xhtml having errors
like image 192
BalusC Avatar answered Oct 03 '22 10:10

BalusC