2

I've tried both org.omnifaces.resourcehandler.CDNResourceHandler and org.omnifaces.resourcehandler.UnmappedResourceHandler. It must be some misconfiguration, but I can't find what's wrong:

faces-config-xml:

<faces-config ...>
  <application>
  ....
    <resource-handler>org.omnifaces.resourcehandler.CDNResourceHandler</resource-handler>
    <resource-handler>org.omnifaces.resourcehandler.UnmappedResourceHandler</resource-handler>
  ...

web.xml:

<web-app ...>

...

  <context-param>
    <param-name>org.omnifaces.CDN_RESOURCE_HANDLER_URLS</param-name>
    <param-value>
      ionicons:ionicons.min.css=http://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css
    </param-value>
  </context-param>

...

Then on my jsf page:

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

I get no runtime warnings or errors appart from the "Resource not found" error:

Unable to find resource ionicons, ionicons.min.css

I am using mojarra 2.2.5 and tomcat 6.0.41. I've tried both omnifaces 1.7 and 1.10 (my project also uses primefaces, but I don't know if that's relevant). Any ideas on how to find what's wrong?

NotGaeL
  • 8,344
  • 5
  • 40
  • 70

1 Answers1

3

UnmappedResourceHandler doesn't support resource libraries. It's mentioned in the javadoc and showcase.

And the following CSS file reference (note: the library is not supported by the UnmappedResourceHandler! this is a technical limitation, just exclusively use name):

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

The technical limitation is that it's otherwise not possible to relatively reference resources from inside the CSS file. When using libraries, the path /ionicons would in your particular case be moved to query parameter ?ln=ionicons and then the CSS file would be looking for relative image references in the wrong relative folder /resources instead of /resources/ionicons.

Just get rid of library by moving it into the name:

<h:outputStylesheet name="ionicons/ionicons.min.css" />
<param-value>
  ionicons/ionicons.min.css=http://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css
</param-value>

Update: after all, it appears that you actually don't need CDNResourceHandler. It is primarily designed to be able to move auto-included JSF resources such as javax.faces:jsf.js of standard JSF and primefaces:jquery/jquery.js of PrimeFaces to a CDN host. Standard JSF namely doesn't offer any possibility for this.

If you actually need to reference an external CSS or JS resource, just use plain <link> or <script> instead of <h:outputStylesheet> or <h:outputScript>.

<link rel="stylesheet" href="http://cdn.example.com/style.css" />
<script src="http://cdn.example.com/script.js"></script>

If you intend to template it so that you could use target="head" of the usual JSF resource components, then declare a separate <ui:insert>. E.g. in master template

<h:head>
    ...
    <ui:insert name="head-resources" />
</h:head>

and in template client

<ui:define name="head-resources">
    ...
</ui:define>
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I know about the unmapped resource handler. But is that the case for the CDN resource handler too? Because that's the one I set up on the example, or am I wrong and I have to avoid library on CDN handler too when the unmapped is also registered? – NotGaeL Feb 16 '15 at 12:37
  • Alright, got it. And it's working now (Silly me, I don't know why I didn't try that before asking!) Thanks a lot! :-) – NotGaeL Feb 16 '15 at 12:39
  • The error came back, and this time I was able to trace it. I am not familiar with omnifaces' source code so I am probably wrong, but shouldn't this `== null` be `!= null`? https://github.com/elcodedocle/omnifaces/commit/76521b502e24e7d13acb8a0364fe963cf1d99196 – NotGaeL Feb 16 '15 at 14:12
  • Of course I was wrong about it and the code was ok! ^^' Only I was not realizing I needed a local copy of the CDN resource on my server in order for the wrapped resource handler to return a resource instead of null when calling its `createResource` method from `CDNResourceHandler.createResource` method. – NotGaeL Feb 16 '15 at 16:51
  • If you don't want to have a local resource at all, just use plain HTML `` instead. The `CDNResourceHandler` is primarily intented for delegating auto-included resources of e.g. JSF and PrimeFaces to a CDN host. – BalusC Feb 16 '15 at 19:52