File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes *.faces suffix mapping Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "*.faces suffix mapping" Watch "*.faces suffix mapping" New topic
Author

*.faces suffix mapping

Brendan Healey
Ranch Hand

Joined: May 12, 2009
Posts: 218
I don't believe I'm asking this, but I can't get it working. I've been using Faces Servlet
prefix mapping for years and just tried a switch to suffix mapping, i.e. /faces/* to
*.faces. No matter what I do I can't get it to work, has anyone got an example. When
I try it the browser seems to want to download the file rather than display it. Thinking
back this is probably why I ended up using prefix mapping in the first place.

If I use the suffix *.xhtml I can get it working but all my non .xhtml files get .xhtml
appended, so in css files I have to specify things like background-image: url(img1.png.xhtml).



and I've got a file called index.xhtml in context root:



My project structure is a NetBeans web project generating a war file.

Thanks.
Brendan Healey
Ranch Hand

Joined: May 12, 2009
Posts: 218

So, it turns out that if I specify *.faces as the url pattern then if I have a welcome page
named index.xhtml, I have to specify the welcome-file as index.faces, even though it
isn't actually called that. Really obvious!

Then there's the issue of the stylesheets. If I include a stylesheet with hutputStylesheet
then any url references within the stylesheet have to start using a syntax like this:

background-image: url(images/background.png.faces);

so you have to append .faces (or whatever your suffix is) to each resource reference. A
way around this as pointed out to me by BalusC is to use this alternative but convoluted
syntax:

background-image: url("#{resource['images:background.png']}");

This latter syntax has the big advantage that it doesn't need to be changed if the suffix
is changed.

It's unfortunate that when people sit down to try a test JSF project that if they opt for
suffix mapping within minutes they're faced with this totally unintuitive behavior. I
recall having these problems myself and resolved them at the time by using prefix
mapping, but I wonder how many people just give up and use a different solution.

Regards,
Brendan.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15959
    
  19

Sounds like you made the common mistake of thinking that a URL is the same thing as a resource path.

/mywebapp/admin/deleteclient.jsf is a (relative) URL.

admin/deleteclient.xhtml is a resource path object.

The only reason you can use admin/deleteclient.xhtml is because you configured a URL-matching rule in web.xml that says that /*.jsf (or in your case, /*.faces) URLs get routed to the FacesServlet. The FacesServlet then deconstructs the URL, uses it to form the resource pathname admin/deleteclient.xhtml (based on JSF/Facelets config parameters), then locates that resource in the WAR and routes it to the FacesServlet View Controller.

I have no idea why you're having to put a ".faces" extension on CSS references, since I don't. After all, the images and stylesheets don't need JSF at all, and can therefore be served up as simple resources using the default URL-to-resource mapping rule that says that the url "images/funnypic.jpg" will return the contents of the WAR resource "images/funnypig.jpg".


Customer surveys are for companies who didn't pay proper attention to begin with.
Michael Brooks
Greenhorn

Joined: May 11, 2011
Posts: 11
Because I was having trouble locating my css file, the copied one from a tutorial and it worked.
In the war file, under the resources directory, there is a css\common-style.css.

The web.xml has the following entries:

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

<!-- Map these files with JSF -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

And the outputStylesheet gets converted to the following:
<link href="/JavaServerFaces/faces/javax.faces.resource/common-style.css?ln=css" rel="stylesheet" type="text/css">
And like I said, this worked.


So I went back and looked at my previous code and in my war file
I have a MyWebApp\resources\css\styles.css.

In my web.xml, I have the following entries:

<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>*.xhtml</url-pattern>
</servlet-mapping>

My outputStylesheet got converted to:
<link href="/MyWebApp/javax.faces.resource/styles.css.xhtml?ln=css" rel="stylesheet" type="text/css">

I understand that the first one is using prefix mapping with "faces" and I am using suffix mapping with ".xhtml"
but both are using jsf 2.1 and glassfish 3.1 and using firebug, the first one finds the css file while the second does not.

How do I decode the "javax.faces.resource" part of the URL? Doesn't the .xhtml get inserted onto my styles.css because I sent my web page thru the Faces Servlet?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15959
    
  19

Michael Brooks wrote:
My outputStylesheet got converted to:
<link href="/MyWebApp/javax.faces.resource/styles.css.xhtml?ln=css" rel="stylesheet" type="text/css">


How did THAT happen? If you code a raw HTML link into a JSF view, it should be copied 100% verbatim to the user's browser. It should more properly have looked something like this:


The closest I can think of to the CSS link you're reporting would be if you're using something like RichFaces which has built-in "skin" styles. In that case, yes, the CSS URL would resemble your example, but the actual CSS wouldn't be a file in your webapp, it would be something that RichFaces created dynamically in response to the URL request.


How do I decode the "javax.faces.resource" part of the URL? Doesn't the .xhtml get inserted onto my styles.css because I sent my web page thru the Faces Servlet?


To be precise, the URL gets routed to FacesServlet when it matches your pattern (*.faces). Which wouldn't happen if the URL ended with (.xhtml). The query string part of the URL is ignored.

The FacesServlet in conjunction with the Facelets filter is responsible for rewriting the ".faces" part into ".xhtml".

I think there's a filter in RichFaces which would probably be handling the "/javax.faces.resource" part of the URL.
Raaga Maalika
Greenhorn

Joined: Aug 30, 2011
Posts: 4
Hi , I faced similar issue as Brendan's. On seeing your post, I then changed web.xml mapping to look like this:




And my index.html is:



It simply redirects to index.faces (which is supposed to call index.xhtml). Index.xhtml is as follows:




...a very simple JSF file.

I have deplyed this App on JBoss. Am using eclipse IDE. When I launch the URL http://localhost:8080/MyApp/, it just says "404 : description The requested resource (/MyApp/) is not available."

"MyApp" is my project name in eclipse. Am not sure why it's reporting this error. Can someone please help?

Michael Brooks
Greenhorn

Joined: May 11, 2011
Posts: 11
THANKS for the quick response.

How did THAT happen?


Sorry, I did not mention that I am using a facelets template and in that template, I have:
<hutputStylesheet library="css" name="styles.css"/>
I am also using Primefaces 3.0.M2

The code I quoted above came out of mozilla firebug when I tried to run and debug my web app.

To be precise, the URL gets routed to FacesServlet when it matches your pattern (*.faces). Which wouldn't happen if the URL ended with (.xhtml). The query string part of the URL is ignored.


My web.xml routes.xhtml to the FacesServlet rather than .faces due to the servlet mapping below ( I think)

<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>*.xhtml</url-pattern>
</servlet-mapping>
 
wood burning stoves
 
subject: *.faces suffix mapping
 
Similar Threads
Primefaces setup causing problems JSF 2.0
Error with JBoss and JSF(My Page does not work)
JSF 2.0 - PropertyNotFoundException
No nothing works
Don't work any jfs scripts