1

Here is my setup:

  • Web app 's folder structure and file names are exactly the same as the UnmappedResourceHandler 's javadoc
  • UnmappedResourceHandler is already registered in faces-config.xml
  • /javax.faces.resource/* is already mapped to facesServlet in web.xml

The style.css is :

body {
    background: url("image/background.png");
}

body .test{
    background-image: url("#{resource['css:image/background.png']}");
}

Then I request http://localhost:8080/app/javax.faces.resource/style.css?ln=css and the respond is :

body {
    background: url("image/background.png");
}

body .test{
    background-image: url("/app/javax.faces.resource/image/background.png?ln=css");
}

I expect that all the relative URLs in CSS will be converted to the JSF 's valid URL like what #{resource} does such that I do not have to use #{resource} to refer to the relative URLs in CSS anymore , but the background 's relative URL of the body selector still remains unchanged .


Update to BalusC 's reply:

  1. If resource library is used , adding ?ln=libraryname to all CSS images will work!

  2. But if resource library is not used , <h:outputStylesheet name="css/style.css" /> generates <link rel="stylesheet" media="screen" type="text/css" href="/app/javax.faces.resource/css/style.css.xhtml">

    If I understand correctly from this , using UnmappedResourceHandler and mapping /javax.faces.resource/* to the facesServlet in web.xml should cause JSF generates the link of the style.css without xhtml extension.

Community
  • 1
  • 1
Ken Chan
  • 84,777
  • 26
  • 143
  • 172

1 Answers1

1

You're using css as a resource library as in:

<h:outputStylesheet library="css" name="style.css" />

This is not right. It's just a folder:

<h:outputStylesheet name="css/style.css" />

This will produce /javax.faces.resource/css/style.css URL instead of /javax.faces.resource/style.css?ln=css. You've otherwise to specify it in the image URL as well:

background: url("image/background.png?ln=css");

I will update the javadoc to clarify that more.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks BalusC! I tried your suggestions and still cannot get it work . Please see my update in the question . Thanks so much. – Ken Chan May 31 '13 at 12:52
  • Did you deregister the `UnmappedResourceHandler`? The `` shouldn't produce an URL with `.xhtml` extension. Do you have by chance any other `ResourceHandler` in your webapp which is possibly overruling the `UnmappedResourceHandler`? The `library` is unnecessary and only adds confusion and maintenance trouble (you've to specify `?ln=libraryname` in all CSS images). Just get rid of it. – BalusC May 31 '13 at 13:03
  • I tried again if resource library is used , adding `?ln=libraryname` to all CSS images will work! So , the current problem is if resource library is not used , `` produces the URL with .xhtml extension . The `UnmappedResourceHandler` is already registered and there are no other `ResourceHandler` overrules it as I specially create this webapp from scratch in order to try `UnmappedResourceHandler`. – Ken Chan May 31 '13 at 15:22
  • Btw , according to the original feature request (https://code.google.com/p/omnifaces/issues/detail?id=119) , I originally thought that the purpose of `UnmappedResourceHandler` is to allow the URL in CSS to be defined as normal way and does not require any changes in order to suit JSF resource system. So , it seems I misunderstood it before as URL in css is still need to be changed by adding `?ln=libraryname` when resource library is used – Ken Chan May 31 '13 at 15:26
  • JSF resource libraries are not part of standard HTML/CSS, so just omit it. The point is that you don't need to edit those 3rd party CSS files (Bootstrap, jQuery UI, etc). I can't reproduce your problem of `.xhtml` still being appended when referencing the CSS without resource library. Which JSF impl/version are you using? – BalusC May 31 '13 at 15:29
  • Yes. This is exactly the purpose of `UnmappedResourceHandler` I thought before : is to allow 3rd party CSS files (Bootstrap, jQuery UI, etc) be used directly with `` without any changes no matter resource libraries is used or not. I am using myFaces 2.1.1 .Maybe I try to use `mojarra` to see if there are any differences. – Ken Chan May 31 '13 at 15:54
  • Can't reproduce in MyFaces 2.1.11 (the current latest). Will try 2.1.1 later as I don't have it at hands now. – BalusC May 31 '13 at 15:58
  • Oh . I have typo before .Sorry about that. I am using MyFaces 2.1.11 .But I just try `mojarra 2.1.22 ` and it works . So funny. – Ken Chan May 31 '13 at 16:04
  • Did you restart the server after making changes in Facelets file? When not set to development stage, MyFaces caches Facelets files very agressively. – BalusC May 31 '13 at 16:05
  • Yes . I have already set to development stage and the restart the server . I push my testing project into [this](https://github.com/kenchanmc/jsf-test2) .I would be grateful if you can test it when you are free. – Ken Chan May 31 '13 at 21:32