2

Here's what I'm trying to do:

  1. I have a @ViewScoped JSF bean in which I call a JAX-RS service using Jersey.
  2. The resource I'm requesting returns a response with content-type image/svg+xml.
  3. Display it in a Facelet page.

My research so far has lead me to believe:

  • h:graphicImage (Core JSF) does not support SVG
  • p:graphicImage (PrimeFaces) does not support SVG
  • o:graphicImage (OmniFaces) does not support SVG either.

Is there no way to deliver an SVG image to a facelets page from a backing bean? The service that serves the SVG images will be extended later to support delivering (among other formats) PNG but I was hoping to utilize the SVG directly.

fxnn
  • 978
  • 6
  • 19
matsa
  • 346
  • 5
  • 16
  • Please be specific. Show us what you've tried so far, explain why it's not doing the right thing, instead of asking "how can I do this". – Nic Feb 04 '15 at 16:00

2 Answers2

6

The <o:graphicImage> sets a default content type of image, but your browser apparently didn't swallow that for SVG images. As per this commit, I've for OmniFaces 2.1 added SVG support for <o:graphicImage dataURI="true"> and I've added a new type attribute which allows you to explicitly specify the image type via file extension:

<o:graphicImage value="#{bean.image}" type="svg" />

In case it throws an IllegalArgumentException like this one

java.lang.IllegalArgumentException: o:graphicImage 'type' attribute must represent a valid file extension. Encountered an invalid value of 'svg'.

Then it means that your server doesn't recognize it as a registered mime mapping. You should then add a new mime mapping to server's or webapp's web.xml as below:

<mime-mapping>
    <extension>svg</extension>
    <mime-type>image/svg+xml</mime-type>
</mime-mapping>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • OmniFaces is very cool, it's just too bad I can't use it in portlets. I understand it is a technical restriction though. – Christian Wilkie Feb 05 '15 at 14:09
  • BalusC, thank you! I think you've hit the nail on the head here. I will test the snapshot tomorrow and update here afterwards. – matsa Feb 05 '15 at 14:20
  • It works perfectly! To clarify, here's my test example: The EL expression returns an InputStream. – matsa Feb 06 '15 at 08:56
  • You're welcome. Only `dataURI="true"` should not be necessary in this construct (relatively expensive). If you would still stick to `dataURI="true"`, then `type="svg"` should not be necessary. – BalusC Feb 06 '15 at 09:20
  • From your doc: "When not rendered as data URI, the InputStream or byte[] property must point to a stateless @ApplicationScoped bean (both JSF and CDI scopes are supported)." This is why I enabled it. – matsa Feb 06 '15 at 09:58
0

I haven't tried it, but can you use JSF's ui:include? Something like this:

<ui:include src="assets/img/fileFromJersey.svg" />
Christian Wilkie
  • 3,693
  • 5
  • 34
  • 49