Advertise with us

Search Mastertheboss

Our eBooks

ModeShape with AS7 example application


Now that we have learnt how to store images in the JCR repository, we will show how to display the uploaded images. For this purpose let's add one more page which will display the single images using a Primefaces component named graphicImage.

The graphicImage is a simple but effective component which uses the StreamedContent API to display any binary image easily. This binary image could be images stored in a database, in a JCR Repository (like in our example) or images can be created programmatically on-the-fly.

Add the following display.xhtml image to your project:

<html xmlns=""


    <h:form id="form">

        <h3>Dynamic Image Display</h3>
        <p:dataTable value="#{dynamicImageController.list}" var="item"
                <f:facet name="header">Image</f:facet>
                <h:outputText value="#{}" />
                <f:facet name="header">View</f:facet>
                <h:commandButton id="book"
                    value="Show Image" />
        <br />
        <p:graphicImage value="#{dynamicImageController.image}" />

And here's the DynamicImageController, which gets injected the Repository, populates a datatable in the @Postconstruct init() method and uses the method showImage to stream the Image content:

package com.mastertheboss;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import javax.jcr.*;

import org.modeshape.jcr.api.JcrTools;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
public class DynamicImageController {

    private Repository repository; 
    private JcrTools tools = new JcrTools();

    List<Image> list = new ArrayList();

    Session session = null;

    public List<Image> getList() {
        return list;

    public void setList(List<Image> list) {
        this.list = list;

    public void init() {
        try {
            // Create a session ...
            session = repository.login("default");

            Node doc = session.getNode("/files/");
            NodeIterator nodeList = doc.getNodes();
            while (nodeList.hasNext()) {
                Node nodeImage = nodeList.nextNode();
                list.add(new Image(nodeImage.getName()));
        } catch (Exception e) {
        finally {
            // Always close the session ...
            if ( session != null ) session.logout();


    private StreamedContent image;

    public DynamicImageController() {


    public StreamedContent getImage() {
        return image;

    public void showImage(String name) {

        try {
            Node doc = session.getNode("/files/"+name);
            Node imageContent = doc.getNode("jcr:content");
            Binary content = imageContent.getProperty("jcr:data").getBinary();
            InputStream is = content.getStream();

            image = new DefaultStreamedContent(is, "image/png");

        } catch (PathNotFoundException e) {
            // TODO Auto-generated catch block
        } catch (RepositoryException e) {
            // TODO Auto-generated catch block


Once launched the display.xhtml displays the list of available pictures in the JCR Repository:

modeshape jboss as 7 tutorial

By clicking on the "Show Image", the Image is streamed and loaded into the graphicImage as shown by the following picture:

modeshape tutorial jboss as 7

I want to thank Randall Hauch (leader of the ModeShape project) for providing great tips and suggestions to this article!

Download the sample code for this article.

© 2021 All Rights Reserved.

Please publish modules in offcanvas position.