The <o:graphicImage> is a component that extends the standard <h:graphicImage>
with support for referencing an InputStream or byte[] property in the value
attribute, optionally as a data URI.
Set dataURI attribute to true in order to render image in
data URI format.
<o:graphicImage name="icon.png" dataURI="true" /> <!-- Faces resource as data URI -->
<o:graphicImage value="#{bean.icon}" dataURI="true" /> <!-- byte[]/InputStream property as data URI -->
This basically renders the image inline in HTML output immediately during Faces render response phase. This approach is very useful for a "preview" feature of uploaded images and works also in combination with view scoped beans. This approach is however not recommended for "permanent" and/or "large" images as it doesn't offer the browser any opportunity to cache the images for reuse, ~10KB would typically be the max even less so if there are more such images on the same page.
When not rendered as data URI, the InputStream or byte[] property must point to
a stateless @GraphicImageBean or @Named @ApplicationScoped bean.
The property will namely be evaluated at the moment the browser
requests the image content based on the URL as specified in HTML <img src>, which is usually a
different request than the one which rendered the Faces page. E.g.
@Named
@RequestScoped
public class Bean {
private List<Image> images; // Image class should NOT have "content" property, or at least it be lazy loaded.
@Inject
private ImageService service;
@PostConstruct
public void init() {
images = service.list();
}
public List<Image> getImages() {
return images;
}
}
@GraphicImageBean
public class Images {
@Inject
private ImageService service;
public byte[] get(Long id) {
return service.getContent(id);
}
}
<ui:repeat value="#{bean.images}" var="image">
<o:graphicImage value="#{images.get(image.id)}" />
</ui:repeat>
A @RequestScoped and @SessionScoped bean would theoretically work, but this is wrong design
(a servlet is inherently also application scoped and stateless, not without reason). A @ViewScoped
wouldn't work because the image request doesn't share the Faces view state.
In case the property is a method expression taking arguments, each of those arguments will be converted to a string
HTTP request parameter and back to actual objects using the converters registered by class as available via
Application#createConverter(Class). So, most of standard types like Long are already implicitly
supported. In case you need to supply a custom object as argument for some reason, you need to explicitly register
a converter for it yourself via @FacesConverter(forClass).
In case your "image" entity supports it, you can also supply the "last modified" property which will be used in the
ETag and Last-Modified headers and in If-Modified-Since checks, hereby
improving browser caching. The lastModified attribute supports both Date and Long as
timestamp in milliseconds.
<ui:repeat value="#{bean.images}" var="image">
<o:graphicImage value="#{images.get(image.id)}" lastModified="#{image.lastModified}" />
</ui:repeat>
When unspecified, then the "default resource maximum age" as set in either the Mojarra specific context parameter
com.sun.faces.defaultResourceMaxAge or MyFaces specific context parameter
org.apache.myfaces.RESOURCE_MAX_TIME_EXPIRES will be used, else a default of 1 week will be assumed.
When rendered as data URI, the content type will be guessed based on content header. So far, WEBP, JPEG, PNG, GIF, ICO,
SVG, BMP and TIFF are recognized. If the content header is unrecognized, or when the image is rendered as regular
image source, then the content type will default to "image" without any subtype. This should work for
most images in most browsers. This may however fail on newer images or in older browsers. In that case, you can
explicitly specify the image type via the type attribute which must represent a valid file extension.
E.g.
<o:graphicImage value="#{images.get(image.id)}" type="svg" />
The content type will be resolved via Faces#getMimeType(String). You can add unrecognized ones as
<mime-mapping> in web.xml. E.g.
<mime-mapping>
<extension>svg</extension>
<mime-type>image/svg+xml</mime-type>
</mime-mapping>
When serving a SVG image, you can use fragment attribute to trigger
SVG view modes
(beware of browser support).
E.g.
<o:graphicImage value="#{images.get(image.id)}" type="svg" fragment="svgView(viewBox(0,50,200,200))" />
Since OmniFaces 3.10, you can set the lazy attribute to true to indicate that the
referenced image should only be loaded when the window is finished loading and the image is visible in the viewport.
<o:graphicImage ... lazy="true" />
This attribute is ignored when the dataURI attribute is set to true.
The bean class name and method name will end up in the image source URL. Although this is technically harmless and not tamperable by hackers, you might want to choose a "sensible" class and method name for this purpose.
Like <h:graphicImage>, the value attribute is ignored
when the name attribute is specified (for Faces resources). And, the value attribute of
<o:graphicImage> does not support URLs anymore. For that, just keep using
<h:graphicImage> or even plain <img>.
| Info | Value |
|---|---|
| Component Type | org.omnifaces.component.output.GraphicImage |
| Handler Class | None |
| Renderer Type | None |
| Description | None |
| Name | Required | Type | Description |
|---|---|---|---|
alt | false | jakarta.el.ValueExpression
(must evaluate to java.lang.String)
| Alternate textual description of the element rendered by this component. |
binding | false | jakarta.el.ValueExpression
(must evaluate to jakarta.faces.component.UIComponent)
| The ValueExpression linking this component to a property in a backing bean. |
dataURI | false | jakarta.el.ValueExpression
(must evaluate to boolean)
| Whether or not to render image in data URI format. |
dir | false | jakarta.el.ValueExpression
(must evaluate to java.lang.String)
| Direction indication for text that does not inherit directionality. Valid values are "LTR" (left-to-right) and "RTL" (right-to-left). |
fragment | false | jakarta.el.ValueExpression
(must evaluate to java.lang.String)
| The URL fragment identifier, which will be appended to generated resource URL. This is particularly useful with SVG images with view modes. The value does not necessarily need to start with '#', this will be checked. This attribute is ignored when 'dataURI' attribute is set to 'true'. |
height | false | jakarta.el.ValueExpression
(must evaluate to java.lang.String)
| Override for the height of this image. |
id | false | jakarta.el.ValueExpression
(must evaluate to java.lang.String)
| The component identifier for this component. This value must be unique within the closest parent component that is a naming container. |
ismap | false | jakarta.el.ValueExpression
(must evaluate to boolean)
| Flag indicating that this image is to be used as a server side image map. Such an image must be enclosed within a hyperlink ("a"). A value of false causes no attribute to be rendered, while a value of true causes the attribute to be rendered as ismap="ismap". |
lang | false | jakarta.el.ValueExpression
(must evaluate to java.lang.String)
| Code describing the language used in the generated markup for this component. |
lastModified | false | jakarta.el.ValueExpression
(must evaluate to java.lang.Object)
| The "last modified" timestamp, can be either a Long, or Date, or String which
is parseable as Long. This attribute is ignored when 'name' attribute is specified or when 'dataURI'
attribute is set to 'true'. |
lazy | false | jakarta.el.ValueExpression
(must evaluate to boolean)
| Whether or not to lazily load image. |
library | false | jakarta.el.ValueExpression
(must evaluate to java.lang.String)
| The resource library name of the resource. Works the same way as on .
This attribute is only used when 'name' attribute is specified. |
longdesc | false | jakarta.el.ValueExpression
(must evaluate to java.lang.String)
| URI to a long description of the image represented by this element. |
name | false | jakarta.el.ValueExpression
(must evaluate to java.lang.String)
| The resource name of the resource. Works the same way as on .
When this attribute is specified, 'value', 'type' and 'lastModified' attributes are ignored. |
onclick | false | jakarta.el.ValueExpression
(must evaluate to java.lang.String)
| Javascript code executed when a pointer button is clicked over this element. |
ondblclick | false | jakarta.el.ValueExpression
(must evaluate to java.lang.String)
| Javascript code executed when a pointer button is double clicked over this element. |
onkeydown | false | jakarta.el.ValueExpression
(must evaluate to java.lang.String)
| Javascript code executed when a key is pressed down over this element. |
onkeypress | false | jakarta.el.ValueExpression
(must evaluate to java.lang.String)
| Javascript code executed when a key is pressed and released over this element. |
onkeyup | false | jakarta.el.ValueExpression
(must evaluate to java.lang.String)
| Javascript code executed when a key is released over this element. |
onmousedown | false | jakarta.el.ValueExpression
(must evaluate to java.lang.String)
| Javascript code executed when a pointer button is pressed down over this element. |
onmousemove | false | jakarta.el.ValueExpression
(must evaluate to java.lang.String)
| Javascript code executed when a pointer button is moved within this element. |
onmouseout | false | jakarta.el.ValueExpression
(must evaluate to java.lang.String)
| Javascript code executed when a pointer button is moved away from this element. |
onmouseover | false | jakarta.el.ValueExpression
(must evaluate to java.lang.String)
| Javascript code executed when a pointer button is moved onto this element. |
onmouseup | false | jakarta.el.ValueExpression
(must evaluate to java.lang.String)
| Javascript code executed when a pointer button is released over this element. |
rendered | false | jakarta.el.ValueExpression
(must evaluate to boolean)
| Flag indicating whether or not this component should be rendered (during Render Response Phase), or processed on any subsequent form submit. The default value for this property is true. |
role | false | jakarta.el.ValueExpression
(must evaluate to java.lang.String)
| Per the WAI-ARIA spec and its relationship to HTML5 (Section title ARIA Role Attriubute), every HTML element may have a "role" attribute whose value must be passed through unmodified on the element on which it is declared in the final rendered markup. The attribute, if specified, must have a value that is a string literal that is, or an EL Expression that evaluates to, a set of space-separated tokens representing the various WAI-ARIA roles that the element belongs to. It is the page author's responsibility to ensure that the user agent is capable of correctly interpreting the value of this attribute. |
style | false | jakarta.el.ValueExpression
(must evaluate to java.lang.String)
| CSS style(s) to be applied when this component is rendered. |
styleClass | false | jakarta.el.ValueExpression
(must evaluate to java.lang.String)
| Space-separated list of CSS style class(es) to be applied when this element is rendered. This value must be passed through as the "class" attribute on generated markup. |
title | false | jakarta.el.ValueExpression
(must evaluate to java.lang.String)
| Advisory title information about markup elements generated for this component. |
type | false | jakarta.el.ValueExpression
(must evaluate to java.lang.String)
| The image type, represented as file extension. E.g. "webp", "jpg", "png", "gif", "ico", "svg", "bmp", "tiff", etc. This attribute is ignored when 'name' attribute is specified. |
url | false | jakarta.el.ValueExpression
(must evaluate to java.lang.String)
| Context-relative URL to retrieve the resource associated with this component. This is an alias for the "value" property. |
usemap | false | jakarta.el.ValueExpression
(must evaluate to java.lang.String)
| The name of a client side image map (an HTML "map" element) for which this element provides the image. |
value | false | jakarta.el.ValueExpression
(must evaluate to java.lang.Object)
| The current value of this component. |
width | false | jakarta.el.ValueExpression
(must evaluate to java.lang.String)
| Override for the width of this image. |
Output generated by Vdldoc View Declaration Language Documentation Generator.