JBoss.orgCommunity Documentation
This chapter covers those components used to handle and manage resources and beans.
The <a4j:mediaOutput>
component is used for generating images, video, sounds, and other resources defined on the fly.
The createContent
attribute points to the method used for generating the displayed content.
If necessary, the value
attribute can be used to pass input data to the content generation method specified with createContent
. The cacheable
attribute specifies whether the resulting content will be cached or not.
The mimeType
attribute describes the type of output content, and corresponds to the type in the header of the HTTP request. The element
attribute defines XHTML element used to display the content:
img
object
applet
script
link
a
Example 4.1. <a4j:mediaOutput>
example
This example uses the <a4j:mediaOutput>
component to generate a JPEG image of verification digits. The code on the application page is a single element:
<a4j:mediaOutput element="img" cacheable="false" session="false"
createContent="#{mediaBean.paint}" value="#{mediaData}"
mimeType="image/jpeg" />
The <a4j:mediaOutput>
component uses the MediaBean.paint
method to create the image. The method generates a random number, which is then converted into an output stream and rendered to a JPEG image. The MediaBean
class is as follows:
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);
}
}
}
Another class, MediaData
is required by the value
attribute for keeping data to be used as input for the content creation method. The MediaData
class is as follows:
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 */
...
}
The <a4j:mediaOutput>
component uses the MediaBean
and MediaData
classes to generate a new image on each page refresh.
A bean class passed using the value
attribute of <a4j:mediaOutput>
should implement the Serializable
interface so that it will be encoded to the URL of the resource.