Create new RichFaces Documentation Jira issue

This will launch the RichFaces Jira page - to complete your feedback please login if needed, and submit the Jira.

JBoss.orgCommunity Documentation

6.4.2.  < a4j:mediaOutput > available since 3.0.0

The <a4j:mediaOutput> component is a facility for generating images, video, sounds and other binary resources defined by you on-the-fly.

The <a4j:mediaOutput> component is used for generating images, videos or sounds on-the-fly. Let's consider an image creation and generate a JPEG image with verification digits for captcha (the image will include just digits without any graphical noise and distortion).

Write the following line on the page:


<a4j:mediaOutput element="img" cacheable="false" session="false" createContent="#{mediaBean.paint}" value="#{mediaData}" mimeType="image/jpeg"/>

As You see from the example above, first it is necessary to specify the kind of media data You want to generate. This can be done with the help of "element" attribute, which possible values are img, object, applet, script, link or a.

The "cacheable" defines whether the response will be cached or not. In our case we don't need our image to be cached, cause we need it to be changed every time we refresh the page.

The "mimeType" attribute defines the type of output content. It is used to define the corresponded type in the header of an HTTP response.

The <a4j:mediaOutput> attribute has two main attributes:

Now let's create the MediaBean class and specify there a primitive random-number generator and paint method that will convert the generated numbers into an output stream and give a JPEG image as a result. The code for MediaBean class is going to look as following:

Example:

package demo;


import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
public class MediaBean {
    public void paint(OutputStream out, Object data) throws IOException{
        Integer high = 9999;
        Integer low = 1000;
        Random generator = new Random();
        Integer digits = generator.nextInt(high - low + 1) + low;
        if (data instanceof MediaData) {            
            MediaData paintData = (MediaData) data;
            BufferedImage img = new BufferedImage(paintData.getWidth(),paintData.getHeight(),BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics2D = img.createGraphics();
            graphics2D.setBackground(paintData.getBackground());
            graphics2D.setColor(paintData.getDrawColor());
            graphics2D.clearRect(0,0,paintData.getWidth(),paintData.getHeight());
            graphics2D.setFont(paintData.getFont());
            graphics2D.drawString(digits.toString(), 20, 35);
            ImageIO.write(img,"png",out);
        }
    }
}

Now it is necessary to create a class that will keep transmissional data that will be used as input data for a content creation method. The code for MediaData class is going to be as following:

Example:

package demo;


import java.awt.Color;
import java.awt.Font;
import java.io.Serializable;
public class MediaData implements Serializable{
    private static final long serialVersionUID = 1L;
    Integer Width=110;
    Integer Height=50;
    Color Background=new Color(190, 214, 248);
    Color DrawColor=new Color(0,0,0);
    Font font = new Font("Serif", Font.TRUETYPE_FONT, 30);
    
    /* Corresponding getters and setters */
    
}

As a result the <a4j:mediaOutput> component will generate the following image that will be updated on each page refresh:


Hence, when using the component it's possible to output your data of any type on a page with Ajax requests.

Table of <a4j:mediaOutput> attributes.


Visit the MediaOutput page at RichFaces LiveDemo for more examples of component usage and their sources.